ฉันมีรหัสเช่นนี้
void find_groupings ()
int *bandwidths;
int *execution_time;
bandwidths = (int *)malloc(sizeof(int)*node_count); // node_count is glbl
execution_time = (int *)malloc(sizeof(int)*node_count);
//other mallocs, other code etc
while (condition) {
// lot of code
find_bandwidths(bandwidths);
find_execution_time(execution_time);
//lot of code
}
free(bandwidths);
free(execution_time);
}
รหัส segfaults ที่บรรทัด "ฟรี (execution_time);"
Thread 1 "vx_tutorial_exe" received signal SIGSEGV, Segmentation fault.
0xf7dd0cd9 in _int_free (av=0xf7f15780 <main_arena>, p=<optimized out>, have_lock=0) at malloc.c:4005
4005 malloc.c: No such file or directory.
ฉันสามารถรับประกันได้ว่า "execution_time" จะไม่อยู่นอกขอบเขตภายใน find_execution_time() ฉันเชื่อว่าฉันปลดปล่อย malloc ทุกตัวที่ฉันทำในโค้ด
นอกจากนี้ยังพบว่าค่าตัวชี้สำหรับExecution_timeเหมือนกันก่อนที่จะเกิดปัญหาที่ free() โดยใช้ gdb
ลองใช้ valgrind แต่ก็ไม่ได้ช่วยอะไรเนื่องจากโปรแกรมมีข้อผิดพลาด
อาจเกิดปัญหาอะไรที่นี่
other code
บิต หรือบางที mallocs ของคุณอาจล้มเหลว อย่างน้อยก็ทดสอบค่าว่าง - person pm100   schedule 20.09.2018