realloc():在为char上的strcat腾出空间时,下一个无效的下一个大小* [英] realloc(): invalid next size when reallocating to make space for strcat on char *

查看:136
本文介绍了realloc():在为char上的strcat腾出空间时,下一个无效的下一个大小*的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在以下代码上收到无效的内存错误:

I am getting invalid memory error on following code:

printf(" %s\n","FINE 5");
printf("%s LENGTH IS: %d\n","FINE 6",strlen(": "));
buffer = (char *)realloc(buffer, strlen(buffer)* sizeof(char) + (strlen(": ")+1)* sizeof(char));
printf(" %s\n","FINE 7");
strcat(buffer, ": \0");

输出:

精细5
精细6长度是:2
*检测到glibc * ./auto:realloc():无效的下一个大小:0x08cd72e0 *** =======回溯:=========/lib/tls/i686/cmov/libc.so.6(+0x6b591)[0x6dd591]

FINE 5
FINE 6 LENGTH IS: 2
* glibc detected * ./auto: realloc(): invalid next size: 0x08cd72e0 *** ======= Backtrace: ========= /lib/tls/i686/cmov/libc.so.6(+0x6b591)[0x6dd591]

这里要注意的一点是Fine 7从未打印过.并且每次运行的下一个尺寸错误均位于同一位置.

The point to note here is Fine 7 is never printed. and invalid next size error on every run is at the same location.

找到了与此相关

推荐答案

发生此错误是因为代码的其他部分损坏了堆.在看不到其余代码的情况下,我们无法告诉您该错误是什么.

This error occurs because some other part of your code has corrupted the heap. We can't tell you what that error is without seeing the rest of the code.

未打印FINE 7的事实表明您realloc出现故障.并且该失败必须是因为buffer由于执行早期的堆损坏而无效.

The fact that FINE 7 is not printed tells you that realloc is failing. And that failure must be because buffer is invalid due to a heap corruption earlier in the execution.

与您的实际问题正交,根据定义,sizeof(char)1,因此有必要将其从代码中删除.

Orthogonal to your actual problem, sizeof(char) is 1 by definition so it makes sense to remove it from the code.

这篇关于realloc():在为char上的strcat腾出空间时,下一个无效的下一个大小*的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆