码迷,mamicode.com
首页 > 其他好文 > 详细

malloc中 heap block 的 blocksize 大小问题

时间:2014-07-27 23:38:29      阅读:452      评论:0      收藏:0      [点我收藏+]

标签:c语言   malloc   heap   alignment   内存   

heap block 引发的思考

问题背景:


Implicit Free Lists


                  Any practical allocator needs some data structure that allows it to distinguish block boundaries and to distinguish between allocated and free blocks. Most allocators embed this information in the blocks themselves. One simple approach is shown in Figure 9.35.


bubuko.com,布布扣



malloc函数第一次申请出的是payload区域顶端的内存区域,返回的指针指向该处。


在payload之前还有一个header的区域,这个区域记录了block size,这里作者有点误导,由于是8byte align

于是在只有最底位(图中的a,用来记录block是否已经allocated还是free)


 首先要理解malloc中block size的原理

bubuko.com,布布扣

这个问题要能搞定,填对,不然下面的demo看了没用。。。

这是在32bit的机器上

对于上面那个题目的题解:

                  This problem touches on some core ideas such as alignment requirements, minimum block sizes, and header encodings. The general approach for determining the block size is to round the sum of the requested payload and the header size to the nearest multiple of the alignment requirement (in this case 8 bytes). For
example, the block size for the malloc(1) request is 4 + 1 = 5 rounded up to 8. The block size for the malloc(13)request is 13 + 4 = 17 rounded up to 24.

Request        Block size (decimal bytes) Block header (hex)
malloc(1)                   8                                          0x9
malloc(5)                  16                                         0x11
malloc(12)                16                                         0x11
malloc(13)                24                                         0x19




64bit的机器的malloc采用的是16byte对齐的!


在linux 64bit的 Ubuntu上做测试:

 


bubuko.com,布布扣


malloc(42)

这里申请 42byte的空间,malloc返回的连续内存空间是 64byte的


42 byte,由于开始有8byte的block size 区域,

42+8 = 50;

由于16byte对齐,于是对齐到64byte


至于最后的temp == 65 ,那是因为最后一位是用来提示该内存区域是否allocated。由于该bit 位等于1,于是,是allocated



上述测试代码:

 

/***********************************************************
code writer : EOF
code date : 2014.07.27
e-mail:jasonleaster@gmail.com

code purpose:
	Find out what beyond the payload location. :)

************************************************************/
#include <stdio.h>
#include <stdlib.h>

#define MACHINE_ADDRESS_LENGTH 64

void print_dec2bin(int dec_number)
//Just a simple function which translate decimal number into binary numebr
{
	int temp = 0;
	
	int to_be_print = 0;
	
	int array[MACHINE_ADDRESS_LENGTH];
	
	for(temp = 0;temp <  MACHINE_ADDRESS_LENGTH; temp++)
	{
		array[MACHINE_ADDRESS_LENGTH-temp-1] = dec_number%2;
		dec_number >>=1;
	}	

	for(temp = 0;temp <  MACHINE_ADDRESS_LENGTH; temp++)
	{
		printf("%d",array[temp]);
	}	

	printf("\n");
}

int main()
{
	int *ptr = NULL;
	
	int temp = 42;//how many bytes to be allocated

	printf("byte to be allocated, temp : %d\n",temp);

	ptr = (int *)malloc(temp);

	if(ptr == NULL)
	{
		printf("malloc failed\n");
		return 0;
	}
	else
	{
		*ptr = 2014;//just write some data into payload location.
	}

	temp = *(ptr - 2);//You may never forget that this code must be run on 64-bits machine, and ptr point to 'int'!!!Attention!!
			// otherwise you have to change 'ptr-2' into 'ptr-1'
	
	print_dec2bin(temp);
	
	printf("temp : %d\n",temp);

	free(ptr);
	return 0;
}


再三提示那个ptr-2!



bubuko.com,布布扣

malloc中 heap block 的 blocksize 大小问题,布布扣,bubuko.com

malloc中 heap block 的 blocksize 大小问题

标签:c语言   malloc   heap   alignment   内存   

原文地址:http://blog.csdn.net/cinmyheart/article/details/38174421

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!