0
char *c = (char *)malloc(30*sizeof(char));
printf("%lu \n",sizeof(c));

In the above code I am trying to print the size of 'c'. No matter what number I give instead of '30' in the code, I get the sizeof(c) as 8.

What is the problem? How can I determine the size of an array?

Is length and size the same for an array?

Eimantas
  • 48,927
  • 17
  • 132
  • 168
mit23
  • 53
  • 6
  • 4
    `sizeof(c)` is a compile-time constant, not a runtime calculated value. – barak manos Feb 11 '15 at 12:42
  • 2
    Side-note: You should use `"%zu"`, not `"%lu"` for `size_t` (which is the type that `sizeof()` returns). Your code, as is, will be UB in 64-bit Windows and a few POSIX systems (because `size_t` is 64-bit, but `long unsigned int` is 32-bit). – Tim Čas Feb 11 '15 at 12:50
  • 4
    [Please don't cast the return value of `malloc()` in C](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – unwind Feb 11 '15 at 12:53
  • It's a 64-bit pointer. I dont need to look at the code or any other comments/amswers. – Martin James Feb 11 '15 at 14:28

6 Answers6

8

At the moment you are asking for the size of a pointer which on a 64 bits machine is 8 bytes. Since this is a malloc and not a real array, you can't retrieve the size of the buffer allocated through c.

If c was declared as

char c[30];

You could determine size with

size_t size = sizeof(c)/sizeof(c[0])

But to be honest if I had to do that, I would just #define the array size even though the size calculation would be stripped out at compilation. It makes the code clearer in my opinion.

Eric Fortin
  • 7,533
  • 2
  • 25
  • 33
3

You are printing the size of a char * which in your system is 8.

If you want to know the amount of memory that was "malloc"ed you must store it somewhere. In your case your should store the 30 for future use if you are going to need it.

phuclv
  • 37,963
  • 15
  • 156
  • 475
DieMartin
  • 66
  • 1
1

sizeof(c) means give me the size of the variable c. Since c is a pointer it's giving you back the number of bytes that the pointer takes up in memory. The fact you're seeing 8 suggests you're compiling for 64bit. On a 32bit built it would be 4.

It's your job to remember the size of the thing you've allocated. After all, you asked for an amount of memory, so you do know how much you allocated! In your case, size_t size = 30*sizeof(char) will give you the amount you've allocated.

Sean
  • 60,939
  • 11
  • 97
  • 136
  • Also, the actual allocated memory may be larger, so even if there were a function like `memlen`, it would be pretty much useless. For example, it might round up to a multiple of some `N` for alignment purposes. Not that you should rely on that in code, of course (if you need `k` more bytes, ask for `k` more bytes!). – Tim Čas Feb 11 '15 at 13:07
0

sizeof is an operator, not a function, thus the compiler creates the code equivalent to something like sizeof(char *) and pointer of a char (depending on the architecture of course) is of 8 bytes.

Eimantas
  • 48,927
  • 17
  • 132
  • 168
0

Actually sizeof(*c) will return 1. The size of a mallocced buffer cannot be determined compile time (which is what sizeof does).

Henk Kok
  • 191
  • 2
  • 11
0

You're getting the size of the variable c, not the size of the memory region it points to. There's no way to get the size of an array inside a function because it always decays to pointer. You must store the size yourself. If you've passed the size to malloc then why can't you store that size to a variable?

phuclv
  • 37,963
  • 15
  • 156
  • 475