-4

I have to read a file in C and create an int**.

This is the file:

2
-1,1,1,0,0,1
1,-1,0,1,0

I'm doing this:

FILE *fp = fopen("grafo.txt", "r");
char line[100];
int numLinea = 0;
char** tokens;

while (1) {
    if (fgets(line,150, fp) == NULL) break;
    if(numLinea == 0){
        NUMERO_NODOS = atoi( line );
        nodos = (int **)malloc (NUMERO_NODOS*sizeof(int *));
    }else{
        tokens = str_split(line, ',');
        if (tokens) {
            for (int i = 0; *(tokens + i); i++) {
                char* contactoNodo;
                strcpy(contactoNodo, *(tokens + i));
                int numNodo = numLinea-1;
                nodos[numNodo] = (int *) malloc (NUMERO_NODOS*sizeof(int));
                nodos[numNodo][i] = atoi(contactoNodo);
                printf("nodos[%i][%i] = %i\n",numNodo,i,nodos[numNodo][i]);
                printf("nodos[0][0] = %i\n",nodos[0][0]);
                //free(contactoNodo);
            }
            printf("nodos[0][0] = %i\n",nodos[0][0]);
            //free(tokens);
        }
    }
    numLinea++;
    //printf("%3d: %s", i, line);
}

And this is the output:

nodos[0][0] = -1
nodos[0][0] = -1
nodos[0][1] = 1
nodos[0][0] = -1163005939
(...)

Why is nodos[0][0] = -1163005939 in the second iteration of the for loop?


SOLUTION

LOL, it was that:

if(i==0){
      nodos[numNodo] = (int *) malloc (NUMERO_NODOS*sizeof(int));
}

I can't believe I didn't see it. Thanks MikeCAT!!!

Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
Victor Company
  • 1,177
  • 1
  • 9
  • 9

1 Answers1

0

Fatal errors:

  • You invoked undefined behavior by using value of uninitialized variable contactoNodo having automatic storage duration, which is indeteminate.
  • You threw away what is read in the first iteration by allocating new buffer and overwriting the pointer to old buffer by it, and invoked undefined behavior again by reading contents of buffer allocated via malloc and not initialized.

Warnings:

Try this:

FILE *fp = fopen("grafo.txt", "r");
char line[100];
int numLinea = 0;
char** tokens;

while (1) {
    /* use correct buffer size to avoid buffer overrun */
    if (fgets(line,sizeof(line), fp) == NULL) break;
    if(numLinea == 0){
        NUMERO_NODOS = atoi( line );
        /* remove cast of what is returned from malloc() */
        nodos = malloc (NUMERO_NODOS*sizeof(int *));
    }else{
        tokens = str_split(line, ',');
        if (tokens) {
            for (int i = 0; *(tokens + i); i++) {
                char contactoNodo[100]; /* allocate buffer statically */
                strcpy(contactoNodo, *(tokens + i));
                int numNodo = numLinea-1;
                if (i == 0) { /* allocate buffer in only the first iteration */
                    /* remove cast of what is returned from malloc() */
                    nodos[numNodo] = malloc (NUMERO_NODOS*sizeof(int));
                }
                nodos[numNodo][i] = atoi(contactoNodo);
                printf("nodos[%i][%i] = %i\n",numNodo,i,nodos[numNodo][i]);
                printf("nodos[0][0] = %i\n",nodos[0][0]);
                /* do not free() what is not allocated via memory management functions such as malloc() */
            }
            printf("nodos[0][0] = %i\n",nodos[0][0]);
            //free(tokens);
        }
    }
    numLinea++;
    //printf("%3d: %s", i, line);
}
Community
  • 1
  • 1
MikeCAT
  • 73,922
  • 11
  • 45
  • 70