-1

I have a problem of compilation "Core dumped" and I don't understand why.

I have a struct for a vector and i have to add two vector together. If someone can explain me why that don't work, i will appreciate the help!

Thanks you !

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

}

snaaketv
  • 13
  • 2
  • 1
    Unrelated to your problem, but regarding that symbol `__vec`, please see [this old answer](https://stackoverflow.com/a/1449301/440558). – Some programmer dude Jun 05 '19 at 11:11
  • 1
    As for your problem, you have many pointer variables (like for example `vec1` and `vec2` in the `main` function). But *where do these pointers really point?* I really recommend you get a few text-books to read and learn about pointers. – Some programmer dude Jun 05 '19 at 11:12
  • 1
    Third question today with this problem... what's going on, did people stop studying programming books entirely? – Lundin Jun 05 '19 at 11:19
  • @Lundin how to know ? ^^ – bruno Jun 05 '19 at 11:23
  • There are several problems in your code, see my answer – bruno Jun 05 '19 at 11:23

2 Answers2

3

How works pointer on function in C

they work well if they are set to a valid address, this is not the case in your program.

In

vec *new_vec(double x, double y) {

   vec* vector;

   vector->x = x;

   vector->y = y;

   return vector;

}

you missed to allocate the vector, vector is not set but you dereference it, the behavior in undefined (typically can be a crash)

do

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

Same problem in

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

do

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

And again same problem in 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);

probably you wanted to do

vec * vec1 = new_vec(2.0, 3.0);
vec * vec2 = new_vec(4.0, 7.0);
vec * vector = add_vec(vec1, vec2);

but you can also do not allocate the vec1 and vec2 because they are temporary and just used to initialize 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);
    ...

Note your code never free the allocated blocks, to do that and also modifying the printf for a more readable result and removing the useless parameters :

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

Compilation and execution:

pi@raspberrypi:/tmp $ gcc -pedantic -Wall -Wextra -g v.c
pi@raspberrypi:/tmp $ ./a.out
2.000000 7.000000

Execution under 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 $ 

Or allocating vec1 and 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);
}

Compilation and execution :

pi@raspberrypi:/tmp $ gcc -pedantic -Wall -Wextra -g v.c
pi@raspberrypi:/tmp $ ./a.out
2.000000 7.000000
pi@raspberrypi:/tmp $ 

Execution under 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 $ 
bruno
  • 32,421
  • 7
  • 25
  • 37
  • 1
    Thank's you very much, my bad, I understand my mistake and thanks for valgrind, i didn't know this ! – snaaketv Jun 05 '19 at 13:57
  • @snaaketv you welcome, yes _valgrind_ is a fantastic tool, use it even when you think your program works well – bruno Jun 05 '19 at 14:01
0

vec* vec1; you do not allocatememory for it so you cant dereference the pointer

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

0___________
  • 60,014
  • 4
  • 34
  • 74