0

I want to calloc 25 bytes of memory exactly. In this case, msg_len = 5 bytes, since that is what the input is. This is the code I have:

  int full_msg_size = 20 + msg_len;
  printf("full_msg_size: %d\n", full_msg_size);
  void *full_msg = calloc(full_msg_size, sizeof(char));
  printf("size of full_msg: %d\n", (int*)sizeof(full_msg));

This is what prints out:

full_msg_size: 25 size of full_msg: 8

But I want size of full_msg to equal 25 bytes just like full_msg_size.

I also want all the memory spaces initialized to zero.

Can anyone tell me how to calloc/malloc this properly?

Thanks

umdcoder
  • 137
  • 1
  • 2
  • 15

4 Answers4

4

You are most likely allocating the correct number of bytes but you are asking how big a pointer to a void is. And on a 64 bit box it will very likely be 8 bytes. If it failed to allocate the amount of memory you asked for then full_message would have the value NULL.

Matthew V Carey
  • 196
  • 1
  • 10
3

The sizeof operator is (almost always) a compile-time operator (exception: VLAs). So the sizeof some pointer is always the same (on your machine, it is always 8) and does not depend of the size passed to some malloc or calloc function.

Given a malloc-ed or calloc-ed pointer, there is no portable way to know the size requested at its allocation time. You should keep that size somewhere! You might use a flexible array member like here if you wanted to keep that dynamic size inside the allocated structure. You often keep the size elsewhere i.e. in some variable (full_msg_size in your case).

So your calloc is working (BTW, you should test it against failure).

void *full_msg = calloc(full_msg_size, 1);
if (!full_msg) {perror("calloc"); exit(EXIT_FAILURE); };

But you cannot use sizeof to compute the runtime size of the allocated zone.

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
1

In the context of your post, it seems that you really want to determine the length of a string contained by a variable, as opposed to the size of the variable. sizeof when applied to a pointer, will tell you the size of the address, not the length of the string. If you need to determine the length of a string at runtime, use the function strlen:

`int len = strlen(some_str)`    

You may be getting full_msg_size: 25 size of full_msg: 8 because sizeof
is returning the size of the address of full_msg.

So, your function:

printf("size of full_msg: %d\n", (int*)sizeof(full_msg));  

Should be:

printf("length of full_msg: %d\n", strlen(full_msg));

if you want to want to calloc 25 bytes of memory exactly:

char *full_msg = {0};
full_msg = calloc(25, 1); //all space initialized to 0     
//or you can also use:  
full_msg = malloc(25);  

full_msg now has memory for exactly 25 bytes, (but one needs to be used for '\0')

The sizeof operator when used on a char *str, returns to you the size of a char *, which is just an address.

Note: The size of an address (or pointer) will not always be 8 bytes just because you are running a 64 bit machine, it depends on what the application is compiled to: 32bit or 64bit. i.e., For a 32 but application, size of an address is 4 bytes, for a 64 bit application, it will be 8 bytes.

ryyker
  • 22,849
  • 3
  • 43
  • 87
1

You are printing size of pointer which will be 8 bytes in the case of 64-bit machines.

Karthik Balaguru
  • 7,424
  • 7
  • 48
  • 65
  • 1
    _size of pointer which will be 8 bytes in the case of 64-bit machines_. Not always true. Only if the target for the application using the code is compiled to be 64bit. Many applications, even when built on a 64 bit machine are built as 32bit. In that case, size of an address (pointer) will be 32 bits or 4 bytes. – ryyker Oct 24 '14 at 16:34
  • Yes, i was also referring to the same point only but did not mention it explicitly. – Karthik Balaguru Oct 24 '14 at 16:44