tags:

views:

269

answers:

5

Possible Duplicate:
newbie questions about malloc and sizeof

I am trying to read strings into a program. When I noticed that the strings were sometimes being corrupted, I tried the following code:

 void *mallocated = malloc(100);
 printf("sizeof(mallocated) = %d\n", sizeof(mallocated));

According to my program, the size of mallocated was 8, even though I allocated 100 bytes for it. Because of this, whenever I try to store a string longer than 8 bytes, everything after the 8th bit will sometimes disappear. Why is this happening, and how can I prevent it?

+8  A: 

Because the size of the "string" pointer is 8 bytes. Here are some examples of using sizeof() with their appropriate "size". The term size_of() is sometimes deceiving for people not used to using it. In your case, the size of the pointer is 8 bytes.. below is a representation on a typical 32-bit system.

sizeof (char)   = 1
sizeof (double) = 8
sizeof (float)  = 4
sizeof (int)    = 4
sizeof (long)   = 4
sizeof (long long)  = 8
sizeof (short)  = 2
sizeof (void *) = 4

sizeof (clock_t)    = 4
sizeof (pid_t)  = 4
sizeof (size_t) = 4
sizeof (ssize_t)    = 4
sizeof (time_t) = 4

Source

You are leaving out how you are determining your string is disappearing (char array). It is probably being passed to a function, which you need to pass the explicit length as a variable or track it somewhere. Using sizeof() won't tell you this.

See my previous question about this and you'll see even my lack of initial understanding.

0A0D
It's good to see you able to apply your learning from your previous question.
Jonathan Leffler
Thanks for the help. The problem with the program wasn't quite what I thought it was (is it ever?) but your advice was very useful.
Michael Dickens
+3  A: 

In C89, sizeof operator only finds the size of a variable in bytes at compile time (in this case a void pointer of 8 bytes). It works the way you'd expect it to work on plain arrays, because their size is known at compile time.

char arr[100]; // sizeof arr == 100
char *p = arr; // sizeof p == 4 (or 8 on 64-bit architectures)
char *p = malloc(100); // sizeof p == 4 (or 8). Still!

To know the size of heap-allocated memory, you need to keep track of it manually, sizeof won't help you.

Alex B
That's not true. In C99, `sizeof` returns the size of a variable length array, which in many cases can only be determined at runtime.
dreamlax
Thanks, I excluded C99 case from the answer to keep things simpler.
Alex B
+4  A: 

sizeof returns the size of the pointer (void *) that you gave it, not the size of the memory you allocated. You would have to store the size of the memory in a separate variable if you want to use it later.

Kyle Lutz
+3  A: 

You cannot. As pointed out, you can get the size of the void * mallocated, but that does not tell you much.

You cannot get the size of the malloed *mallocated. That is to say, there is no function call to return 100 (in your example) - unless you write your own memory management routines.

Simplest is just to remember it somewhere ... maybe ....

stuct {
  void *date;
  unsigned int size
 } myStructureWhichRemebersItsSize;

myStructureWhichRemebersItsSize *someData;
someData.data = malloc(100);  // and check for NULL
someData.size = 100;
Mawg
"You cannot get the size of the malloed"... Not true, it depends on your C libraries. Under MS's CRT STDLIB, you can simply call `_msize(*mallocated)` to get the "100" that the OP seeks.
clintp
+1 Thanks for the info. I don't use MS, so did not know that. A useful feature that would be nice to see in more places. Of course, behind the scenes the compiler is probably generating a structure such as I describe.
Mawg
A: 

void* is the type of a location in memory if you don't know what it contains. It should be avoided.

char* is the type of a value which points to some location in memory which holds a char. Identifying a location in memory takes eight bytes.

sizeof tells you how many bytes a particular type takes. Not how many were allocated with malloc but just how much memory compiler knows the type should take. Applying sizeof to values is often considered bad style, and as someone else mentioned here, sometimes invokes smart behavior in C99.

char[100] the type of a value which holds 100 chars. char[100] a; is a string of 100 chars on the stack.

char(*)[100] is the type of a pointer to a value that holds 100 chars. char(*b)[100]; makes b point to 100 chars, possibly on the heap. What you probably want is

char (*s)[100] = malloc( sizeof( char[100] ) );
printf( "%u byte pointer to %u bytes of size %u elements\n",
  sizeof s, sizeof *s, sizeof **s );
Potatoswatter