简体   繁体   English

C样式字符数组 - 我们存储多少字节?

[英]C style char arrays - How many bytes do we store?

char firstName[32];

I understand that each char occupies 1 byte in memory. 我知道每个char占用内存中的1个字节。 So does the above occupy 32 bytes of memory? 那么上面占用了32个字节的内存吗?

Am I missing a pointer that takes up memory too or is this just 32 bytes? 我错过了一个占用内存的指针,还是只有32个字节?

No, that takes up exactly 32 bytes of memory. 不,这占用了大约32个字节的内存。 There is no pointer. 没有指针。

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. 因此, firstName可以是const char*类型,但它本身不是char *变量。 It is exactly like: 它完全像:

 int x = 5;

x is int variable and takes up space. x是int变量并占用空间。 5 on the other hand, is just a constant value of int type. 另一方面, 5只是int类型的常量值。 It takes of no space; 它没有空间; it's just a value. 这只是一个价值。

它占用了正好32个字节的内存。内部的一切都是一个地址。变量只是为了我们的理解。

This is just 32 bytes. 这只是32个字节。 The name of the array sometimes acts like a pointer to the first element, but it is not a pointer. 数组的名称有时指向第一个元素的指针,但它不是指针。

That takes up 32 bytes. 这需要32个字节。 It will hold a 31-character string (the other byte is for the null string terminator). 它将包含一个31个字符的字符串(另一个字节用于空字符串终止符)。

The statement char firstName[32] creates an array of 32 characters, or 32 bytes, on the stack . 语句char firstName[32]堆栈上创建一个32个字符或32个字节的数组。 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. 不需要存储指针。

It's important to note that if you attempt to pass this array as a function argument, it will degrade into a pointer to the array, because C++ doesn't allow passing primitive arrays by value. 重要的是要注意,如果您尝试将此数组作为函数参数传递,它将降级为指向数组的指针 ,因为C ++不允许按值传递基本数组。 Most people who are new to C++ would expect it to pass a copy of the array. 大多数不熟悉C ++的人都希望它能传递一个数组副本。

There are two ways to go when you need, eg 32 bytes, to store your data. 当您需要时,有两种方法可以存储,例如32字节,以存储您的数据。 The difference in these two versions: 这两个版本的区别:

// version 1
char *firstName = new char[32];

// version 2
char firstName[32];

is that for version 1 the space your data allocated on the heap and you have to free before the program ends, whereas in version 2 the space is on the stack . 对于版本1,您的数据在堆上分配的空间必须在程序结束前释放,而在版本2中,空间在堆栈上 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. 两者都会给你一个指向可用空间的第一个字节的变量,在这两种情况下这个空间都是32个字节。 People will argue that there are reasons why you might want to choose one over the other , but that is a different story. 人们会争辩说,你有可能想要选择一个而不是另一个 ,但这是一个不同的故事。

sizeof( firstName ) 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 firstName to a function degrades it into a pointer. 有趣的一点是sizeof将返回的内容,这是版本1的版本1和32的char指针(取决于您的系统和编译器)的大小。请记住,另一个用户提到的内容,将firstName传递给函数会将其降级为一个指针。

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(可能)来存储地址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM