Segmentation Error

Asked

Viewed 125 times

-1

Hello. Could anyone help me with this error (Segmentation fault)? I don’t know why it’s happening.

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

typedef struct cel {
    char *palavra;
    int numOcorrencias;
} celula;



int charValido(char c) {
    if (c != '\0' && c != ',' && c != '.' && c != '?' && c != '/' && c != '\\' && c != '<' && c != '>' && c != '!' && c != '=' && c != '-' && c != '|' && c != '[' && c != ']' && c != '(' && c != ')' && c != '{' && c != '}' && c != 39 && c != '"' && c != '`' && c != '~' && c != '@' && c != '#' && c != '$' && c != '^' && c != '&' && c != '*' && c != ';' && c != ':')
        return 1;
    return 0;
}


celula *montaDicVet(char *nomeArq, int *tam) {
    FILE *arquivo;
    celula *vetor;
    char *texto, *p, c;
    int i, j, num;

    i = j = num = 0;
    texto = p = NULL;
    vetor = NULL;

    arquivo = fopen(nomeArq, "r");

    while (1) {
        c = fgetc(arquivo);

        if (c == EOF) {
            texto = realloc(texto, ++i);
            texto[i-1] = '\0';
            break;
        }
        if (charValido(c)) {
            i++;
            texto = realloc(texto, i);
            texto[i-1] = c;
        }
    }

    fclose(arquivo);

    i = 0;

    while (texto[i] != '\0')
        printf("%c", texto[i++]);

    return vetor;
}


int main(int numargs, char **args) {
    celula *vetor;
    int *tam = 0;

    vetor = montaDicVet(args[1], tam);

    printf("%d", *tam);
    return 0;
}

Execute as follows: ./testEP4 textoPequeno.txt

The command line argument refers to the txt file with the following content:


*The Polytechnic School of the University of São Paulo (Poli/USP) has more than a century of history, forming generations of engineers who have excelled not only in their professional specialties, but also in the political life of the country and in the administration of companies and public agencies. Founded in 1893, the so-called Polytechnic School of São Paulo was incorporated into USP in 1934; today it is a national reference and considered the most complete faculty of Engineering in Latin America. Poli occupies nine buildings in the University City, in São Paulo, totaling 141,500 square meters of built area. There they work or study 457 professors, 478 employees, 4,500 undergraduate students and 2,500 graduate students. The School is organized in 15 departments, responsible for teaching, research and extension of services to the community.

At graduation, 17 courses are offered, grouped into four major engineering areas: Civil, Electrical, Mechanical and Chemical. Of these courses, 15 are semesters and two - Computer Engineering and Chemical Engineering - have characteristics that differentiate them from the others: they are organized in four-month periods and carried out in cooperation with companies. In graduate school, Poli offers ten master’s, nine doctoral and one master’s degree programs. From 1970 to 2006, about 7,000 degrees were awarded, between master’s and doctorate, which places the School as one of the largest postgraduate centers in the country and the largest in the area of Engineering. Poli also excels in carrying out scientific and technological research, with which it contributes to the social and economic progress of the country and to the modernization, competitiveness and quality of the products and processes of the companies.*


  • The code is very confusing, more complex than it should be and has things completely meaningless, for example vetor is not used in fact, there celula should not exist. Why not make it simpler, then probably the problem will disappear alone. You may need all this, but in the current code you’re not using.

1 answer

0

The code is very inefficient (one realloc to each character read ? wtf ?).
Disregarding this, the cause of segmentation error is incorrect use of pointer.

int main(int numargs, char **args)
{
  celula *vetor;
  // int *tam = 0;  // <----
  int tam = 0;

  // vetor = montaDicVet(args[1], tam); // <----
  vetor = montaDicVet(args[1], &tam);

  // printf("%d", *tam); // <-----
  printf("%d", tam);

  return 0;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.