2

I'm having some trouble dereferencing a pointer to a pointer. I have a node e that I put onto the heap and also another node called List. List is a pointer pointing to e.

For some reason I am having a segmentation fault when I am dereferencing e through List:

struct ELEMENT{
    int key;
    int edge;
    struct ELEMENT *adjList;
};

int numOfNodes = 3; 

struct ELEMENT *e = (ELEMENT*)malloc(numOfNodes * sizeof(struct ELEMENT));
// e is now on the heap

struct ELEMENT **List = (ELEMENT**)malloc(numOfNodes * sizeof(struct ELEMENT));
// List (pointer to e) is now on the Heap

List[1]->key = 5;  // segmentation fault occurs here
JokimNoah
  • 35
  • 2

3 Answers3

1

As others have already noticed you don't want a second malloc. You simply want List to point to e so all you need is to assign the "address of" e to list.

struct ELEMENT **List = &e;

Besides that the way you try to access elements using List is wrong. Look at it like this:

  • e[n] will access struct ELEMENT at index n

  • *List is the same e

  • So to access elements in the array you'll need (*List)[n]

  • and to access a member you'll need (*List)[n].key

BTW: None of your ... malloc.... can compile as you have forgotten struct in the cast but instead of adding structjust remove the cast. Like:

struct ELEMENT *e =  malloc(numOfNodes * sizeof *e);
                    ^                           ^^
                no cast                    use dereferenced pointer to calculate size

With these changes your complete code will be:

struct ELEMENT{
    int key;
    int edge;
    struct ELEMENT *adjList;
};

int numOfNodes = 3; 

struct ELEMENT *e = malloc(numOfNodes * sizeof *e);
struct ELEMENT **List = &e;
(*List)[1].key = 5;
Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
0

If List should be pointing to e, then you should not call malloc() again. You should do:

struct ELEMENT **List = &e;
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

With this line

struct ELEMENT *e = (struct ELEMENT*)malloc(numOfNodes * sizeof(struct ELEMENT));

you don't just allocate memory for struct ELEMENT variable but you create an array with size 3 and type struct ELEMENT.

But as i understand by your code you want to create a List and add variable e. This can be done like that

struct ELEMENT **List = malloc(numOfNodes * sizeof(struct ELEMENT*));   //This allocate memory for 3 pointers, List[i] is type struct ELEMENT *

After you can allocate memory for each pointer like that

int i;
for(i = 0; i < numOfNodes; i++)
{
    List[i] = malloc(sizeof(struct ELEMENT));   //here allocate memory for each pointer, for now each List[i] is type struct ELEMENT
}

So can change the declaration of e to struct ELEMENT *e = malloc(sizeof(struct ELEMENT));

and use e as you want for example

e->adjList = NULL;
e->edge = 5;
e->key = 15;

And add it to List

List[i] = e;

If you just want a pointer to reference at variable e you can use struct ELEMENT **List = &e; or more simple struct ELEMENT *p_elem = e;. So you can use these pointers to change the value of e members.

Don't forget to free the memory that previous allocated.

Hope this solve you any conclusions.

aragon
  • 114
  • 1
  • 8