Execution error in c

Asked

Viewed 50 times

0

I have a problem with the following code:

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

char **tabela_hash;
int valorTotal=0;

void inicializarTabela(int *pm)
{
    for (int i = 0; i < (*pm); i++){
        tabela_hash[i] = malloc(50*sizeof(char));
    }
}
int acharPosicaoAlfabeto(char *c)
{
    int asc;
    asc = *c;
    return (asc-65);
}

int calculoHash(char *c, int *i, int *j)
{   
    int posicaoAlfabeto;
    int elementoEntrada;
    int posicaoElemento;
    int valor;
    posicaoAlfabeto = acharPosicaoAlfabeto(c);
    posicaoElemento = *i;
    elementoEntrada = *j;
    valor = elementoEntrada + posicaoAlfabeto + posicaoElemento; 
    valorTotal = valorTotal + valor;
    return valorTotal;
}

void lerString(int *pqtdLinhas)
{   
    int valorHash;
    for (int j=0; j < (*pqtdLinhas); j++){
        scanf("%50s",tabela_hash[j]);
        int i = 0;
        while (tabela_hash[j][i] != '\0' &&  i < 50){
            valorHash = calculoHash(&tabela_hash[j][i], &i, &j);
            i++;
        }
    }
    printf("%d\n",valorHash);
    valorTotal=0;
}

int main()
{
    int qtdTestes, *pqtdTestes = &qtdTestes;
    scanf("%d",pqtdTestes);
  
    int qtdLinhas, *pqtdLinhas = &qtdLinhas;
    scanf("%d",pqtdLinhas);

    tabela_hash = (char **) malloc ((*pqtdLinhas) * sizeof(char*));
    inicializarTabela(pqtdLinhas);
    lerString(pqtdLinhas);

    int i=1;
    while (*pqtdTestes > 1 && i < *pqtdTestes){
        scanf("%d",pqtdLinhas);
        tabela_hash = (char **) realloc(tabela_hash, (*pqtdLinhas) * sizeof(char*));
        inicializarTabela(pqtdLinhas);
        lerString(pqtdLinhas);
        i++;
    }
    return 0;
}

Quick explanation of what it does: I create a hash table matrix and each element of it is a pointer to a char vector, that is, a string. In each string, I need to read character by character to make a calculation of the value of each, and at the end I print the cumulative total value of the matrix. As there may be more than one test case, I reallocate space for table_hash and do all the operations again.

The question is this: I have tested this code in several IDE’s and it works. However, as it is part of an activity, I need to send it through the online judge Thehuxley, and there at the time of executing it gives an error "Error of execution". I have already reviewed all my code and I do not know where this error could be, although I suspect the functions malloc and realloc. If anyone can help me find him I’d be grateful.

Edit: Below, follows another version of the code working table_hash as an array, rather than matrix, as occurs in the first code. This second version also works, but the execution error it generates is exactly this: "realloc(): invalid next size". I need a solution to either version. But since this second code generates a specific error, it may be easier to find a solution.

Version 2:

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

char *tabela_hash;
int valorTotal = 0;


int acharPosicaoAlfabeto(char *c)
{
    int asc;
    asc = *c;
    return (asc-65);
}

int calculoHash(char *c, int *i, int *j)
{   
    int posicaoAlfabeto;
    int elementoEntrada;
    int posicaoElemento;
    int valor;
    posicaoAlfabeto = acharPosicaoAlfabeto(c);
    posicaoElemento = *i;
    elementoEntrada = *j;
    valor = elementoEntrada + posicaoAlfabeto + posicaoElemento; 
    valorTotal = valorTotal + valor;
    return valorTotal;
}

void lerString(int *pqtdLinhas)
{   
    int valor;
    for (int j=0; j < (*pqtdLinhas); j++){
        scanf("%51s",tabela_hash);
        int i = 0;
        while (tabela_hash[i] != '\0' &&  i < 50){
            valor = calculoHash(&tabela_hash[i], &i, &j);
            i++;
        }
    }
    printf("%d\n",valor);
    valorTotal=0;
}

int main()
{
    int qtdTestes, *pqtdTestes = &qtdTestes;
    scanf("%d",pqtdTestes);
  
    int qtdLinhas, *pqtdLinhas = &qtdLinhas;
    scanf("%d",pqtdLinhas);

    tabela_hash = (char *) malloc ((*pqtdLinhas) * sizeof(char));
    lerString(pqtdLinhas);

    int i=1;
    while (*pqtdTestes > 1 && i < *pqtdTestes){
        scanf("%d",pqtdLinhas);
        tabela_hash = (char *) realloc (tabela_hash, (*pqtdLinhas) * sizeof(char));
        lerString(pqtdLinhas);  
        i++;
    }
    return 0;
}

Input example:

5 -> corresponde a 5 casos de teste
2 -> corresponde a 2 linhas
CBA -> string de entrada da linha 1
DDD -> string de entrada da linha 2
1 -> corresponde a uma linha dos mesmos 5 casos de teste citado acima
Z -> string desta linha acima
6 -> corresponde a 6 linhas dos mesmos 5 casos de teste citado acima
A -> string da linha 1 destas 6 linhas citada aqui acima
B -> string da linha 2 destas 6 linhas citada aqui acima
C -> string da linha 3 destas 6 linhas citada aqui acima
D -> string da linha 4 destas 6 linhas citada aqui acima
E -> string da linha 5 destas 6 linhas citada aqui acima
F -> string da linha 6 destas 6 linhas citada aqui acima
6 -> mais 6 linhas abaixo:
ABCDEFGHIJKLMNOPQRSTUVWXYZ
ABCDEFGHIJKLMNOPQRSTUVWXYZ
ABCDEFGHIJKLMNOPQRSTUVWXYZ
ABCDEFGHIJKLMNOPQRSTUVWXYZ
ABCDEFGHIJKLMNOPQRSTUVWXYZ
ABCDEFGHIJKLMNOPQRSTUVWXYZ
1 -> uma linha abaixo:
ZZZZZZZZZZ

Totaling 5 test cases above.

The expected result is:

21
25
30
4290
295
  • Try using _malloca(size_t size) (https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/malloca?view=msvc-160). I hope it helps!

  • I tried here, but in thehuxley did not work because this _malloca function is from windows, correct? It does not recognize, even if I import windows. h and malloc. h

No answers

Browser other questions tagged

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