25

Possible Duplicate:
How to find the sizeof(a pointer pointing to an array)

I'm learning how to create a dynamic array in C, but have come across an issue I can't figure out.

If I use the code:

int num[10];
for (int i = 0; i < 10; i++) {
    num[i] = i;
}
printf("sizeof num = %li\n sizeof num[0] = %li", sizeof(num), sizeof(num[0]));

I get the output:

sizeof num = 40
sizeof num[0] = 4

This is what I'd expect to happen. However if I malloc the size of the array like:

int *num;
num = malloc(10 * sizeof(int));
for (int i = 0; i < 10; i++) {
    num[i] = i;
}
printf("sizeof num = %li\n sizeof num[0] = %li", sizeof(num), sizeof(num[0]));

Then I get the output:

sizeof num = 8
sizeof num[0] = 4

I'm curious to know why the size of the array is 40 when I use the fixed length method, but not when I use malloc().

1
  • num is pointer to int in second case and its size depends on machine i.e for 32 bits it is of 4 byte and for 64 bit it is of 8 bytes.while in 1st case num is not pointer it is base address of array so sizeof() gives the total size of array. . Commented Oct 3, 2017 at 3:32

4 Answers 4

32

In the second case, num is not an array, is a pointer. sizeof is giving you the size of the pointer, which seems to be 8 bytes on your platform.

There is no way to know the size of a dynamically allocated array, you have to save it somewhere else. sizeof looks at the type, but you can't obtain a complete array type (array type with a specified size, like the type int[5]) from the result of malloc in any way, and sizeof argument can't be applied to an incomplete type, like int[].

Sign up to request clarification or add additional context in comments.

4 Comments

you know how free function is working, compiler or O/S keeping record/track of heap allocated chunks,then with only providing pointer it can be freed no need of number of num , then why compiler vendors can provide a functionality to users to retrieve this num from heap records? Since it's already been there... whenever the user calls free(ptr)
@BuddhikaChaturanga Sorry I'm not sure I understood correctly: are you asking why don't implementations expose details about heap allocated chunks of memory? Like, given a pointer obtain information?
yeah ... If they have information about dynamically allocated data already, if they use it for free then why can't they provide functionality to user(Developers)in order to obtain such information ... :)
@BuddhikaChaturanga if an implementation exposed such details then they would part of their interface and they won't be able to freely change it in the future. Besides, you can also have pointers to the middle of an allocated chunk of memory: what would you do in such cases?
5

Arrays are not pointers (the decay to pointers in some situations, not here).

The first one is an array - so sizeof gives you the size of the array = 40 bytes.

The second is a pointer (irrespective of how many elements it points to) - sizeof gives you sizeof(int*).

Comments

2

The second size refers to the size of a pointer, that, in your machine -- probably 64bits --, is 8 bytes.

You cannot use sizeof() to recover the size of a dynamically allocated structure, but you can do so for statically allocated ones.

Comments

2

If you want to know the size of something you have allocated, then you need to "remember" that yourself, since your code did the allocation. If your code hasn't done the allocation, then there's no way [in a standard sense] to find out how large the memory are a pointer is pointing to. You just have to "know" some other way.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.