1
不正确的校验和我试着写了从标准输入读取数据代码: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如果= /开发/零BS = 1024 count = 8 | ./test 我有这样的错误:
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
谁能帮我请。
是的,你有权利,非常感谢。这是我的错,它太晚了,我有点累了:D。 – 2011-04-13 00:10:56