malloc堆分配问题,返回重叠地址

时间:2013-04-02 07:43:48

标签: c++ memory-management malloc heap

我有一个现有的代码正在运行,我不得不添加一个额外的类。我尝试使用new和malloc,程序崩溃了。我已经通过分配8196字节来缩小问题范围。除此之外,我可以分配而不会崩溃。如果在第二个malloc之后调用第一个malloc,它可以正常工作并获得一个超出范围的地址

代码段:

DBGTRACE(DBG_ERR,"\n Before Alloting ISSU resrc \n");
print_status();
UINT32 * buf =  (UINT32 *) ssiMalloc(sizeof(IssuResource));
DBGTRACE(DBG_ERR, "\n buf in chopperCrd: %x , size: %d\n",buf,sizeof(IssuResource));
DBGTRACE(DBG_ERR, "\n After Alloting ISSU resrc \n");
print_status();
free (buf);


mallopt(MALLOC_STATS, (int)&stat);
print_status1(&stat);

UINT32 size = CHOP_MAX_PATH_NUM * sizeof(Buffer<pathElement>);
DBGTRACE(DBG_ERR, "Allocating Buffer of size %d for paths\n", size);
mallopt(MALLOC_VERIFY,1);
void *buf = ssiMalloc(size);
memset(buf, 0, size);
mallopt(MALLOC_STATS, (int)&stat);
print_status1(&stat);

以上信息的输出 我添加了一些mallopt和mallinfo来检查问题。 当我分配8k字节时,mallinfo之前和之后如下:

Jan 01 00:00:17 nto chop_cema_app: Enable interactive mode in chop_cema_app

Jan 01 00:00:17 nto chop_cema_app:   Before Alloting ISSU resrc

Jan 01 00:00:17 nto chop_cema_app: Total non-mmapped bytes (arena):       32768

Jan 01 00:00:17 nto chop_cema_app: # of free chunks (ordblks):            11

Jan 01 00:00:17 nto chop_cema_app: Total allocated space (uordblks):      6880

Jan 01 00:00:18 nto chop_cema_app: Total free space (fordblks):           25784

Jan 01 00:00:18 nto chop_cema_app: Topmost releasable block (keepcost):   0

Jan 01 00:00:18 nto chop_cema_app:   **buf in chopperCrd: 4872a570** , size: 8196

Jan 01 00:00:18 nto chop_cema_app:   After Alloting ISSU resrc

Jan 01 00:00:18 nto chop_cema_app: Total non-mmapped bytes (arena):       32768

Jan 01 00:00:18 nto chop_cema_app: # of free chunks (ordblks):            12

Jan 01 00:00:18 nto chop_cema_app: Total allocated space (uordblks):      15080

Jan 01 00:00:18 nto chop_cema_app: Total free space (fordblks):           17576

Jan 01 00:00:18 nto chop_cema_app: Topmost releasable block (keepcost):   0

粗体地址是Malloc返回的值。  现在,还有一个名为(之前已经存在过)的malloc,它分配了巨大的空间。第一种情况下malloc返回的地址在该范围内 - 这就是崩溃的原因。

分配前后的malloc-Stats:

Jan 01 00:00:46 nto chop_cema_app:   memory in free small blocks 8632 , memory in free big blocks 111224

Jan 01 00:00:46 nto chop_cema_app:   space in small blocks in use 7168 , space in big blocks in use 248784

Jan 01 00:00:46 nto chop_cema_app:   size of the arena 360448


Jan 01 00:00:46 nto chop_cema_app: Allocating Buffer of size 38109696 for paths

Jan 01 00:00:47 nto chop_cema_app:   memory in free small blocks 8632 , memory in free big blocks 143448

Jan 01 00:00:47 nto chop_cema_app:   space in small blocks in use 7168 , space in big blocks in use 38358480

Jan 01 00:00:47 nto chop_cema_app:   size of the arena 38502400

Jan 01 00:00:47 nto chop_cema_app:   Line Mgr[0] Address: 0x48722d98

Jan 01 00:00:47 nto chop_cema_app: **SonetPathMgr 0 at address 0x48722d98**

谢谢,

0 个答案:

没有答案