C: valgrind 显示结构中双指针枚举的无效写入
C: valgrind shows invalid write for double pointer enum in struct
在我的 test.c
中,我有:
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
enum MyEnum{
H1 = '#',
H2 = '$'
};
typedef struct {
enum MyEnum** myenum;
int myint;
char *s;
} MyStruct;
MyStruct* somefunction(const char* fp){
FILE* file = fopen(fp, "r");
if(!file){
fprintf(stderr, "Error %s not found", fp);
exit(-1);
}
char line[100] = {0};
int i = 0;
fgets(line, 100, file);
sscanf(line, "%i", &i);
MyStruct* strct = (MyStruct *) malloc(sizeof(strct));
strct->myint = i;
printf("%i\n", strct->myint);
fclose(file);
free(strct);
return strct;
}
int main(int argc, char** argv){
char* fp = argv[1];
somefunction(fp);
bool run = true;
while(run){
char entry = fgetc(stdin);
switch(entry){
case 'q' :{
run = false;
break;
}
}
}
}
我的 test.txt
包含:
1234
5678
运行 Valgrind
给予:
1234
q
==2030==
==2030== HEAP SUMMARY:
==2030== HEAP SUMMARY:
==2030== in use at exit: 0 bytes in 0 blocks
==2030== total heap usage: 5 allocs, 5 frees, 6,704 bytes allocated
==2030==
==2030== All heap blocks were freed -- no leaks are possible
==2030==
==2030== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
==2030==
==2030== 1 errors in context 1 of 2:
==2030== Invalid read of size 4
==2030== at 0x108AA9: somefunction (in /root/SokobanTechDev/test)
==2030== by 0x108B03: main (in /root/SokobanTechDev/test)
==2030== Address 0x51de2f8 is 0 bytes after a block of size 8 alloc'd
==2030== at 0x4C2D0AF: malloc (vg_replace_malloc.c:381)
==2030== by 0x108A93: somefunction (in /root/SokobanTechDev/test)
==2030== by 0x108B03: main (in /root/SokobanTechDev/test)
==2030==
==2030==
==2030== 1 errors in context 2 of 2:
==2030== Invalid write of size 4
==2030== at 0x108AA2: somefunction (in /root/SokobanTechDev/test)
==2030== by 0x108B03: main (in /root/SokobanTechDev/test)
==2030== Address 0x51de2f8 is 0 bytes after a block of size 8 alloc'd
==2030== at 0x4C2D0AF: malloc (vg_replace_malloc.c:381)
==2030== by 0x108A93: somefunction (in /root/SokobanTechDev/test)
==2030== by 0x108B03: main (in /root/SokobanTechDev/test)
为什么 valgrind
在定义枚举 MyEnum
后给我一个 invalid write
?在我的结构中,如果我评论 enum MyEnum** myenum;
,那些上下文就会消失。我推测他正在为 H1
和 H2
分配 2 个指针(每个指针 8 个字节),但是 malloc
不应该处理这个问题吗?
我试图寻找类似 here and 的答案,但我似乎找不到任何与双指针相关的东西。
提前致谢
函数中至少有两个错误。
第一个是内存分配不正确
MyStruct* strct = (MyStruct *) malloc(sizeof(strct));
你需要写任何一个
MyStruct* strct = (MyStruct *) malloc(sizeof(*strct));
或
MyStruct* strct = (MyStruct *) malloc(sizeof(MyStruct));
并在调用 free 后返回具有无效值的指针
//...
free(strct);
return strct;
}
没有意义。
在我的 test.c
中,我有:
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
enum MyEnum{
H1 = '#',
H2 = '$'
};
typedef struct {
enum MyEnum** myenum;
int myint;
char *s;
} MyStruct;
MyStruct* somefunction(const char* fp){
FILE* file = fopen(fp, "r");
if(!file){
fprintf(stderr, "Error %s not found", fp);
exit(-1);
}
char line[100] = {0};
int i = 0;
fgets(line, 100, file);
sscanf(line, "%i", &i);
MyStruct* strct = (MyStruct *) malloc(sizeof(strct));
strct->myint = i;
printf("%i\n", strct->myint);
fclose(file);
free(strct);
return strct;
}
int main(int argc, char** argv){
char* fp = argv[1];
somefunction(fp);
bool run = true;
while(run){
char entry = fgetc(stdin);
switch(entry){
case 'q' :{
run = false;
break;
}
}
}
}
我的 test.txt
包含:
1234
5678
运行 Valgrind
给予:
1234
q
==2030==
==2030== HEAP SUMMARY:
==2030== HEAP SUMMARY:
==2030== in use at exit: 0 bytes in 0 blocks
==2030== total heap usage: 5 allocs, 5 frees, 6,704 bytes allocated
==2030==
==2030== All heap blocks were freed -- no leaks are possible
==2030==
==2030== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
==2030==
==2030== 1 errors in context 1 of 2:
==2030== Invalid read of size 4
==2030== at 0x108AA9: somefunction (in /root/SokobanTechDev/test)
==2030== by 0x108B03: main (in /root/SokobanTechDev/test)
==2030== Address 0x51de2f8 is 0 bytes after a block of size 8 alloc'd
==2030== at 0x4C2D0AF: malloc (vg_replace_malloc.c:381)
==2030== by 0x108A93: somefunction (in /root/SokobanTechDev/test)
==2030== by 0x108B03: main (in /root/SokobanTechDev/test)
==2030==
==2030==
==2030== 1 errors in context 2 of 2:
==2030== Invalid write of size 4
==2030== at 0x108AA2: somefunction (in /root/SokobanTechDev/test)
==2030== by 0x108B03: main (in /root/SokobanTechDev/test)
==2030== Address 0x51de2f8 is 0 bytes after a block of size 8 alloc'd
==2030== at 0x4C2D0AF: malloc (vg_replace_malloc.c:381)
==2030== by 0x108A93: somefunction (in /root/SokobanTechDev/test)
==2030== by 0x108B03: main (in /root/SokobanTechDev/test)
为什么 valgrind
在定义枚举 MyEnum
后给我一个 invalid write
?在我的结构中,如果我评论 enum MyEnum** myenum;
,那些上下文就会消失。我推测他正在为 H1
和 H2
分配 2 个指针(每个指针 8 个字节),但是 malloc
不应该处理这个问题吗?
我试图寻找类似 here and
提前致谢
函数中至少有两个错误。
第一个是内存分配不正确
MyStruct* strct = (MyStruct *) malloc(sizeof(strct));
你需要写任何一个
MyStruct* strct = (MyStruct *) malloc(sizeof(*strct));
或
MyStruct* strct = (MyStruct *) malloc(sizeof(MyStruct));
并在调用 free 后返回具有无效值的指针
//...
free(strct);
return strct;
}
没有意义。