0

I have a segmentation fault and would like to know where is my mistake.

Let me explain.

In my main, I declare a 3D array: int*** Matricegroupegeneralisant

Then this main uses a function recuperationinfoFich(&matricegroupegeneralisant); This function is declared as : recuperationinfoFich(int* * * * matricegroupegeneralisant)

This function recuperationinfoFich uses another function recuperationmatricegroupesgeneralisants(matricegroupegeneralisantA[Ni]); This function is declared as recuperationmatricegroupesgeneralisants( int*** matricegroupegeneralisant)

My code:

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

void allocationdynamiquetableautroisdimdentier(int**** Matrice,int nbniveau, int nbligne, int nbcolonne)
{
int i,j;
    *Matrice=(int***) malloc (sizeof(int**)*nbniveau);
    for (i=0; i<nbniveau; i++)
    {
        (*(Matrice))[i]=(int**) malloc (sizeof(int*)*nbligne);  // allocation dynamique de la matrice Matrice
        for (j=0; j<nbligne; j++)
        {
            ((*(Matrice))[i])[j]=(int*) malloc (sizeof(int)*nbcolonne);
        } 
    }
}


void recuperationmatricegroupesgeneralisants(int*** matricegroupegeneralisantA)
{
    (*matricegroupegeneralisantA)[0][1]=1;
}

void recuperationinfoFich(int**** matricegroupegeneralisantA)
{
    allocationdynamiquetableautroisdimdentier(matricegroupegeneralisantA,3, 3, 7);
    recuperationmatricegroupesgeneralisants(matricegroupegeneralisantA[1]);
}



void main(int args, char **argv)
{

    int*** matricegroupegeneralisantA;

    recuperationinfoFich(&matricegroupegeneralisantA);
}

With Gdb :

(gdb) r
Starting program: /home/larimsna1/Desktop/a.out 

Breakpoint 1, 0x000000000040061a in main ()
(gdb) n
Single stepping until exit from function main,
which has no line number information.

Program received signal SIGSEGV, Segmentation fault.
0x00000000004005c8 in recuperationmatricegroupesgeneralisants ()
(gdb) 
6
  • What have you done to debug? Have you find the segfaulting place? I would use gdb or likewise to locate the segfaulting code Commented Jul 30, 2012 at 18:07
  • The first thing you should do when getting a segmenetation fault, or any other type of crash, is to use the debugger. It will help you locate where the crash happens, and also let you examine variables to see what might have caused it. Commented Jul 30, 2012 at 18:08
  • Also, while calling recuperationinfoFich and allocationdynamiquetableautroisdimdentier properly, you do not call recuperationmatricegroupesgeneralisants properly. Or using the passed argument properly in recuperationmatricegroupesgeneralisants. Commented Jul 30, 2012 at 18:11
  • 1
    protip: rather than using sizeof(int**), sizeof(int*), sizeof(int), you can use sizeof(**Matrice), sizeof(***Matrice), sizeof(****Matrice). If you ever change the type to be something other than an int, you won't have to change the code. Commented Jul 30, 2012 at 18:22
  • Another tip, gdb is going to be much much more helpful if you compile with the flag -ggdb (assuming you're using gcc). Commented Jul 30, 2012 at 18:54

1 Answer 1

1

I suspect the problem has something to do with your allocation function. That being said, there are numerous functional and stylistic issues with the code you provided. In one place, you failed to dereference a pointer enough times, causing you to assign an integer to a pointer type. You needlessly use pointers for most of your transactions when you could simply not pass an argument and use a return value.

This code should do what you want, and should work properly, and is easier to read:

// allocation, returns pointer to allocated value
int *** allocationdynamique(int nbniveau, int nbligne, int nbcolonne)
{
    int *** Matrice;
    int i, j;
    Matrice = (int ***) malloc (sizeof(*Matrice) * nbniveau);
    for (i = 0; i < nbniveau; ++i)
    {
        Matrice[i] = (int **) malloc (sizeof(**Matrice) * nbligne);
        for (j = 0; j < nbligne; ++j)
        {
            Matrice[i][j] = (int *) malloc (sizeof(***Matrice) * nbcolonne);
        } 
    }
    return Matrice;
}

// computation, takes 2D array, modifies in place
void recuperationmatrice(int** matrice)
{
    matrice[0][1] = 1;
}

// you shouldn't use void main, its not part of the standard
int main(int args, char **argv)
{
    int*** matrice = allocationdynamique(3, 3, 7);
    recuperationmatrice(matrice[1]);

    return 0; 
}

Also, from a stylistic perspective, your variable names are ridiculously long, and you should use spaces around operators.

A lot of the mistakes in this code are mistakes the compiler is capable of detecting and warning you about. They are valid C code that will compile properly, but in this and most other cases are written in error and will not work as intended. You should compile with compiler warnings to weed out possible accidents:

gcc -Wall -c file.c -o file.o
Sign up to request clarification or add additional context in comments.

2 Comments

Just to add to the points about stylistic issues, if you have a variable or function name that is more than one word it's best to either camel case it or add underscores. e.g. change allocationdynamique to allocationDynamique or allocation_dynamique.
It's also worth noting that of all of the naming conventions i've ever seen, upper case variable names and all lowercase function names with no underscores is not one of them. In general, which style you use is of no consequence, even if you make up your own, but I cannot stress the importance of consistency. Reading code written to no specific style is hard for you and hard for everyone else. Pick one style and stick with it. Also Gordon is a ninja.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.