realloc 错误:已释放对象的校验和不正确
我尝试编写从标准输入读取数据的代码:
size_t bufSize = 1024;
unsigned char *msg = NULL;
size_t msgBytes = 0;
size_t inputMsgBufCount = 0;
unsigned char inputBuffer[bufSize];
size_t bytesRead = 0;
unsigned char *tmp = NULL;
if ((msg = (unsigned char *)malloc(sizeof(unsigned char) * bufSize)) == NULL)
exit(EXIT_FAILURE);
bytesRead = fread(msg, sizeof(unsigned char) * bufSize, 1, stdin);
inputMsgBufCount++;
while (bytesRead) {
printf("iteration: %lu\n", inputMsgBufCount);
if ( (tmp = (unsigned char *)realloc(msg, (inputMsgBufCount * bufSize) + bufSize)) != NULL ) {
printf("reallocated\n");
msg = tmp;
inputMsgBufCount++;
}
else {
printf("Ran out of memory\n");
free(msg);
}
bytesRead = fread(inputBuffer, sizeof(unsigned char) * bufSize, 1, stdin);
memmove((msg + (inputMsgBufCount * bufSize)), inputBuffer, bufSize);
}
free(msg);
msgBytes = (inputMsgBufCount * bufSize);
gettimeofday(&end, NULL);
printf("%10.6lf [MB/s]\n", (msgBytes / (1<<20)) / ( (end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec) * 1.0e-6f ));
但是在像这样运行之后: ~# dd if=/dev/zero bs=1024 count=8 | ./测试 我有这个错误:
iteration: 1 reallocated iteration: 2 reallocated iteration: 3 reallocated iteration: 4 reallocated iteration: 5 reallocated iteration: 6 reallocated iteration: 7 test(11450) malloc: *** error for object 0x100804008: incorrect checksum for freed object - object was probably modified after being freed. *** set a breakpoint in malloc_error_break to debug Abort trap
任何人都可以帮助我吗?
I try to wrote code that read data from stdin:
size_t bufSize = 1024;
unsigned char *msg = NULL;
size_t msgBytes = 0;
size_t inputMsgBufCount = 0;
unsigned char inputBuffer[bufSize];
size_t bytesRead = 0;
unsigned char *tmp = NULL;
if ((msg = (unsigned char *)malloc(sizeof(unsigned char) * bufSize)) == NULL)
exit(EXIT_FAILURE);
bytesRead = fread(msg, sizeof(unsigned char) * bufSize, 1, stdin);
inputMsgBufCount++;
while (bytesRead) {
printf("iteration: %lu\n", inputMsgBufCount);
if ( (tmp = (unsigned char *)realloc(msg, (inputMsgBufCount * bufSize) + bufSize)) != NULL ) {
printf("reallocated\n");
msg = tmp;
inputMsgBufCount++;
}
else {
printf("Ran out of memory\n");
free(msg);
}
bytesRead = fread(inputBuffer, sizeof(unsigned char) * bufSize, 1, stdin);
memmove((msg + (inputMsgBufCount * bufSize)), inputBuffer, bufSize);
}
free(msg);
msgBytes = (inputMsgBufCount * bufSize);
gettimeofday(&end, NULL);
printf("%10.6lf [MB/s]\n", (msgBytes / (1<<20)) / ( (end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec) * 1.0e-6f ));
But after run it like this:
~# dd if=/dev/zero bs=1024 count=8 | ./test
I have this error:
iteration: 1 reallocated iteration: 2 reallocated iteration: 3 reallocated iteration: 4 reallocated iteration: 5 reallocated iteration: 6 reallocated iteration: 7 test(11450) malloc: *** error for object 0x100804008: incorrect checksum for freed object - object was probably modified after being freed. *** set a breakpoint in malloc_error_break to debug Abort trap
Can anyone help me please.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
inputMsgBufCount
应该在您将数据复制到消息缓冲区后增加。您可以在第一次读取时正确执行此操作,但对于其他所有内容,您可以在重新分配后立即增加它。如果您遵循它的值,则在进入循环时它是 1。您重新分配到2*bufSize
,并增加inputMsgBufCount
,使其变为 2。然后读取数据,并将其复制到msg+2*bufSize
代码>.这会破坏你的缓冲区。您应该已复制到msg+bufSize
。只需延迟增加变量,直到复制数据之后。另一方面,您可以安全地使用 memcpy() 来复制数据。
msg
和inputBuffer
永远不会重叠。实际上,您可能应该完全摆脱inputBuffer
,并在正确的偏移处直接读入msg
。inputMsgBufCount
is supposed to be incremented after you copy the data in your msg buffer. You do it right on your first read, but for everything else, you increment it immediately after you realloc. If you follow the values for it, it is 1 as you enter the loop. You reallocate to2*bufSize
, and incrementinputMsgBufCount
, which makes it 2. Then you read the data, and copy it tomsg+2*bufSize
. This is corrupting your buffer. You should have copied tomsg+bufSize
. Simply delay incrementing the variable until after you copy your data.On another note, you can safely use
memcpy()
to copy data.msg
andinputBuffer
will never overlap. Actually you should probably get rid ofinputBuffer
altogether, and read directly intomsg
at the right offset.