0

I have the following program which gives me a Segmentation fault (core dumped) on line *(x[0][0])=(int )&c[1][1];

int main() { 
    int (*ovi)[3]=  malloc(5*(sizeof *ovi));
    int  (**x)[3]=  malloc(5*(sizeof x));
    int    *** c =  malloc(4*(sizeof*c));

    *(c+0)=(int **)&(*(ovi+1));
    *(c+1)=(int **)&(*(ovi+2));

    *(*(ovi+0)+0)=0;
    *(*(ovi+0)+1)=1;

    *(*(ovi+1)+0)=10;
    *(*(ovi+1)+1)=11;

    *(*(ovi+2)+0)=20;
    *(*(ovi+2)+1)=21;

    int *y[3][5] ;
    y[0][0]=(int *)&c[1][1];
    printf("%i\n",*(y[0][0]));

    *(x[0][0])=(int )&c[1][1];

    printf("%i\n",(*(x[0][0]))); //output should be 21

    free(ovi);
    free(c);
    return(0);
}
atk
  • 9,244
  • 3
  • 32
  • 32
  • 3
    That's a lot of asterisks. I would think you could make this more readable. – crashmstr Oct 25 '13 at 18:54
  • 1
    This makes Brainf**k seem almost readable. – Kninnug Oct 25 '13 at 18:56
  • 2
    I think you need a few more levels of indirection in here. – WhozCraig Oct 25 '13 at 18:56
  • 1
    This might help: http://stackoverflow.com/a/18580625/1679849 – r3mainer Oct 25 '13 at 18:58
  • 5
    No 3D arrays here. I only see triple pointers. And [being called a Three-Star programmer is not a compliment](http://c2.com/cgi/wiki?ThreeStarProgrammer). –  Oct 25 '13 at 19:03
  • I would follow the recommendation in @squeamishossifrage's comment, and then try running your code with Valgrind and see what happens. – CmdrMoozy Oct 25 '13 at 19:04
  • And I would suggest that you not try to use arrays as pointers (nor the other way around). If you need a cast related to pointers, that's a red flag and you are probably doing something very nasty. –  Oct 25 '13 at 19:05
  • Yes, @squeamishossifrage's comment is the best, but to answer your question or at least help you figure out what's wrong. I would break that line into pieces and see which dereference is failing. There are *five* pointer references in that line. [That line has a cast to int. Did you mean (int*)?] – Randy Stegbauer Oct 25 '13 at 19:14

1 Answers1

1

There are many problems with this code. Here is the first point at which a segfault can (and likely will) occur:

*(*(ovi+0)+0)=0;
*(*(ovi+0)+1)=1;

*(*(ovi+1)+0)=10;
*(*(ovi+1)+1)=11;

*(*(ovi+2)+0)=20;
*(*(ovi+2)+1)=21;

ovi is a 3-element array of int*. You must initialize the elements of ovi before they can be used safely. Otherwise, you're dereferencing pointers that could point anywhere, hence the segfault.

It's not clear what you're trying to do, but if you're trying to declare a 2-D array of int that has a static size, then do it this way:

int ovi[3][N];

where N is a compile-time constant for the number of columns you'd like for your matrix. This actually allocates storage of the 3N ints.

Otherwise, you need to explicitly initialize all of your pointers, and they need to point to storage allocated for actual integers.

int actual_integers[6];  /* you seem to have six elements of ovi */
ovi[0] = actual_integers + 0;
ovi[1] = actual_integers + 2;
ovi[3] = actual_integers + 4;

But, seriously, there's no reason for playing with pointers in the way that you do. Whatever you're trying to accomplish: do it simply and work on more interesting problems. Programming and computer science are too full of problems that are both hard and interesting to focus on obscure code and pointer/array indirection.

sfstewman
  • 5,589
  • 1
  • 19
  • 26