奇怪的malloc()!
时间:2010-08-23
来源:互联网
众所周知,malloc()是在堆空间分配内存的,麻烦大家来看一段代码:
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
-
- typedef struct student
- {
- char name[11];
- float score;
- struct student *node;
-
- }STUDENT;
-
- int main(int argc, char *argv[])
- {
-
- STUDENT *p1, *p2, *p3;
-
- printf("the size of STUDENT is %d\n",sizeof(STUDENT));
-
- p1 = (STUDENT *)malloc(sizeof(STUDENT));
- if (NULL == p1)
- {
- printf("malloc error");
- return 1;
- }
- printf("the address is %p\n",p1);
-
- p2 = (STUDENT *)malloc(sizeof(STUDENT));
- if (NULL == p2)
- {
- printf("malloc error");
- return 1;
- }
- printf("the address is %p\n",p2);
-
- p3 = (STUDENT *)malloc(sizeof(STUDENT));
- if (NULL == p3)
- {
- printf("malloc error");
- return 1;
- }
- printf("the address is %p\n",p3);
-
- free(p1);
- p1 = NULL;
- free(p2);
- p2= NULL;
- free(p3);
- p3 = NULL;
-
-
- return 0;
- }
the size of STUDENT is 20
the address is 0x804a008
the address is 0x804a020
the address is 0x804a038
请问:很明显,既然STUDENT的大小为20个字节,那为什么malloc()分配的空间会是24(0x804a020 - 0x804a008 = 0x804a038 - 0x804a020 = 24)个字节呢?
ps: 上述程序是在Linux下运行的!
谢谢!
作者: zhuqing_739 发布时间: 2010-08-23
作者: hellioncu 发布时间: 2010-08-23
作者: zhuqing_739 发布时间: 2010-08-23
我猜有二种可能。一种是malloc分配时是4K分页对齐的。第二种可能malloc在分配内存时加了些自已的信息在里面。
同楼主求甚解!
作者: zhangsuozhu 发布时间: 2010-08-23
作者: zhuqing_739 发布时间: 2010-08-23
zhuqing_739 发表于 2010-08-23 15:29
可能是按8字节对齐的
作者: hellioncu 发布时间: 2010-08-23
hellioncu 发表于 2010-08-23 15:38
不对啊!如果是按8个字节对齐的话,第一个printf()输出结果就不是20了!
作者: zhuqing_739 发布时间: 2010-08-23
zhuqing_739 发表于 2010-08-23 15:42
不是说你的结构,是说malloc分配的内存地址
作者: hellioncu 发布时间: 2010-08-23
作者: davelv 发布时间: 2010-08-23
hellioncu 发表于 2010-08-23 15:44
看下面这段改过的程序就知道不是按8对齐的了!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
-
- typedef struct student
- {
- char name[11];
- float score;
- struct student *node;
-
- }STUDENT;
-
- int main(int argc, char *argv[])
- {
-
- STUDENT *p1, *p2, *p3;
-
- printf("the size of STUDENT is %d\n",sizeof(STUDENT));
-
- p1 = (STUDENT *)malloc([size=5]4[/size]);
- if (NULL == p1)
- {
- printf("malloc error");
- return 1;
- }
- printf("the address is %p\n",p1);
-
- p2 = (STUDENT *)malloc([size=5]4[/size]);
- if (NULL == p2)
- {
- printf("malloc error");
- return 1;
- }
- printf("the address is %p\n",p2);
-
- p3 = (STUDENT *)malloc([size=5]4[/size]);
- if (NULL == p3)
- {
- printf("malloc error");
- return 1;
- }
- printf("the address is %p\n",p3);
-
- free(p1);
- p1 = NULL;
- free(p2);
- p2 = NULL;
- free(p3);
- p3 = NULL;
-
-
- return 0;
- }
the size of STUDENT is 20
the address is 0x804a008
the address is 0x804a018
the address is 0x804a028
作者: zhuqing_739 发布时间: 2010-08-23
overhead plus possibly more to obtain necessary alignment and/or
to obtain a size of at least MINSIZE, the smallest allocatable
size. Also, checked_request2size traps (returning 0) request sizes
that are so large that they wrap around zero when padded and
aligned.
作者: zhangsuozhu 发布时间: 2010-08-23
我一直想看一下,各个库函数是怎么编写的,在网上就是找不到!如果有,发到我的邮箱:
[email protected]
或者:
MSN:[email protected]
小弟谢谢了!
作者: zhuqing_739 发布时间: 2010-08-23
作者: zhangsuozhu 发布时间: 2010-08-23
作者: jnjn999 发布时间: 2010-08-23

作者: zhuqing_739 发布时间: 2010-08-23
Minimum overhead per allocated chunk: 4 or 8 bytes
Each malloced chunk has a hidden word of overhead holding size
and status information.
Minimum allocated size: 4-byte ptrs: 16 bytes (including 4 overhead)
8-byte ptrs: 24/32 bytes (including, 4/8 overhead)
When a chunk is freed, 12 (for 4byte ptrs) or 20 (for 8 byte
ptrs but 4 byte size) or 24 (for 8/8) additional bytes are
needed; 4 (8) for a trailing size field and 8 (16) bytes for
free list pointers. Thus, the minimum allocatable size is
16/24/32 bytes.
Even a request for zero bytes (i.e., malloc(0)) returns a
pointer to something of the minimum allocatable size.
另外附上内存空间图
- An allocated chunk looks like this:
-
-
- chunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
- | Size of previous chunk, if allocated | |
- +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
- | Size of chunk, in bytes |M|P|
- mem-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
- | User data starts here... .
- . .
- . (malloc_usable_size() bytes) .
- . |
- nextchunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
- | Size of chunk |
- +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
作者: davelv 发布时间: 2010-08-23
作者: pk8995 发布时间: 2010-08-23
作者: zhuqing_739 发布时间: 2010-08-23
ftp://ftp.gnu.org/gnu/glibc/
作者: davelv 发布时间: 2010-08-23
Alignment: 2 * sizeof(size_t) (default)
(i.e., 8 byte alignment with 4byte size_t). This suffices for
nearly all current machines and C compilers. However, you can
define MALLOC_ALIGNMENT to be wider than this if necessary.
作者: davelv 发布时间: 2010-08-23
作者: 短 发布时间: 2010-09-17
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28