-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.

3
  • 1
    You need to show us what you've tried, what you expect to happen, and what seems to happen instead. Commented Dec 9, 2014 at 0: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. Commented Dec 9, 2014 at 0: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. Commented Dec 9, 2014 at 1:49

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you, you lovely person.
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.
Do you have the same code?, someone corrected me because on the for I had i < Y, when it had to be i < X.
"need" is not correct; you can use a single pointer. see here or one of the countless other examples
Thanks for the correction, I was wrong, there are other options.
|

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.