-2

I started out with a simple idea.

I have one struct with things i need to modify inside

typedef struct {
    int stuff;
    int things;
    unsigned long long store;
 } line;

and I need to make a 2d array of these structures, dependent on two other numbers that have already been read in and initialized. Yet when I try to just create

line book[X][Y];

I segfault all over the place.

I know i need to use malloc but no matter what I try I can't seem to make this work!

How do I achieve my goal? I really just need help understanding malloc, its such a foreign concept. If someone could help me out that would be amazing.

  • 1
    You need to show us what you've tried, what you expect to happen, and what seems to happen instead. – MooseBoys Dec 09 '14 at 00:31
  • What i've tried: I had a 2d array of pointers to instances of the structure that I malloc'd through two nested for loops but I would get very random segfaults (work half of the time, other half not.) So I decided to try to simplify things and declare an array of the structures like above but that won't work either. I just dont understand malloc, and when dealing with structures that appears to be essential. What I want is just a 2d array the above "line" structure that I can manipulate the fields inside of it. Like book[0][2].stuff = 4; for example. – DimDermingshod Dec 09 '14 at 00:34
  • You don't need to use malloc. `line book[X][Y];` is fine (up to a certain size). If you get segfaults it may be because there are problems in the rest of your code. Further, if you do decide to use malloc then you don't need to use multiple malloc calls; a single malloc is fine. – M.M Dec 09 '14 at 01:49

1 Answers1

0

One way to do this is using a double pointer to line, since with one pointer you can alloc a single "array" of lines and with a double pointer you can alloc an "array" of "arrays" of lines.

See here if you have some doubts.

So, you can alloc memory for the "array" to pointers to line, like.

line **book = NULL;

book = malloc(sizeof(line *) * X);

With this you will have an "array" of pointers to line, then you have to iterate trough each element of book to make each pointer point to another "array", to do this we use malloc for each element of book.

for (i = 0; i < X; i++)
    book[i] = malloc(sizeof(line) * Y);

Also, don't forget to free the pointers when you are done with them.

Patricio Sard
  • 2,092
  • 3
  • 22
  • 52
  • Thank you, you lovely person. – DimDermingshod Dec 09 '14 at 00:43
  • i prefer to `malloc(X * Y * sizeof(line))` because having your memory in one big block is better for performance. you can index it like `book[row * X + col]` or still build up another array of pointers to each row if you want that. – japreiss Dec 09 '14 at 00:59
  • Do you have the same code?, someone corrected me because on the for I had `i < Y`, when it had to be `i < X`. – Patricio Sard Dec 09 '14 at 01:11
  • "need" is not correct; you can use a single pointer. [see here](http://stackoverflow.com/a/25516585/1505939) or one of the countless other examples – M.M Dec 09 '14 at 01:59
  • Thanks for the correction, I was wrong, there are other options. – Patricio Sard Dec 09 '14 at 02:09
  • Agreed. As long as it is is pointing to the start of the struct it really doesn't matter if it's a * or a **. The ** allows you to use the built in array mapping functions which just do the pointer arithmetic for you. – David Hoelzer Dec 09 '14 at 02:19