views:

1351

answers:

4

Hello,

I have set the buffer to size 100. I display the buffer in the main function where the buffer is declared. However, when I pass the buffer to the function and get the sizeof '4', I was thinking it should be 100, as that is the size of the buffer that I created in main. output: buffer size: 100 sizeof(buffer): 4

#include <string.h>
#include <stdio.h>

void load_buffer(char *buffer);

int main()
{
    char buffer[100];
    printf("buffer size: %d\n", sizeof(buffer));
    load_buffer(buffer);

    return 0;
}

void load_buffer(char *buffer)
{
    printf("sizeof(buffer): %d\n", sizeof(buffer));
}
+9  A: 

You are using the size of the pointer to the buffer (4 bytes), rather than the size of the buffer.

In C, you have to pass the size of the buffer separately, which is part of the reason buffer overruns happen so easily.

void load_buffer(char * buffer, size_t bufSize)
{    
    ...
}
Mitch Wheat
+4  A: 

What happens, is when you pass an array to a function, you only pass the address of the array in the memory, not the size of the array. What sizeof(buffer) is outputting in load_buffer() is the size of the pointer, which is four bytes.

The best way to keep the size of the buffer in the function is to change the function to:

void load_buffer(char *buffer, int length);

and the call to:

load_buffer(buffer, sizeof(buffer));

and then use length whenever you want the size of buffer.

DeadHead
+6  A: 

From the OP

void load_buffer(char *buffer)
{
    printf("sizeof(buffer): %d\n", sizeof(buffer));
}

Even though you can imagine that load_buffer() is passed the buffer by refrence, what is really happening is you are passing a pointer to char by value. The actual array is not passed so there is no way for the load_buffer() function to know the size of the buffer array

So what is sizeof(buffer) doing? It is simply returning the size of a pointer to char. If load_buffer() needs the size of the buffer it needs to be passed speratly.

Or you can create a new struct that contains both a char array and the size of the array, and pass a pointer to that struct instead, that way the buffer and it's size are always together ;)

hhafez
+11  A: 

The answers by Mitch Wheat and hhafez are completely right and to the point. I'm going to show some additional information which may prove useful sometimes.

Note that the same happens if you tell the compiler that you have an array of the right size

void load_buffer(char buffer[100]) {
    /* prints 4 too! */
    printf("sizeof(buffer): %d\n", sizeof(buffer));
}

An array as parameter is just declaring a pointer. The compiler automatically changes that to char *name even if it was declared as char name[N].

If you want to force callers to pass an array of size 100 only, you can accept the address of the array (and the type of that) instead:

void load_buffer(char (*buffer)[100]) {
    /* prints 100 */
    printf("sizeof(buffer): %d\n", sizeof(*buffer));
}

It's a pointer to the array you have in main, so you need to dereference in the function to get the array. Indexing then is done by

buffer[0][N] or (*buffer)[N]

Nobody I know is doing that and I'm neither doing it myself, because it rather complicates passing of the argument. But it's good to know about it. You can call the function like this then

load_buffer(&buffer)

If you want to accept other sizes too, i would go with the passing-N option the other two answers recommend.

Johannes Schaub - litb
+1 For doing a little extra effort
Tom