2
votes

I have a struct defined in my program as:


    struct memregion {
      void *from;
      void *to;
      int mode;
    }

I declare in my program, an array of this struct as struct memregion regions[10]. Then I pass it to a function as get_mem_layout(regions, 10) whose declaration is:


    void get_mem_layout(struct memregion *regions, int size)

However, when I try to write data to any member variable of the struct as


    regions[j].mode = 1;

OR


    void *addr;
    addr = (void *)0;
    regions[j].from = addr;

I receive a segmentation fault. I cannot determine why this is happening. Please help.

UPDATE: Full code removed because it was part of an assignment. Problem was resolved through @paddy's answer.

1
Can you guarantee that j < size? - chrisaycock
Please add more complete code, such as the loop. - Jonathan Seng
Can you paste the code relevant to the variable j? - asgs
Yeah this description has to be incorrect, we need to see more code. Also... why not simply assign NULL to regions[n].from? - Ed S.
Exactly where does your declaration of regions[10] occur? Are you calling get_mem_layout() in the same block of code? - DarenW

1 Answers

3
votes

Why has nobody picked this up? Or am I missing something?

In get_mem_layout one of the first things you did is this:

regions = 0;

Then you go on to access regions as an array...

Did you mean:

num_regions = 0;