当我试图执行我的程序时,它得到这样的错误-
sendip: malloc.c:4631: _int_malloc: Assertion `(unsigned long)(size)
>= (unsigned long)(nb)' failed
尝试通过valgrind捕获错误,得到这个-
HEAP SUMMARY:
==3335== in use at exit: 24 bytes in 2 blocks
==3335== total heap usage: 111 allocs, 109 frees, 7,929 bytes allocated
==3335==
==3335== 4 bytes in 1 blocks are definitely lost in loss record 1 of 2
==3335== at 0x40268A4: malloc (vg_replace_malloc.c:236)
==3335== by 0x8049EEF: main (sendip.c:435)
==3335==
==3335== 20 bytes in 1 blocks are definitely lost in loss record 2 of 2
==3335== at 0x40268A4: malloc (vg_replace_malloc.c:236)
==3335== by 0x4031F57: ???
==3335== by 0x804A338: main (sendip.c:521)
==3335==
行不。435
datalen = stringargument(gnuoptarg, &datarg);
/*This is the line*/ data=(char *)malloc(datalen);
memcpy(data, datarg, datalen);
如果需要,我可以添加其他代码块…但帮我摆脱这个??
您一定是覆盖了"data"而忘记释放旧内容…你检查过这个了吗?
glibc的malloc()
将元数据(例如块的长度,指向前一个块的指针,…)添加到返回给调用者的块中,因此在覆盖"数据"时,很容易损坏元数据。