I'm experiencing some behavior I don't understand with malloc.
For instance, allocating memory for a structure works just fine, i.e.:
typedef struct my_struct {
char buffer[4096];
struct my_struct *next;
} MY_STRUCT;
...
MY_STRUCT *ptr = (MY_STRUCT *)malloc(sizeof(struct my_struct));
printf("malloc() gave us %lu bytes\n", sizeof(*ptr));
printf("My structure's first member is %lu in length\n", sizeof(ptr->buffer));
free(ptr);
...
Returns
malloc() gave us 4104 bytes
My structure's first member is 4096 in length
...exactly as expected. Now, when I try to dynamically allocate a buffer for a character string:
int bufsize = 4096;
char *buffer = (char *)malloc(sizeof(char)*bufsize);
printf("bufsize: %d\n", bufsize);
printf("Allocated buffer size: %lu\n", sizeof(*buffer));
free(buffer);
...returns
bufsize: 4096
Allocated buffer size: 8
Now, I can hard-code the malloc()
call to 4096, 1, 4, anything really... It always comes up 8.
What am I getting wrong here?