sizeof

Mar 15, 2012 at 4:44pm
Hi everyone. Here's my code. My question is included in the comments:

1
2
3
4
5
int array[5];
int*ptr;
cout<<sizeof(array); //output is 20, as I expected.
ptr=new int[5];
cout<<sizeof(ptr); //output is 4. Why? Shouldn't it be the same output as line 3? 
Mar 15, 2012 at 4:49pm
well, 5 elements, 20/5=4
logically, new int[5] is probably just one element, then, try new int array2[5], not sure if it will work.
Mar 15, 2012 at 4:57pm
ptr is a pointer so sizeof(ptr) gives you the size of a pointer which happens to be 4 bytes on your system.
Last edited on Mar 15, 2012 at 4:57pm
Mar 15, 2012 at 5:20pm
You are asking for the size of the pointer, not the object it is pointing to.

If you did cout << ptr; you'd get something like 0x804A1570 as an output. It's a memory address which on a 32-bit operating system is 32 bits (or 4 bytes).
Mar 15, 2012 at 5:28pm
Thanks for all the replies.

You are asking for the size of the pointer, not the object it is pointing to. 


I figured this was what it was doing. But then how can I get the size of the object that it's pointing to?

Ultimately, I want to find the length of the dynamically created integer array. I had originally intended to use sizeof(ptr) and then divide by sizeof(int) to get the length, but it seems that this is no longer possible. Any other ways?
Mar 15, 2012 at 5:45pm
There is no way to get the size of the array through the pointer. You have to remember the size somehow. You could have a separate variable that stores the size that you can use later.

1
2
3
int size = 5;
int *ptr=new int[size];
cout<<size;


You could also use std::vector that keeps track of the size for you, instead of the array.

1
2
std::vector<int> v(5);
cout<<v.size();
Last edited on Mar 15, 2012 at 5:47pm
Mar 15, 2012 at 9:46pm
Im not well versed in pointers, but couldn't OP just dereference the pointer to get what it's pointing at, and then get the size?
Mar 15, 2012 at 9:58pm
ResidentBiscuit wrote:
Im not well versed in pointers, but couldn't OP just dereference the pointer to get what it's pointing at, and then get the size?


Yes, but it will give the size of an int, not the size of the array. The pointer points to the first element of the array, not to the array itself.
Mar 15, 2012 at 10:01pm
Ah I always forget about that bit.

So there is really know way to get the size of an array using a pointer?
Mar 15, 2012 at 10:10pm
ResidentBiscuit wrote:
So there is really know way to get the size of an array using a pointer?

Only way I can think of off hand is to set the last element to a sentinel value say -999 and iterate through and count. But that would be incredibly ugly to me, considering you need to know the size to create the array it shouldn't be an issue to remember it.
Last edited on Mar 15, 2012 at 10:10pm
Mar 15, 2012 at 10:12pm
since when do you need to know the size of an array to create one?
 
int array[];

not that you can actually get the size of the array.
Last edited on Mar 15, 2012 at 10:14pm
Mar 15, 2012 at 10:37pm
@Zephilinox

That code won't work.

Something like this will:
int array[] = {1,2,3,4,5,6,7,8,9,10}; // OK - compiler knows there's 10 elements
but not just
int array[]; // error: storage size of 'array' isn't known

If you need the size of an array, count it yourself.
(or, if using dynamic memory, store the size in a variable)
Mar 16, 2012 at 12:08am
I know you can't get the size of it, as I mentioned in my reply, but you don't need to know the size of an array to create one.
Mar 16, 2012 at 12:18am
Okay, I just decided to use Peter87's advice about using the vector class. Thanks again everyone.
Last edited on Mar 16, 2012 at 12:42am
Topic archived. No new replies allowed.