C 风格的字符数组 - 我们存储多少字节?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3443222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C style char arrays - How many bytes do we store?
提问by Carlo del Mundo
char firstName[32];
I understand that each char occupies 1 byte in memory. So does the above occupy 32 bytes of memory?
我知道每个字符在内存中占用 1 个字节。那么以上是不是占用了32字节的内存呢?
Am I missing a pointer that takes up memory too or is this just 32 bytes?
我是否错过了一个也占用内存的指针,或者这只是 32 个字节?
回答by James Curran
No, that takes up exactly 32 bytes of memory. There is no pointer.
不,这正好占用了 32 字节的内存。没有指针。
This is often an area of confusion, since an array name silently "decays" to a "char*"
这通常是一个混乱的领域,因为数组名称会默默地“衰减”为“char *”
char* fname = firstName;
So, firstName
may be of type const char*
, but it is not itself a char* variable. It is exactly like:
因此,firstName
可能是 type const char*
,但它本身不是 char* 变量。它就像:
int x = 5;
x
is int variable and takes up space. 5
on the other hand, is just a constant value of int type. It takes of no space; it's just a value.
x
是 int 变量并占用空间。 5
另一方面,只是一个 int 类型的常量值。不占空间;这只是一个值。
回答by chaitanyavarma
It occupies exactly 32 bytes of memory.Internally everything an address.Variables are only for our understanding.
它正好占据了 32 字节的内存。内部所有的东西都是一个地址。变量仅供我们理解。
回答by Thomas
This is just 32 bytes. The name of the array sometimes acts likea pointer to the first element, but it is nota pointer.
这只是 32 个字节。数组的名称有时就像指向第一个元素的指针,但它不是指针。
回答by David Thornley
That takes up 32 bytes. It will hold a 31-character string (the other byte is for the null string terminator).
这占用了 32 个字节。它将保存一个 31 个字符的字符串(另一个字节用于空字符串终止符)。
回答by Collin Dauphinee
The statement char firstName[32]
creates an array of 32 characters, or 32 bytes, on the stack. Because it's on the stack, the compiler knows exactly where it is in relation to the stack pointer. The compiler will hardcode the address of the array into any operations that use it; there's no need for storing a pointer to it.
该语句char firstName[32]
在堆栈上创建一个包含 32 个字符或 32 个字节的数组。因为它在堆栈上,编译器确切地知道它相对于堆栈指针的位置。编译器会将数组的地址硬编码到任何使用它的操作中;不需要存储指向它的指针。
It's important to note that if you attempt to pass this array as a function argument, it will degrade into a pointerto the array, because C++ doesn't allow passing primitive arrays by value. Most people who are new to C++ would expect it to pass a copy of the array.
重要的是要注意,如果您尝试将此数组作为函数参数传递,它将降级为指向该数组的指针,因为 C++ 不允许按值传递原始数组。大多数不熟悉 C++ 的人都希望它传递数组的副本。
回答by Niko
There are two ways to go when you need, e.g. 32 bytes, to store your data. The difference in these two versions:
当您需要(例如 32 字节)来存储数据时,有两种方法可以使用。这两个版本的区别:
// version 1
char *firstName = new char[32];
// version 2
char firstName[32];
is that for version 1 the space your data allocated on the heapand you have to free before the program ends, whereas in version 2 the space is on the stack. Both will give you a variable that points to the first byte of your available space and this space is 32 bytes in both cases. People will argue that there are reasons why you might want to choose one over the other, but that is a different story.
对于版本 1,您的数据分配在堆上的空间,您必须在程序结束之前释放空间,而在版本 2 中,空间在堆栈上。两者都会给你一个变量,指向可用空间的第一个字节,在这两种情况下这个空间都是 32 个字节。人们会争辩说,您可能想要选择一个而不是另一个是有原因的,但这是另一回事。
sizeof( firstName )
大小(名字)
The interesting point is what sizeof would return and this is the size of a char pointer (depends on your system and compiler) for version 1 and 32 for version 2. Keep in mind what another user mentioned, passing firstNameto a function degrades it into a pointer.
有趣的一点是 sizeof 将返回什么,这是版本 1 和版本 2 的 32 字符指针的大小(取决于您的系统和编译器)。请记住另一个用户提到的内容,将firstName传递给函数会将其降级为一个指针。
回答by schoetbi
In the debug version there are also bytes stored beyond the array (on some compilers) to check for writing after the array. In the release version it should be 32 bytes plus one int on the stack (probably) to store the address.
在调试版本中,还有存储在数组之外的字节(在某些编译器上)以检查数组之后的写入。在发布版本中,它应该是 32 个字节加上堆栈上的一个 int(可能)来存储地址。