我遇到编译“Core dumped”的问题,我不明白为什么。
我有一个 vector 的结构,我必须将两个 vector 加在一起。如果有人可以向我解释为什么这不起作用,我将不胜感激!
谢谢你!
typedef struct __vec
{
double x, y;
} vec;
vec *new_vec(double x, double y) {
vec* vector;
vector->x = x;
vector->y = y;
return vector;
}
vec *add_vec(const vec *const v1, const vec *const v2) {
vec* vector_result;
vector_result->x = v1->x;
vector_result->y = v2->y;
return vector_result;
}
int main(int argc, char *argv[]) {
vec* vec1;
vec* vec2;
vec1->x = 2.0;
vec1->y = 3.0;
vec2->x = 4.0;
vec2->y = 7.0;
vec* vector = add_vec(vec1,vec2);
printf("%f%f", vector->x, vector->y);
}
最佳答案
How works pointer on function in C
如果将它们设置为有效地址,它们就可以很好地工作,但您的程序中并非如此。
在
vec *new_vec(double x, double y) { vec* vector; vector->x = x; vector->y = y; return vector; }
您错过了分配 vector , vector 未设置,但您取消引用它,未定义的行为(通常可能是崩溃)
做
vec* vector = malloc(sizeof(vec));
同样的问题
vec *add_vec(const vec *const v1, const vec *const v2) { vec* vector_result; vector_result->x = v1->x; vector_result->y = v2->y; return vector_result; }
做
vec* vector_result = malloc(sizeof(vec));
在main中又出现同样的问题:
int main(int argc, char *argv[]) { vec* vec1; vec* vec2; vec1->x = 2.0; vec1->y = 3.0; vec2->x = 4.0; vec2->y = 7.0; vec* vector = add_vec(vec1, vec2);
也许你想做
vec * vec1 = new_vec(2.0, 3.0);
vec * vec2 = new_vec(4.0, 7.0);
vec * vector = add_vec(vec1, vec2);
但您也可以不分配vec1和vec2,因为它们是临时的,仅用于初始化 vector :
int main(int argc, char *argv[]) {
vec vec1;
vec vec2;
vec1.x = 2.0;
vec1.y = 3.0;
vec2.x = 4.0;
vec2.y = 7.0;
vec* vector = add_vec(&vec1,&vec2);
...
请注意,您的代码永远不会释放分配的 block ,为此,还要修改 printf 以获得更具可读性的结果并删除无用的参数:
int main(void) {
vec vec1;
vec vec2;
vec1.x = 2.0;
vec1.y = 3.0;
vec2.x = 4.0;
vec2.y = 7.0;
vec* vector = add_vec(&vec1,&vec2);
printf("%f %f\n", vector->x, vector->y);
free(vector);
}
编译与执行:
pi@raspberrypi:/tmp $ gcc -pedantic -Wall -Wextra -g v.c
pi@raspberrypi:/tmp $ ./a.out
2.000000 7.000000
在valgrind下执行:
pi@raspberrypi:/tmp $ valgrind ./a.out
==3154== Memcheck, a memory error detector
==3154== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==3154== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==3154== Command: ./a.out
==3154==
2.000000 7.000000
==3154==
==3154== HEAP SUMMARY:
==3154== in use at exit: 0 bytes in 0 blocks
==3154== total heap usage: 2 allocs, 2 frees, 1,040 bytes allocated
==3154==
==3154== All heap blocks were freed -- no leaks are possible
==3154==
==3154== For counts of detected and suppressed errors, rerun with: -v
==3154== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 3)
pi@raspberrypi:/tmp $
或者分配vec1和vec2:
int main(void) {
vec * vec1 = new_vec(2.0, 3.0);
vec * vec2 = new_vec(4.0, 7.0);
vec* vector = add_vec(vec1, vec2);
// vec1 and vec2 useless from here
free(vec1);
free(vec2);
printf("%f %f\n", vector->x, vector->y);
free(vector);
}
编译和执行:
pi@raspberrypi:/tmp $ gcc -pedantic -Wall -Wextra -g v.c
pi@raspberrypi:/tmp $ ./a.out
2.000000 7.000000
pi@raspberrypi:/tmp $
在valgrind下执行:
pi@raspberrypi:/tmp $ valgrind ./a.out
==3191== Memcheck, a memory error detector
==3191== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==3191== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==3191== Command: ./a.out
==3191==
2.000000 7.000000
==3191==
==3191== HEAP SUMMARY:
==3191== in use at exit: 0 bytes in 0 blocks
==3191== total heap usage: 4 allocs, 4 frees, 1,072 bytes allocated
==3191==
==3191== All heap blocks were freed -- no leaks are possible
==3191==
==3191== For counts of detected and suppressed errors, rerun with: -v
==3191== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 3)
pi@raspberrypi:/tmp $
关于c - C 中函数指针如何工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56459296/