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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
pointeris a pointer. It is the size of a pointer, which is 4 bytes on your system.*pointeris also a pointer.sizeof(*pointer)will also be 4.**pointeris a char.sizeof(**pointer)will be 1. Note that **pointer is a char because it is defined aschar**. The size of the array new`ed nevers enters into this.Note that
sizeofis 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 usingsizeof.Note 2: If you had defined that as:
Now
pointerhas essencially the same value as before, but now you can say: