C

时间:2019-06-05 11:09:10

标签: c pointers vector

我在编译“ Core dumped”时遇到问题,我不明白为什么。

我有一个向量的结构,我必须将两个向量加在一起。如果有人可以解释我为什么不起作用,我将不胜感激!

谢谢!

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);

}

2 个答案:

答案 0 :(得分:3)

  

C语言中的函数指针如何工作

如果将它们设置为有效地址,它们可以很好地工作,但程序中不是这种情况。

vec *new_vec(double x, double y) {

   vec* vector;

   vector->x = x;

   vector->y = y;

   return 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);
    ...

请注意,您的代码永远不会释放已分配的块,要这样做,还要修改 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 $ 

答案 1 :(得分:0)

vec* vec1;您没有为其分配内存,因此您无法取消引用指针

vec* vec1 = malloc(sizeof(*vec1));