[英]Behind the scene allocation of memory on heap using malloc
if a process request 1-24 bytes on heap why 32 bytes difference ?如果进程在堆上请求 1-24 个字节,为什么会有 32 个字节的差异?
if a process request 25-40 bytes on heap why 48 bytes difference ?如果一个进程在堆上请求 25-40 个字节,为什么会有 48 个字节的差异?
if a process request 41-56 bytes on heap why 64 bytes difference ?如果一个进程在堆上请求 41-56 个字节,为什么 64 个字节的差异?
Initial 8bytes is used to hold the length of the allocated memory初始 8bytes 用于保存分配内存的长度
#include <stdio.h>
#include<stdlib.h>
int main()
{
int size=41;
char* c = (char*) malloc(size);//initial 8bytes used for length
char* d = (char*) malloc(size);
printf("a = %p\nb = %p\n difference is %d\n",c,d,d-c);
free(c);
free(d);
}
Usually the function malloc is implemented such a way that it returns a chunk of memory multiple by the paragraph size equal to 16 bytes (this value can be equal to the value of alignof(max_align_t)
).通常函数 malloc 是这样实现的,它返回一个内存块乘以段落大小等于 16 字节(这个值可以等于
alignof(max_align_t)
的值)。 That is the allocated memory must be aligned for any allocated object.也就是说,分配的内存必须与任何分配的对象对齐。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.