Utilisation d'une valeur non initialisée de taille 8

Solved
rosate999 Posted messages 5 Status Member -  
fiddy Posted messages 441 Registration date   Status Contributor Last intervention   -
Hello,
Here is my code, I don't know why it gives a segmentation fault!!!

#include <stdio.h>
#include <stdlib.h>

typedef struct {
int i;
int j;
double c;
}point;
typedef struct {
point *a;
}tableau_t;

int myfunc( char *file){
int i, i1,i2, ind1, ind2;
double c,val;
double calculation;
point **a;
FILE *distanceMatrix;
distanceMatrix = fopen("file.txt", "r");

for(i=0;i<6;i++) {
fscanf(distanceMatrix,"%d %d %lf", &i1,&i2, &val);
((*a)[i]).i=i1;
((*a)[i]).j=i2;
((*a)[i]).c=val;
printf("%d %d %lf \n", i1, i2, ((*a)[i]).c);

}
tableau_t *tableau;
calculation=0;
a=tableau->a;
for(i=0;i<6;i++){
ind1=a[i]->i;
ind2=a[i]->j;
c=a[i]->c;
calculation=calculation+(*a)[i].c;

}
printf("%lf", calculation);
}

int main(int argc,char **argv){
char *distanceMatrix, tab;
tab=myfunc(distanceMatrix);
return 0;
}

the segmentation fault is as follows: Use of uninitialized value of size 8

I hope someone can help me.
Thank you in advance

rosate999

3 answers

  1. ElementW Posted messages 5690 Status Contributor 1 293
     
    Hello, by the way: use the
    <code>
    tag to put code in your messages, it keeps the indentation and gives you extra colors.

    To start, the first mistake I see is that you're trying to access an element of
    a
    without allocating memory, so
    a
    is a null pointer, leading to a guaranteed crash.
    Since your loop processes a maximum of 6 lines, you need to allocate 6 pointers to points:
    ... matriceDistance = fopen("fichier.txt", "r"); a = calloc(sizeof(point *), 6); ...
    Now
    a
    can hold 6 pointers to the
    point
    structure.

    Secondly, your way of accessing the elements of your struct in the array is quite strange, especially since you access it correctly later:
    ((*a)[i]).i=i1; // WTF o_O? /* Becomes */ a[i]->i=i1; // Now that's neat
    But once again, you're accessing a
    NULL
    pointer, you need to allocate memory for your point:
    ... fscanf(matriceDistance,"%d %d %lf", &i1,&i2, &val); a[i] = malloc(sizeof(point)); ...


    The last issue is that I don't see the purpose of your
    tableau_t *tableau;
    since you're not using it, and you do this
    a=tableau->a;
    and that's not cool: you're going to lose your array of points, without freeing the memory either... So I commented out the 2 lines mentioned, and it works.
    --
    from human import idiocy
    del idiocy
    2
    1. fiddy Posted messages 441 Registration date   Status Contributor Last intervention   1 847
       
      The allocation of "a" is not correct concerning its code. It's the opposite ;-).
      0
  2. fiddy Posted messages 441 Registration date   Status Contributor Last intervention   1 847
     
    Hello,

    In addition to gravgun's remarks,

    point **a;
    a is a double pointer. Given how you are using it, you should do:
     a=malloc(sizeof(point *)); *a=malloc(6*sizeof(point)); 

    Or declare "a" as point *a;
    And don't forget the necessary free() calls.

    printf("%d %d %lf \n", i1, i2, ((*a)[i]).c);
    printf("%lf", calcul);
    Rather %f (instead of %lf).
    And finally, don't forget to add a '\n' in the last printf() to ensure the output, or flush(stdout); before the return 0; in main.

    Google is your friend
    0
    1. ElementW Posted messages 5690 Status Contributor 1 293
       
      'lut fiddy, in fact my approach is different from yours: you make
      a
      a pointer to another unique pointer in a contiguous memory area of 6
      points
      , whereas mine was a pointer to an array of different pointers to
      points
      (non-contiguous distribution), which means I changed the way it accesses the array; but it's true that after observation,
      ((*a)[i]).i
      works with your code.
      As for the rest, well spotted ;)
      0
    2. fiddy Posted messages 441 Registration date   Status Contributor Last intervention   1 847
       
      My bad, I didn't notice that you had also changed the way he accesses the board.
      0
  3. rosate999 Posted messages 5 Status Member
     
    Thank you for your responses.
    0