为什么分配的内存不同于字符串的大小?(Why does allocated memory is different than the size of the string?)

编程入门 行业动态 更新时间:2024-10-28 06:34:44
为什么分配的内存不同于字符串的大小?(Why does allocated memory is different than the size of the string?)

请考虑以下代码:

char **ptr; str = malloc(sizeof(char *) * 3); // Allocates enough memory for 3 char pointers str[0] = malloc(sizeof(char) * 24); str[1] = malloc(sizeof(char) * 25); str[2] = malloc(sizeof(char) * 25);

当我使用一些printf打印每个指针的内存地址时:

printf("str[0] = '%p'\nstr[1] = '%p'\nstr[2] = '%p'\n", str[0], str[1], str[2]);

我得到这个输出:

str[0] = '0x1254030' str[1] = '0x1254050' str[2] = '0x1254080'

我期望对应于第二个地址的数字是对应于第一个地址的数字和24的对数,它对应于字符串str[0]的字节大小(因为char的大小为1个字节) 。 我预计对应于第二个地址的数字是0x1254047 ,考虑到这个数字用16(0123456789abcdef)表示。

在我看来,我发现了一个模式:从字符串中的24个字符开始,对于其中包含的每16个字符,使用的内存大16个字节。 例如,45个字符长的字符串使用64个字节的内存,77个字符长的字符串使用80个字节的内存,150个字符长的字符串使用160个字节的内存。

以下是模式的说明:

我想了解为什么分配的内存不等于字符串的大小。 为什么会遵循这种模式?

Please consider the following code:

char **ptr; str = malloc(sizeof(char *) * 3); // Allocates enough memory for 3 char pointers str[0] = malloc(sizeof(char) * 24); str[1] = malloc(sizeof(char) * 25); str[2] = malloc(sizeof(char) * 25);

When I use some printf to print the memory adresses of each pointer:

printf("str[0] = '%p'\nstr[1] = '%p'\nstr[2] = '%p'\n", str[0], str[1], str[2]);

I get this output:

str[0] = '0x1254030' str[1] = '0x1254050' str[2] = '0x1254080'

I expected the number corresponding to the second adress to be the sum of the number corresponding to the first one, and 24, which corresponds to the size of the string str[0] in bytes (since a char has a size of 1 byte). I expected the number corresponding to the second adress to be 0x1254047, considering that this number is expressed in base 16 (0123456789abcdef).

It seems to me that I spotted a pattern: from 24 characters in a string, for every 16 more characters contained in it, the memory used is 16 bytes larger. For example, a 45 characters long string uses 64 bytes of memory, a 77 characters long string uses 80 bytes of memory, and a 150 characters long string uses 160 bytes of memory.

Here is an illustration of the pattern:

I would like to understand why the memory allocated isn't equal to the size of the string. Why does it follow this pattern?

最满意答案

三个原因:

(1)字符串“ABC”包含4个字符,因为C中的每个字符串都必须有一个终止'\ 0'。

(2)许多处理器都有内存地址对齐问题,当malloc()分配的任何块在4或8的倍数或任何“自然”内存大小的地址上启动时,它会最有效。

(3)malloc()函数本身需要一些内存来存储有关已分配内容的信息,以便free()知道该怎么做。

Three reasons:

(1) The string "ABC" contains 4 characters, because every string in C has to have a terminating '\0'.

(2) Many processors have memory address alignment issues that make it most efficient when any block allocated by malloc() starts on an address that is a multiple of 4, or 8, or whatever the "natural" memory size is.

(3) The malloc() function itself requires some memory to store information about what has been allocated where, so that free() knows what to do.

更多推荐

本文发布于:2023-04-28 08:40:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1331375.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不同于   字符串   分配   大小   内存

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!