Skip to content
Advertisement

Segmentation fault core dumped 2d array

I’m trying to compile a c code under linux using gcc-4.9 (tried also 5.4) while so, I faced a segmentation fault error.

Program received signal SIGSEGV, Segmentation fault.
0x080492e6 in dot (p1=0x8d9e6c0 <permy>, p2=0x3d77ca7c) at autrq.h:135
135     j = p2[i];  

this is the part of code where the problem exists:

#define N 239
#define K 120

void dot(int p1[N], int p2[N]) {
    int p3[N], i, j; //printf("n debut dot ");

    for (i = 0; i < N; i++){
        p3[i] = p1[i];
    }

    for (i = 0; i < N; i++) { 
        j = p2[i];  
        if(j>=N){
           printf("Too largen");
        }
        else{

           p1[i]=p3[j];
        }
    } //printf("n fin dot ");
}

void GenAut(int permy[N]) {
    int i, j, c, f;
    //printf("n debut GenAUT ");
    int inf[K], mo[N], mi[N];

    for (i = 0; i < N; i++){
        permy[i] = i;
    }

    j = GenIdex(1, 100);

    for (c = 0; c < j; c++) {
        f = GenIdex(0, pos);
        //printf("n pos: %d et f %d ",pos,f);
        dot(permy, automorf[f]);
    }
}

automorf is an array n x n long (int automorf[n][n])

Can you please help correcting the problem?

Advertisement

Answer

Try this and see output output

we have not p3[3]; and trying to access make error or undefined behavior.

#include <stdio.h>
#define N 3

void dot(int p1[N], int p2[N]) {
    int p3[N], i, j; //printf("n debut dot ");

    for (i = 0; i < N; i++){
        p3[i] = p1[i];
    }

   for (i = 0; i < N; i++) { 
        printf("i:%d p3[i]%dn", i,p3[i]) ;
   }

    printf("n") ;


    for (i = 0; i < N; i++) { 

        j = p2[i]; 

        printf("i:%d j:%d p3[%d]:%dn", i, j, j, p3[j]) ;

        p1[i]=p3[j];

    } 

    //printf("n fin dot ");
}


int 
main(){

    int x[N]={10,20,30};
    int y[N]={1,2,3};

    dot(x, y);


    printf("n");
    return 0;   
}
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement