Problem with Segmentation Fault

1

I am creating a program that simulates a minefield, and option number 1 should pick up a matrix (containing points (where it has no pump) and asterisks (where it has a bomb) from a computer file and copy it to a array that I'm dynamically allocating to the read function.

I'm having a segmentation fault problem and I can not detect the error, I'd be grateful for the help, here's the code below:

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

char** leitura(char* nome)//Declaracao de funcao que aloca a matriz
{
    FILE* ponteiro;
    int i = 0,j = 1, k ,l;
    char** campo = NULL;
    char carac;

    ponteiro = fopen("nome", "w");

    while(!feof(ponteiro))
    {
        i++;
        campo = (char**) realloc(campo, i * sizeof(char*));     

        while(fscanf(ponteiro, "%c", &carac) != '\n')
        {
            campo[j - 1] = (char*) realloc(campo[j - 1], j * sizeof(char));
            campo[i - 1][j - 1] = carac;
            j++;    
        }
    }

    for(k = 0; k < i; k++)
    {
        for(l = 0; l < j; l++)
        {
            printf("%c", campo[i][j]);
        }
        printf("\n");
    }

    fclose(ponteiro);

    return campo; //Retorno da matriz alocada dinamicamente
}

int main (void)
{
    int tamanho = 0, linha = 0, coluna = 0;
    char* nome = NULL;
    char** retornoCampo = NULL;
    size_t tam;
    getline(&nome, &tam, stdin); //Funcao para adquirir o nome do arquivo
    int i, j;

    nome[strlen(nome) - 1] = '
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char** leitura(char* nome)//Declaracao de funcao que aloca a matriz
{
    FILE* ponteiro;
    int i = 0,j = 1, k ,l;
    char** campo = NULL;
    char carac;

    ponteiro = fopen("nome", "w");

    while(!feof(ponteiro))
    {
        i++;
        campo = (char**) realloc(campo, i * sizeof(char*));     

        while(fscanf(ponteiro, "%c", &carac) != '\n')
        {
            campo[j - 1] = (char*) realloc(campo[j - 1], j * sizeof(char));
            campo[i - 1][j - 1] = carac;
            j++;    
        }
    }

    for(k = 0; k < i; k++)
    {
        for(l = 0; l < j; l++)
        {
            printf("%c", campo[i][j]);
        }
        printf("\n");
    }

    fclose(ponteiro);

    return campo; //Retorno da matriz alocada dinamicamente
}

int main (void)
{
    int tamanho = 0, linha = 0, coluna = 0;
    char* nome = NULL;
    char** retornoCampo = NULL;
    size_t tam;
    getline(&nome, &tam, stdin); //Funcao para adquirir o nome do arquivo
    int i, j;

    nome[strlen(nome) - 1] = '%pre%';

    int opcao = 0;

    scanf("%d", &opcao);

    switch(opcao) //Opcoes a serem escolhidas
    {
        case 1: //Leitura

            retornoCampo = leitura(nome);

            break;
        case 2: //Inicializacao do tabuleiro
            break;
        case 3: //Acao do usuario
            break;
    }

    return 0;
}
'; int opcao = 0; scanf("%d", &opcao); switch(opcao) //Opcoes a serem escolhidas { case 1: //Leitura retornoCampo = leitura(nome); break; case 2: //Inicializacao do tabuleiro break; case 3: //Acao do usuario break; } return 0; }
    
asked by anonymous 08.08.2015 / 22:23

1 answer

0

I see two problems:

The first is in the various calls to the realloc command: when the size of the memory region is increased, realloc performs the memory copy that was already allocated to the new memory region (if this is effectively a new memory region), but does not initialize the bytes that have been allocated any more.

So, in the line: campo = (char**) realloc(campo, i * sizeof(char*)); is allocated a memory region of size sizeof(char*) (when i == 0 ), and this region contains memory garbage, since you do not initialize it.

When the campo[j - 1] = (char*) realloc(campo[j - 1], j * sizeof(char)); call is executed, it attempts to relocate memory to a pointer that points to memory garbage, which can cause segmentation to fail.

Another problem occurs in the following sub-sequential iterations:

campo[j - 1] = (char*) realloc(campo[j - 1], j * sizeof(char));

To facilitate the explanation, let's ignore the first problem, and pretend that realloc will do the right service on the first problem raised.

When j == 1 , campo[j - 1] will be allocated correctly with the size sizeof(char) .

On the next line, even when j == 1 , you assign to [0][0] by using the command: campo[i - 1][j - 1] = carac; , and then increments j .

In the second iteration ( i == 1 , j == 2 ), realloc is done on the campo[1] element, because j - 1 == 1 . But notice that the number of positions of the campo variable is still 1, so only the 0 index is valid, again causing segmentation fault. That is, you are resizing the wrong position of the campo variable. To solve this, just change j by i in the line of this realloc, getting:

campo[i - 1] = (char*) realloc(campo[i - 1], j * sizeof(char));

I emphasize that these two problems occur simultaneously and must be corrected. Try to show realloc , it is a very expensive command to execute.

There may be other problems, but these were the ones I noticed most easily.

I hope I have helped, and the explanation has not been too confusing.

    
09.08.2015 / 01:06