内存中 char 指针的 C 字节
我想计算一个 char 指针在内存中占用了多少字节。 指针指向一个包含 100 个字符的字符串。
根据以下程序,一个字符需要 4 个字节,
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char b;
b = 'b';
printf("%p\n",&b);
system("pause");
return 0;
}
指针也一样吗? 那么对于 100 个字符的字符串,内存中保存了 400 个字节吗?
I would like to count how many bytes a char pointer takes in memory.
The pointer points to a string with 100 chars.
According to the following program a char needs 4 bytes
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char b;
b = 'b';
printf("%p\n",&b);
system("pause");
return 0;
}
Is it the same for pointers?
So it's 400 bytes saved in memory for a string of 100 chars?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
char
需要一个字节。指向任何内容的指针通常需要 4 或 8 个字节,具体取决于体系结构。当然,对象和指向它的指针是不同的东西,它们使用单独的内存,因此如果 100 个字符的字符串使用 100 或 101 个字节(取决于您如何计数以及如何存储字符串),则指向它的指针仍将使用 4 个字节。char
needs one byte. Pointer to anything usually needs 4 or 8 bytes depending on the architecture. Of course an object and a pointer to it are separate things and they use separate memory, so if a 100-char string uses 100 or 101 byte (depends on how do you count and how do you store the string), a pointer to it will still use 4 bytes.不,对于 100 个字符的字符串,您需要 100 个字节 + 1 个字节作为“\0”字符串终止符。
您的程序返回 4 字节整数,因为您正在打印指向 char 的指针的值。
Char 的大小为 1 字节,并且不依赖于体系结构。指针的大小取决于体系结构,通常,32 位架构为 4 字节,64 位架构为 8 字节。
No, for a string of 100 chars you need 100 bytes + 1 byte for '\0' string terminator.
Your program returns 4-byte integer because you are printing value of the pointer to char.
Char has a size of 1 byte and does not depend on architecture. Size of a pointer depends on architecture and, typically, 4-byte for 32 bit archs and 8-byte for 64 bit ones.
你的程序打印为 b 分配的空间的内存地址。它与保留的内存量无关。
要查看指针在内存中占用的内存量,可以使用 sizeof(void*)。该值取决于系统,我猜在你的系统中它是4(32位)
但是你无法获得指针指向的已分配内存量(例如通过malloc)。在一般情况下,您需要在分配期间存储它。在某些情况下,例如通过 strdup() 进行字符串复制,它将等于字符串的长度 + 1(对于结尾的“\0”字符。
Your program prints the memory address of the space allocated for b. It's not related to the amount of memory reserved.
To see the amount of memory a pointer takes in memory, you can use sizeof(void*). The value depends on the system, I guess in yours it's 4 (32 bits)
However you cannot get the amount of allocated memory a pointer points at (e.g. through malloc). In the general case you need to store this during allocation. In some cases such as string duplication through strdup(), it will be equal to the length of the string + 1 (for the ending '\0' character.
听起来好像您还没有清楚地了解指针与它们所指向的内容的概念 - 公平地说,这可能很难开始。
尝试重新表述一下,指针是保存其他元素的地址或位置的东西。根据您运行的系统,指针本身所需的内存量会有所不同,但通常为 4 或 8 字节。它与它所指向的内存使用是分开的,它可以是从零开始的任何值(如果它是空指针)向上。 (顺便说一句,没有标准方法可以找到指针指向的大小,但这是另一个主题。)
无论如何,如果您有一个 100 个字符的字符串,您会期望分配一个 101 个字符的缓冲区(如习惯问题,留出空间并添加终止 null,以防万一您确实获得了完整的 100 个字符),因此您的程序的内存使用量将按以下顺序排列105 或 109 字节的缓冲区和一个指向它的指针。
It sounds as if you've not got the concepts of pointers vs what they point to clear yet - which to be fair can be tricky to start.
To try and rephrase, a pointer is something that holds the address or location of some other element. Depending upon the system you're running on, the amount of memory need by a pointer itself will vary, but will typically be 4 or 8 bytes. That's separate from the memory use by what it points to, which could be anything from zero, if it's a null pointer, upwards. (By the way, there's no standard way to find the size of what a pointer's pointing to, but that's another topic.)
Anyway, if you had a string of 100 chars, you'd expect to allocate a buffer of 101 chars (as a matter of habit, allow space for and add a terminating null, just in case you do actually get the full 100 chars), so your program's memory usage would be in the order of 105 or 109 bytes for the buffer and a single pointer to it.
指向
char
类型的指针的大小为sizeof (int *)
字节。该值由实现定义,但在 32 位系统中通常为 4 字节,在 64 位系统中通常为 8 字节。要在系统上打印大小,您可以执行以下操作:
char
类型的大小始终为 1 个字节。字符串的大小为
sizeof the_string_array_object
字节。例如,字符串"Hello world!"
的大小为sizeof "Hello world!"
字节。它是字符串中包含尾随空字符的字符数。The size of the pointer to
char
type issizeof (int *)
bytes.The value is implementation defined but is usually 4 bytes in 32-bit system and 8 bytes in 64-bit system. To print the size on your system, you can do:
The size of the
char
type is always 1 byte.The size of a string is
sizeof the_string_array_object
bytes. For example, the size of the string"Hello world!"
issizeof "Hello world!"
bytes. It is the number of character in the string including the trailing null character.指针是指向内存位置的地址。一个 100 字节的字符串占用 100 个内存位置。因为它们是连续的,所以您可以使用单个指针引用该字符串,但它仍然占用 100 个内存位置。
如果这是一条街道,则该字符串类似于一排房屋,每个房屋都有一个单独的地址,但您可以通过街道名称来引用所有房屋;这类似于引用一串位置而不是单个位置的字符串名称或指针。然而,街道仍然占据相同的空间!
本例中的指针为 4 个字节,因为 32 位处理器使用 32 位地址,因此它可以寻址 232 个离散内存位置 (4Gb)。
A pointer is an address to a memory location. A 100 byte string occupies 100 memory locations. Because they are contiguous, you can reference the string with a single pointer, but it still occupies 100 memory locations.
If this were a street, the string is analogous to a row of houses, each one has a separate address, but you might refer to all of them by the street name; that is analogous to the string name or pointer referring to a string of locations rather than, an individual location. The street still occupies the same space however!
A pointer in this case is 4 bytes because a 32bit processor uses 32 bit addresses so it can address 232 discrete memory locations (4Gb).
通常 char 为 1 个字节,并由 C 和 C++ 标准保证。
Normally char is 1 byte and guaranteed by the C and C++ standards.
你是怎么想出的:
首先,您的编译器在编译时应该产生警告,您的 printf 需要一个
int
值,您传递一个char*
。使用sizeof()
确定任何类型的内存占用量。How do you come up with:
First of all your compiler should have produced warnings when compiling, your printf expects an
int
value, you pass achar*
. Usesizeof()
to determine the memory footprint of any type.