我不明白为什么我肯定会失去记忆块
I don't understand why I'm definitely losing blocks of memory
作为一个项目,我正在创建一个学生数据库。但是根据 valgrind 的说法,我的程序中存在内存泄漏,我不知道为什么。
我真的不能说更多:我不明白为什么记忆肯定会丢失。
学生结构:
typedef struct {
char student_number[7];
char *first_name;
char *last_name;
int round_points[6];
} Student;
免责声明:我使用的是 gcc 选项 -std=c99
,因此我必须实现自己的 strdup()
。
重要代码:
char *copy_string(const char *string) {
int len = strlen(string);
char *copy = calloc(len + 1, sizeof(char));
if (copy == NULL)
return NULL;
strcpy(copy, string);
/* copy[len] = '[=11=]'; */
return copy;
}
char **parse_one_line_params(const char *one_line, int param_count) {
char *copy = copy_string(one_line);
if (copy == NULL)
return NULL;
//copy_start is used to free the copy string in the end
char *copy_start = copy;
//It is assumed that one_line is of the form COMMAND|SPACE|ARGUMENTS
//Move pointer to the first important character
copy += 2;
const char *separator = " ";
char **content = malloc(sizeof(char *) * param_count);
if (content == NULL)
return NULL;
int occurrences = 0;
char *delimiter_start;
while ((delimiter_start = strstr(copy, separator)) != NULL) {
delimiter_start[0] = '[=11=]';
char *sub_string = copy_string(copy);
if (sub_string == NULL)
return NULL;
if (sub_string[0] != '[=11=]') {
content[occurrences] = sub_string;
}
//Since separator is of the length of one
copy = delimiter_start + 1;
occurrences++;
}
//param n - 1 will be assigned from the last portion of copy
if (occurrences != param_count - 1)
return NULL;
int last_len = strlen(copy);
if (last_len > 0 && copy[last_len - 1] == '\n')
copy[last_len - 1] = '[=11=]';
content[occurrences] = copy_string(copy);
free(copy_start);
return content;
}
char **deliver_payload(const char *one_line, int param_count) {
if (one_line[1] != ' ') {
printf("Command was longer than one character.\nPlease see manual for instructions\n");
return NULL;
}
char **payload = parse_one_line_params(one_line, param_count);
if (payload == NULL) {
printf("Invalid arguments for given command\n");
return NULL;
}
return payload;
}
有问题的错误是:
(记录1~5同下)
==15== 32 bytes in 4 blocks are definitely lost in loss record 5 of 7
==15== at 0x4C2FB55: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==15== by 0x400ECA: copy_string (projekti.c:80)
==15== by 0x400F81: parse_one_line_params (projekti.c:106)
==15== by 0x4010B6: deliver_payload (projekti.c:133)
==15== by 0x4019A2: main (projekti.c:301)
第 301 行只是:
char **payload = deliver_payload(one_line, 3);
代码的编辑版本:
char **parse_one_line_params(const char *one_line, int param_count) {
char *copy = copy_string(one_line);
if (copy == NULL)
return NULL;
//copy_start is used to free the copy string in the end
char *copy_start = copy;
//It is assumed that one_line is of the form COMMAND SPACE ARGUMENTS
//Move pointer to the first important character
copy += 2;
const char *separator = " ";
char **content = malloc(sizeof(char *) * param_count);
if (content == NULL) {
free(copy_start);
return NULL;
}
int occurrences = 0;
char *delimiter_start;
while ((delimiter_start = strstr(copy, separator)) != NULL) {
delimiter_start[0] = '[=14=]';
char *sub_string = copy_string(copy);
if (sub_string == NULL) {
for (int i = 0; i < occurrences; i++) {
free(content[i]);
}
free(copy_start);
return NULL;
}
if (sub_string[0] != '[=14=]') {
int sub_len = strlen(sub_string);
content[occurrences] = calloc(sub_len + 1, sizeof(char));
strncpy(content[occurrences], sub_string, sub_len);
free(sub_string);
}
//Since separator is of the length of one
copy = delimiter_start + 1;
occurrences++;
}
//param n - 1 will be assigned from the last portion of copy
if (occurrences != param_count - 1) {
printf("Too few parameters\nAborting\n");
if (occurrences > 0) {
for (int i = 0; i < occurrences; i++) {
free(content[i]);
}
}
free(content);
return NULL;
}
int last_len = strlen(copy);
if (last_len > 0 && copy[last_len - 1] == '\n')
copy[last_len - 1] = '[=14=]';
content[occurrences] = calloc(last_len + 1, sizeof(char));
strncpy(content[occurrences], copy, last_len);
/* content[occurrences] = copy_string(copy); */
free(copy_start);
return content;
}
编辑 valgrind:
(在完整输出中,有问题的行是 140 和 118,即 callocs)
==15== 13 bytes in 2 blocks are definitely lost in loss record 3 of 7
==15== at 0x4C2FB55: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==15== by 0x4011B0: parse_one_line_params (projekti.c:140)
==15== by 0x401236: deliver_payload (projekti.c:153)
==15== by 0x401B22: main (projekti.c:321)
==15==
==15== 27 bytes in 6 blocks are definitely lost in loss record 4 of 7
==15== at 0x4C2FB55: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==15== by 0x401075: parse_one_line_params (projekti.c:118)
==15== by 0x401236: deliver_payload (projekti.c:153)
==15== by 0x401BBE: main (projekti.c:337)
最后编辑:我解决了这个特殊问题:我忘记释放主函数中的参数列表,所以最后我找错了树。
你这里有内存泄漏
char **parse_one_line_params(const char *one_line, int param_count){
char *copy = copy_string(one_line);
if (copy == NULL)
return NULL;
/* ... */
char **content = malloc(sizeof(char *) * param_count);
if (content == NULL)
return NULL;
如果 malloc
return 的 content
为 NULL,则您 return 从函数中退出而不释放 copy
的内存。 malloc
极不可能失败,但如果您的其余代码以相同的模式编写,那么这就是泄漏的来源。
"balance" 每个 malloc
/strdup
/calloc
和 a free
.您必须跟踪分配内存的位置并释放它,以防您访问它的最后一个指针超出范围或被覆盖(感谢@Ctx)。
最后故障出在main函数,我忘记释放新创建的参数列表。感谢@GSerg 和@datenwolf 指出我代码中的其他错误。
作为一个项目,我正在创建一个学生数据库。但是根据 valgrind 的说法,我的程序中存在内存泄漏,我不知道为什么。 我真的不能说更多:我不明白为什么记忆肯定会丢失。
学生结构:
typedef struct {
char student_number[7];
char *first_name;
char *last_name;
int round_points[6];
} Student;
免责声明:我使用的是 gcc 选项 -std=c99
,因此我必须实现自己的 strdup()
。
重要代码:
char *copy_string(const char *string) {
int len = strlen(string);
char *copy = calloc(len + 1, sizeof(char));
if (copy == NULL)
return NULL;
strcpy(copy, string);
/* copy[len] = '[=11=]'; */
return copy;
}
char **parse_one_line_params(const char *one_line, int param_count) {
char *copy = copy_string(one_line);
if (copy == NULL)
return NULL;
//copy_start is used to free the copy string in the end
char *copy_start = copy;
//It is assumed that one_line is of the form COMMAND|SPACE|ARGUMENTS
//Move pointer to the first important character
copy += 2;
const char *separator = " ";
char **content = malloc(sizeof(char *) * param_count);
if (content == NULL)
return NULL;
int occurrences = 0;
char *delimiter_start;
while ((delimiter_start = strstr(copy, separator)) != NULL) {
delimiter_start[0] = '[=11=]';
char *sub_string = copy_string(copy);
if (sub_string == NULL)
return NULL;
if (sub_string[0] != '[=11=]') {
content[occurrences] = sub_string;
}
//Since separator is of the length of one
copy = delimiter_start + 1;
occurrences++;
}
//param n - 1 will be assigned from the last portion of copy
if (occurrences != param_count - 1)
return NULL;
int last_len = strlen(copy);
if (last_len > 0 && copy[last_len - 1] == '\n')
copy[last_len - 1] = '[=11=]';
content[occurrences] = copy_string(copy);
free(copy_start);
return content;
}
char **deliver_payload(const char *one_line, int param_count) {
if (one_line[1] != ' ') {
printf("Command was longer than one character.\nPlease see manual for instructions\n");
return NULL;
}
char **payload = parse_one_line_params(one_line, param_count);
if (payload == NULL) {
printf("Invalid arguments for given command\n");
return NULL;
}
return payload;
}
有问题的错误是: (记录1~5同下)
==15== 32 bytes in 4 blocks are definitely lost in loss record 5 of 7
==15== at 0x4C2FB55: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==15== by 0x400ECA: copy_string (projekti.c:80)
==15== by 0x400F81: parse_one_line_params (projekti.c:106)
==15== by 0x4010B6: deliver_payload (projekti.c:133)
==15== by 0x4019A2: main (projekti.c:301)
第 301 行只是:
char **payload = deliver_payload(one_line, 3);
代码的编辑版本:
char **parse_one_line_params(const char *one_line, int param_count) {
char *copy = copy_string(one_line);
if (copy == NULL)
return NULL;
//copy_start is used to free the copy string in the end
char *copy_start = copy;
//It is assumed that one_line is of the form COMMAND SPACE ARGUMENTS
//Move pointer to the first important character
copy += 2;
const char *separator = " ";
char **content = malloc(sizeof(char *) * param_count);
if (content == NULL) {
free(copy_start);
return NULL;
}
int occurrences = 0;
char *delimiter_start;
while ((delimiter_start = strstr(copy, separator)) != NULL) {
delimiter_start[0] = '[=14=]';
char *sub_string = copy_string(copy);
if (sub_string == NULL) {
for (int i = 0; i < occurrences; i++) {
free(content[i]);
}
free(copy_start);
return NULL;
}
if (sub_string[0] != '[=14=]') {
int sub_len = strlen(sub_string);
content[occurrences] = calloc(sub_len + 1, sizeof(char));
strncpy(content[occurrences], sub_string, sub_len);
free(sub_string);
}
//Since separator is of the length of one
copy = delimiter_start + 1;
occurrences++;
}
//param n - 1 will be assigned from the last portion of copy
if (occurrences != param_count - 1) {
printf("Too few parameters\nAborting\n");
if (occurrences > 0) {
for (int i = 0; i < occurrences; i++) {
free(content[i]);
}
}
free(content);
return NULL;
}
int last_len = strlen(copy);
if (last_len > 0 && copy[last_len - 1] == '\n')
copy[last_len - 1] = '[=14=]';
content[occurrences] = calloc(last_len + 1, sizeof(char));
strncpy(content[occurrences], copy, last_len);
/* content[occurrences] = copy_string(copy); */
free(copy_start);
return content;
}
编辑 valgrind: (在完整输出中,有问题的行是 140 和 118,即 callocs)
==15== 13 bytes in 2 blocks are definitely lost in loss record 3 of 7
==15== at 0x4C2FB55: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==15== by 0x4011B0: parse_one_line_params (projekti.c:140)
==15== by 0x401236: deliver_payload (projekti.c:153)
==15== by 0x401B22: main (projekti.c:321)
==15==
==15== 27 bytes in 6 blocks are definitely lost in loss record 4 of 7
==15== at 0x4C2FB55: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==15== by 0x401075: parse_one_line_params (projekti.c:118)
==15== by 0x401236: deliver_payload (projekti.c:153)
==15== by 0x401BBE: main (projekti.c:337)
最后编辑:我解决了这个特殊问题:我忘记释放主函数中的参数列表,所以最后我找错了树。
你这里有内存泄漏
char **parse_one_line_params(const char *one_line, int param_count){ char *copy = copy_string(one_line); if (copy == NULL) return NULL; /* ... */ char **content = malloc(sizeof(char *) * param_count); if (content == NULL) return NULL;
如果 malloc
return 的 content
为 NULL,则您 return 从函数中退出而不释放 copy
的内存。 malloc
极不可能失败,但如果您的其余代码以相同的模式编写,那么这就是泄漏的来源。
"balance" 每个 malloc
/strdup
/calloc
和 a free
.您必须跟踪分配内存的位置并释放它,以防您访问它的最后一个指针超出范围或被覆盖(感谢@Ctx)。
最后故障出在main函数,我忘记释放新创建的参数列表。感谢@GSerg 和@datenwolf 指出我代码中的其他错误。