0

I have a structure to store information in a 2D array:

struct slopes {
    int size;
    int ** slope_array;
};

I malloc the required memory for the structure(the array has dimensions of s*s):

struct slopes * slope=malloc(sizeof(struct slopes));
slope->size=s;
slope->slope_array=malloc(sizeof(int *)*s);
int i;
for(i=0;i<s;i++) {
    slope->slope_array=malloc(sizeof(int)*s);
}

But lines such as these seem to throw segmentation errors:

slope->slope_array[0][0]=3;

Can someone see what I'm doing wrong?

Fiddling Bits
  • 8,712
  • 3
  • 28
  • 46
kawaiinekochan
  • 212
  • 3
  • 12
  • Of course, such basic questions have been answered several times already. :-) Please use the search field next time. But seeing the answers coming, it motivates me next time to just reply rather than marking it as duplicate for the better sake of the site. This is a sad state of affairs. :( – László Papp Dec 18 '13 at 13:00
  • @LaszloPapp I don't actually think this question is a duplicate of that one -- it's similar, but the OP already knows how and why to initialize the inner array variables, the code just had a small bug in the inner loop (maybe even a typo). You are right that this probably is a duplicate of *some* SO question, and I will try harder to flag those cases when I see them. – Tim Pierce Dec 18 '13 at 13:13
  • 1
    @qwrrty: it was closed within 15 minutes, and frankly, there is no need for any typo if you can copy/paste an existing solution. A typo is also off-topic for the site, and they usually get closed so. You cannot have anything usable out of that for the posterity most of the time. – László Papp Dec 18 '13 at 13:14
  • You aren't allocating a 2D array, you are allocating a pointer-based lookup table, segmented all over the heap. See [how to correctly set up a multi-dimensional array](http://stackoverflow.com/questions/12462615/how-do-i-correctly-set-up-access-and-free-a-multidimensional-array-in-c). – Lundin Dec 18 '13 at 13:52

2 Answers2

2

There is a simple bug in your code, in the for loop do not assign the pointer returned by malloc to slope->slope_array, but to slope->slope_array[i]

slope->slope_array[i] = malloc(sizeof(int) * s);

AJ.
  • 4,526
  • 5
  • 29
  • 41
Marco Sandrini
  • 688
  • 4
  • 11
2

In your for loop, you need to initialize slope->slope_array[i], not slope->slope_array:

for (i = 0; i < s; i++) {
    slope->slope_array[i] = malloc(sizeof(int)*s);
}

Note: if you had cast the return call from malloc to an int *, the compiler would have warned you about this error...

Tim Pierce
  • 5,514
  • 1
  • 15
  • 31
  • Funny — that comment about casting the result of `malloc()` was one of my thoughts, too. However, you should be aware that there is an ardent school of C programmers (active on SO) who are adamant that you should never cast the result of `malloc()`. I'm not of that school for a variety of reasons. – Jonathan Leffler Dec 18 '13 at 13:04
  • Yes, I'm aware, and I rather expect this answer to get downvoted for that reason, but I think it's important to point out the pros of casting `malloc` as well as the cons. Remember the Alamo! – Tim Pierce Dec 18 '13 at 13:06