开发者

size of array in c

开发者 https://www.devze.com 2023-04-08 11:20 出处:网络
a simple question that bugs me. Say I have an array defined in main like so int arr[5]. Now, if I\'m still inside main and I s开发者_运维问答et int i = sizeof(arr)/sizeof(arr[0]) then I is set to be 5

a simple question that bugs me. Say I have an array defined in main like so int arr[5]. Now, if I'm still inside main and I s开发者_运维问答et int i = sizeof(arr)/sizeof(arr[0]) then I is set to be 5, but if I pass the array as a function parameter and do the exact same calculation in this function, I get a different number. Why is that? At first I thought its because in a function arr is a pointer, but as far as I know arr is a pointer inside main too!

Also, if I do something very similar only I initialize the array dynamically, I get weird results:

int *arr = (int*) malloc(sizeof(int) * 5);
int length = sizeof(*arr) / sizeof(arr[0]);
printf("%d\n",length);

Here the output is 1. Any ideas why? Thanks in advance!


C arrays don't store their own sizes anywhere, so sizeof only works the way you expect if the size is known at compile time. malloc() is treated by the compiler as any other function, so sizeof can't tell that arr points to the first element of an array, let alone how big it is. If you need to know the size of the array, you need to explicitly pass it to your function, either as a separate argument, or by using a struct containing a pointer to your array and its size.


This is because arr is now a pointer and it could point to a single int or an array of 1000 ints the function just does not know. You will have to pass the size of the array into the function.

In main arr is declared as an int[5] and so the size can be calculated by the compiler.


I didn't understand the first question (post some code, please), but for the second:

sizeof(*arr) ;   // is sizeof(int) as arr is of type "int *"
sizeof(arr[0]) ; // is sizeof(int), as arr[0] is the first
                 //     item of an array of int

Fact is, *arr and arr[0] mean exactly the same thing, but said differently. In fact, let say n is an index: *(arr + n) is the same than arr[n].


as far as I know arr is a pointer inside main too!

That's the mistake. In main, where you defined int arr[5], arr is an array, not a pointer. Its size is equal to the size of the array, so 5*sizeof(int). When you passed it as a function parameter, that parameter had type int*, so inside the function, you were taking the size of an int* rather than an int[5].

Here the output is 1. Any ideas why?

This time, arr really is a pointer, since you declared it int *arr. But either way, whether it's int *arr or int a[5], *arr and arr[0] mean exactly the same thing: the first element. So of course they have the same size, and sizeof(*arr) / sizeof(arr[0]) is 1.


It's because of the difference between an array of known size and a pointer to an array of unknown size. sizeof is done at compile time and it's not possible for the compiler to tell the size of a dynamically created memory region in advance.


You should understand the difference between static and dynamic arrays. Static arrays are types like int, float, double etc. They are different. Even

int a[10];

has a different type from

int b[11];

At compile time, the number of elements in static arrays are known and the sizeof operator returns the number of bytes they occupy.

With pointers that were initialized, either to point to some variable, or to an allocated memory, it is at run-time that it would be apparent where they are pointing to and the compiler cannot determine what would be the size of that array in the future. Therefore, the sizeof a pointer gives you 4 (or 8 in 64bits systems for example).

Note that sizeof operator works at compile-time and not at run-time.

If you need to know the size of an allocated memory (through malloc), you have no choice but to save the size of the array in another variable, for example right after you did the malloc.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号