Muhammad Waqar
Muhammad Waqar

Reputation: 879

Segmentation fault when writing to struct

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.

Upvotes: 2

Views: 375

Answers (1)

paddy
paddy

Reputation: 63451

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;

Upvotes: 3

Related Questions