Matrix in dynamic allocation with execution failure

Asked

Viewed 71 times

0

Hello!

The following code compiled, but shows error in execution:

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

typedef struct 
{
    char *nome;
    int valor;
    int peso;
} objeto;

objeto obj[] = {
    {"map",      10,   2},
    {"compass",   7,   1},
    {"water",    25,   6},
    {"tin",      24,   5},
};

 // max{ f(Xn-1, W-Pn) + Vn , f(Xn-1, W) }    

int solver(objeto *obj, int n, int capac )
{
    int i, j, taDentro, taFora;
    int **matriz = (int **) calloc(n+1, sizeof(int *));

    matriz[0] = (int *) calloc(capac+1, sizeof(int)); 
    for (i=1; i<=n; i++) 
    {
        matriz[i] = (int *) calloc(capac+1, sizeof(int));

        for (j=1; j<=capac; j++)
        {
            taFora = matriz[i-1][j]; //coloca o item anterior?
            if (obj[i-1].peso > matriz[i][j]) matriz[i][j] = taFora;
            // -1 pq os itens em 'matriz' comecam em 1 
            else
            {
              taDentro = matriz[i-1][ j-obj[i].peso ] + obj[i].valor;
              if (taDentro > taFora) matriz[i][j] = taDentro;
              else                   matriz[i][j] = taFora;
            }
        }
    }
    int resultado;
    resultado = matriz[i][j];

    for (i = 0; i < n; i++) free(matriz[i]);
    free(matriz);
    return resultado;
}

int main()
{
    int n = 3;
    int capac = 7;
    int resultado;
    resultado = solver(obj, n, capac);
    printf("%d\n", resultado);
}

The windows window shows this information:

Problem Event Name: APPCRASH

I don’t know if I’m trying to record or read information in wrong space, or if I’m using the pointers wrong. Or both.

Gentlemen, could you help me?

  • The payback is an integer?

2 answers

1


I recommend that you use GDB to debug your program if you cannot solve the problem.

According to the debugger, segmentation failure occurs on the following line:

resultado = matriz[i][j];

Values of variables after segmentation failure:

i = 4

j = 8

Capac = 7

n = 3

What we can see is that:

The following tie:

for (i=1; i<=n; i++) 

The variable i starts at 1, the loop rotates while i is less than or equal to 3 (value of n), so far so good, but note the condition i<=n, this condition will only be false when i is greater than n, that is, when it has the value 4.

In the following line: int **matriz = (int **) calloc(n+1, sizeof(int *)); you reserve n+1 spaces for your array, in case 3+1 = 4, as vector addressing starts at 0, legal positions for your vector would be between 0 and 3 (4 positions), to correct this failure, just decrement the value of i because your vector does not have line 4.

Note that the variable j also suffers from this same problem.

You have two options:

int resultado;
resultado = matriz[--i][--j]; // --i é diferente de i--

Or

i--;
j--;
int resultado;
resultado = matriz[i][j];

I recommend you read:

GDB Tutorial (gnu Debugger)

What is segmentation failure

  • Thank you very much! I fixed the bugs and the program now works. The tips on GBD and what is segmentation failure were very well received. It’s my second question here at Stackoverflow, and I’m really impressed with the attention your members pay.

  • 1

    Don’t forget to check the value of the variable after the loop, this problem was very masked, everything seemed to work perfectly. To avoid confusion, prefer ties with the sign of "lower" and "higher" or "different" rather than "lower and equal" or "greater and equal". the Stackoverflow Community is excellent, for this reason I ask you not to forget to mark a question as correct, this way other people can benefit from the answer.

0

I believe the problem lies on this line:

resultado = matriz[i][j];

For right now the i will be n+1 and the j will be capac+1, which are unallocated positions.

  • Thank you very much, I managed to solve the problem.

Browser other questions tagged

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