[英]Pointer array and sizeof confusion
Why does the following code output 4 ? 为什么以下代码输出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,不应该吗?
pointer
is a pointer. pointer
是一个指针。 It is the size of a pointer, which is 4 bytes on your system. 它是指针的大小,在系统上是4个字节。
*pointer
is also a pointer. *pointer
也是一个指针。 sizeof(*pointer)
will also be 4. sizeof(*pointer)
也将是4。
**pointer
is a char. **pointer
是一个char。 sizeof(**pointer)
will be 1. Note that **pointer is a char because it is defined as char**
. sizeof(**pointer)
将为1.注意**指针是一个char,因为它被定义为char**
。 The size of the array new`ed nevers enters into this. 新的阵列的大小进入了这个。
Note that sizeof
is a compiler operator. 请注意,
sizeof
是编译器运算符。 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
. 任何可以在运行时更改的内容(如新数组的大小)都无法使用
sizeof
确定。
Note 2: If you had defined that as: 注2:如果您将其定义为:
char* array[1];
char** pointer = array;
Now pointer
has essencially the same value as before, but now you can say: 现在
pointer
与以前基本上具有相同的值,但现在你可以说:
int arraySize = sizeof(array); // size of total space of array
int arrayLen = sizeof(array)/sizeof(array[0]); // number of element == 1 here.
sizeof
always returns a number of bytes. sizeof
始终返回多个字节。
Here, pointer
is an ... err ... pointer and is 32 bits on 32 bits architectures, ie 4 bytes. 这里,
pointer
是...... err ...指针,在32位架构上是32位,即4个字节。
When you call sizeof you're asking for how large it is in terms of bytes. 当你调用sizeof时,你要求它的字节大小。 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.
指针实际上是一个整数,表示您指向的数据所在的地址,假设您使用的是x32操作系统,则int的大小为4个字节。
pointer
is of type char**
, whic has size of 4 pointer
的类型为char**
,whic的大小为4
what you might want is char * pointer [1]
你可能想要的是
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
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
没有给出动态数组的大小(其大小仅在运行时确定,并且在不同的执行期间可能具有不同的大小)。
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). sizeof
总是在编译时计算 ,它给出了相关类型的大小,在这种情况下,类型是char**
- 一个指针(指向)。
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.
由于它是一种负担,所以更有理由使用容器和字符串类,它们自己跟踪分配大小。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.