Click here to Skip to main content
15,892,537 members
Please Sign up or sign in to vote.
4.33/5 (3 votes)
See more:
I want to find the size of the total memory allocated on the heap for the following statement:
int* a = new int[1000];
How can I use the sizeof operator for this?
I used:
printf("\t===> %d\n",sizeof(*a));


Is this statement correct?

I have asked the question because when I checked the memory of heap allocated in windbg it shows me the size as fc4 i.e. 4036 which is more then 4000 and not as desired. Any hint as to what may be the cause?
Posted
Updated 5-May-11 0:41am
v2

No, you can't get it like that, that'll give you the size of an int which is what a is pointing to.

In your example you'll have to do sizeof(int) * 1000 to get the allocated amount, because all you hold a reference to is the pointer, not an array. To get the amount allocated by the array it has to be declared as an array
(for example int myArray[1000];).

Consider the following three examples:

C++
int* a = new int[1000];
int b[1000];
char c[] = "Hello, world!";

cout << "sizeof(int) * 1000 = " << (sizeof(int)*1000) << endl;
cout << "sizeof a  = " << (sizeof a) << endl;
cout << "sizeof *a  = " << (sizeof *a) << endl;

cout << "sizeof b  = " << (sizeof b) << endl;
cout << "sizeof *b  = " << (sizeof *b) << endl;

cout << "sizeof c  = " << (sizeof c) << endl;
cout << "sizeof *c  = " << (sizeof *c) << endl;


Which will print something like:

sizeof(int) * 1000 = 4000
sizeof a  = 4
sizeof *a  = 4
sizeof b  = 4000
sizeof *b  = 4
sizeof c  = 14
sizeof *c  = 1


Hope this helps,
Fredrik
 
Share this answer
 
Comments
Hans Dietrich 5-May-11 5:40am    
Good complete answer. My 5.
Olivier Levrey 5-May-11 5:43am    
Yes good explanations. My 5 as well.
rupeshkp728 5-May-11 6:22am    
Thanks Fredrik for the reply.
I had asked the question because when I checked the memory of heap allocated in windbg it shows me the size as fc4 i.e. 4036 which is more then 4000 and not as desired.
Any hint as to what may be the cause?
Stefan_Lang 5-May-11 7:01am    
The debugger allocates more memory than you ask for because it saves debug information. Without this information, WinDbg wouldn't be able to tell you this size, whether it's a single structure or an array, or if the block has been freed already. I believe there's also a reference to the location where the allocation took place.

AFAIK the release version has none of this info and thus should allocate only 4000 - but trying to confirm this suffers from similar problems as trying to confirm that the light in your fridge really goes out when you close the door. ;)
Fredrik Bornander 5-May-11 8:11am    
Stefan has answered this.
You should accept my answer if it solves your question so that we can close the question.
Hi,
sizeof(*a)*1000

Will give you the total memory allocated in bytes.
 
Share this answer
 
Comments
rupeshkp728 5-May-11 6:18am    
Thanks venkat.

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