我得到一个
malloc: *** error for object 0x1001012f8: incorrect checksum for freed object
- object was probably modified after being freed.
*** set a breakpoint in malloc_error_break to debug
以下函数中的错误:
char* substr(const char* source, const char* start, const char* end) {
char *path_start, *path_end, *path;
int path_len, needle_len = strlen(start);
path_start = strcasestr(source, start);
if (path_start != NULL) {
path_start += needle_len;
path_end = strcasestr(path_start, end);
path_len = path_end - path_start;
path = malloc(path_len + 1);
strncpy(path, path_start, path_len);
path[path_len] = '\0';
} else {
path = NULL;
}
return path;
}
我怎样才能使这项工作?当我重写函数来分配内存时,path[path_len + 1]
它工作得很好。
现在,我不明白的部分是,我什至从不调用free
我的应用程序的任何点,因为程序需要每个分配的内存,直到它存在(这,AFAIK 无论如何都会使每个分配的内存无效?!)
那么,如果我从不释放一个被释放的对象,它怎么会被破坏呢?
该函数在此调用:
char *read_response(int sock) {
int bytes_read;
char *buf = (char*)malloc(BUF_SIZE);
char *cur_position = buf;
while ((bytes_read = read(sock, cur_position, BUF_SIZE)) > 0) {
cur_position += bytes_read;
buf = realloc(buf, sizeof(buf) + BUF_SIZE);
}
int status = atoi(substr(buf, "HTTP/1.0 ", " "));
有realloc
,我用错了吗?我想读取完整的服务器响应,所以我必须在每次迭代后重新分配,不是吗?