tags:

views:

146

answers:

6

hey folks, why does this:

char** pointer = new char*[1];
std::cout << sizeof(pointer) << "\n";

output 4? I have an array of pointers, but it should have length 1, shouldn't it?

+5  A: 

sizeof always returns a number of bytes.

Here, pointer is an ... err ... pointer and is 32 bits on 32 bits architectures, i.e. 4 bytes.

Didier Trosset
+3  A: 

When you call sizeof you're asking for how large it is in terms of bytes. A pointer is actually an integer that represents an address where the data you're pointing to is, and assuming that you're using a x32 operating system the size of an int is 4 bytes.

dave
`a pointer is an integer` looks weird to me!
Didier Trosset
actually, the pointer is a pointer. It usually corresponds to the machine word, but I wouldn't call it an int, that's mixing concepts.
roe
Ooh, think I had this problem once before. So I divide sizeof(..) by 4. Thx.
a1337q
@a1337q; the sizeof of a pointer will (most likely) always be 4 on a 32-bit archtitecture, you can't use it to determine the size of an array, no matter how you try.
roe
It's an integer, but not necessarily an `int` (although pointers are frequently the same size as ints, as both are usually the size of the CPU's internal registers)
James Curran
@a1337q: No, you don't divide by 4.
Bill
@Bill: I'll use this: sizeof(pointer)/sizeof(char*). But dividing by 4 would also work if I run the code only on my computer.
a1337q
Pointers certainly weren't integers back when C was designed. Just look at the 8086, where pointers were a pair of integers.
MSalters
@a1337: does it work if you declare an array of size 2? (Hint: No. http://codepad.org/PhVM6bGo)
Bill
+2  A: 

pointer is of type char**, whic has size of 4

what you might want is char * pointer [1]

but to have the length of such array you need the following code

int len = sizeof(pointer)/sizeof(char*)

check this out:

int * pArr = new int[5];
int Arr[5];

sizeof(pArr); //==4 for 32 bit arch
sizeof(Arr); //==20 for 32 bit arch
sizeof(Arr)/sizeof(int); //==5 for any arch
ULysses
+17  A: 

pointer is a pointer. It is the size of a pointer, which is 4 bytes on your system.

*pointer is also a pointer. sizeof(*pointer) will also be 4.

**pointer is a char. sizeof(**pointer) will be 1. Note that **pointer is a char because it is defined as char**. The size of the array new`ed nevers enters into this.

Note that sizeof is a compiler operator. It is rendered to a constant at compile time. Anything that could be changed at runtime (like the size of a new'ed array) cannnot be determined using sizeof.

Note 2: If you had defined that as:

char* array[1];
char** pointer = array;

Now pointer has essencially the same value as before, but now you can say:

 int  arraySize = sizeof(array); // size of total space of array 
 int  arrayLen = sizeof(array)/sizeof(array[0]); // number of element == 1 here.
James Curran
How to determine the size of an array at runtime? Only by keeping track of what length I created it with?
a1337q
Pretty much, that's it. Youhave to remember what size you new'ed it to. Note that the run-time library must also store that info somewhere (so it know how many destructors to run), but that not made accessible.
James Curran
Yes, or use `std::vector< >`. It has a `size` method.
MSalters
+2  A: 

sizeof does not give you the size of dynamic arrays (whose size is only determined at run-time, and could be of different size during different executions).

sizeof is always evaluated at compile-time and it gives you the size of the type in question, in this case the type is char** - a pointer (to pointer).

It is your task to keep track of the size of dynamically allocated arrays (you know how much you requested in the first place). Since it is a burden, all the more reason to use containers and the string class, which keep track of the allocation size themselves.

UncleBens
+4  A: 

I have an array of pointers

Nopes, you have a pointer to pointer to char and sizeof(pointer) shows the size of a pointer on your implementation.

char *p[10];<-- Here p is an array of 10 pointers to char, so sizeof(p) correctly shows the size of that array

Prasoon Saurav
how can I initialize such an array with new ? this was actually what I tried but failed.
a1337q
@a1337q: `sizeof()` is a compile time operator, it basically is just concerned about the *type* of its operand at *compile time*.
Prasoon Saurav