9

Why does the following code output 4?

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

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

1
  • 1
    Why do you think you need a char**? In C++, we prefer the type std::vector<std::string>. Then you can simply call .size() and get the desired result. Commented Aug 3, 2010 at 15:07

5 Answers 5

17

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.
Sign up to request clarification or add additional context in comments.

3 Comments

How to determine the size of an array at runtime? Only by keeping track of what length I created it with?
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.
Yes, or use std::vector< >. It has a size method.
5

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.

1 Comment

and 8 bytes on a 64bit system, but only when you compile in 64bit mode
3

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.

8 Comments

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.
Ooh, think I had this problem once before. So I divide sizeof(..) by 4. Thx.
@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.
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)
@a1337q: No, you don't divide by 4.
|
2

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

1 Comment

Any idea how to get the size of "pArr" ?
2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.