Click here to Skip to main content
15,937,127 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
i am confused about the result of the sizeof command.
In the main function the result is 16 and in the printArr funtion it is 4.
Any idea?
Thx

What I have tried:

C++
void printArr(int arr[]) {

	int i = 0;
	int max = sizeof(arr);
	//int max = (sizeof(arr) / sizeof(arr[0])

	std::cout << "size of arr in function = " << sizeof(arr) << std::endl;

	do {
		std::cout << arr[i] << std::endl;
		i++;
	} while (i < max);

}

int main() {

	int arr[] = { 1,2,3,4 };

	std::cout << "size of arr in main =" << sizeof(arr) << std::endl; // SIZE = 16

	printArr(arr);

	system("pause");
	return 0;
}
Posted
Updated 16-May-20 4:44am

That's because they are measuring different things: arr in main is declared as an array of four integers: since an integer in your system is 32 bits, the size of the total array is 4 times the size of an integer == 4 * 4 == 16

In printArr it's declared as int arr[], which means it has no size information, and the declaration equates to int *arr - in fact that declaration will work exactly the same, and give you exactly the same results. Remember, all parameters in C and C++ are passed by value by default, so a copy of the value is made and passed to the function parameter rather than the "real object" being passed. With an array you don't pass a copy of the array, but a copy of the pointer to the first element (otherwise you couldn't pass any changes to the array data back to the calling function).

So in printArr, arr has no size info, and is in fact a pointer to an integer (so the system doesn't try to copy the entire array and pass your function the copy it passes a pointer to the first element)

And in your system, any pointer is 32 bits, so you get the result "4" as all pointers fit in 4 bytes.
On my system, I get "16" and "8" because my pointers are all 64 bit wide, and that needs 8 bytes to store.
 
Share this answer
 
v2
Comments
toni_1 16-May-20 10:21am    
Thank you very much !! This info helps me a lot !
OriginalGriff 16-May-20 10:39am    
You're welcome!
Griff, of course, is right.
Never pass an array by value unless you provide the size of the array. Because an array passed to a function degenerates to a pointer. The size of the pointer is the size of the memory required to hold a pointer (implementation dependent); not the size of the array. Therefore, the function that receives the pointer argument has no idea of the amount of memory it points to (future bug).

I'll give you 3 options:
1) func(int arg[], int argn n);
2) func(int *arg, int argn n);
3) func(int arg[10]);

Only number 2 makes sense and number 1 is basically the same thing. Number 3 is legal, but it limits what can be passed and excepted.

I hate the word "never", but I recommend that you never use number 3 unless you have a very explicit reason for doing so (hardware requirement - very low level).
 
Share this answer
 
Comments
[no name] 16-May-20 11:09am    
"... pass an array by value ..." in c++, how you do that? :-)
John R. Shaw 16-May-20 12:26pm    
Technically, that would be number 3. It is what 'toni_1' thought he/she was doing. Coping by value means a copy is passed on the stack, which is what number 3 is doing. What 'toni_1' was doing amounted to passing a pointer to the original array, which means any change to the array in the function would change the original. Number 3 would only change the copy. That is why we need to specify constant variables in our arguments (I tell you where the data is, but you are not allowed to change it).

:) Rubiks - best time 2.54 minutes (now, have no idea).
[no name] 16-May-20 13:23pm    
:thumbsup: :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900