sizeof

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? 
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.
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
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).
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?
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
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?
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.
Ah I always forget about that bit.

So there is really know way to get the size of an array using a pointer?
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
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
@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)
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.
Okay, I just decided to use Peter87's advice about using the vector class. Thanks again everyone.
Last edited on
Topic archived. No new replies allowed.