简体   繁体   中英

Having trouble allocating memory for my double pointer in structure

I'm trying to allocate memory for a pointer, but have a reference to the address of that pointer. I'm still pretty new to C and this is my first time working with double pointers really. So I have two structures and they look like this:

typedef struct myNodes {
   int data;
   struct myNodes *next;
} listElement;


typedef struct {
   listElement **ptrToElements;
} myStruct;

In another file, I'm trying to dynamically allocate memory for my pointer by doing something like this:

myStruct *myStruct = malloc(sizeof(*myStruct));
*(myStruct->ptrToElements) = (listElement*)malloc(sizeof(listElement));

but I keep encountering a segmentation fault from doing so. What could be the issue? Thanks!

The problem is with

*(myStruct->ptrToElements) ....

statement. Before dereferencing myStruct->ptrToElements , you need to make sure it points to a valid memory.

To elaborate, you allocate memory for myStruct . Fine.

That constitutes allocating memory for the member ptrToElements . Good.

  • Question: What does ptrToElements points to?

  • Answer: Indeterministic.

So, when you try to derefernce a pointer which points to an indeterministic memory address, it's pretty much invalid memory address and attempt to do so will invoke undefined behavior.

Solution : You need to allocate memory for myStruct->ptrToElements before you can go ahead and dereference it.

having said that, please see do I cast the result of malloc?

I think this is what you want:

typedef struct myNodes {
   int data;
   struct myNodes *next; // not clear what this pointer is used for...
} listElement;

typedef struct {
   listElement *ptrToElements;
} myStruct;


// Memory Allocation
// allocate myStruct pointing to an array of N listElements
myStruct      *ms = malloc(sizeof(myStruct));
ms->ptrToElements = malloc(N * sizeof(listElement));

// element access
for (int i = 0; i < N; i++) {
    listElement *le = ms->ptrToElements[i];
    le->data = ...
    le->next = NULL; // not clear what this pointer is used for...
}

// Memory deallocation
free(ms->ptrToElements);
free(ms);

You define the structure to contain a pointer to a pointer to a listElement

typedef struct {
   listElement **ptrToElements;
} myStruct;

As Sourav Ghosh wrote, you try to assign a value to the pointer where ptrToElements would point to without allocating memory.

Probably you should change the pointer type to

typedef struct {
   listElement *ptrToElements;
} myStruct;

and when allocating the memory

myStruct *myStruct = malloc(sizeof(*myStruct));
/* If the list can be empty, initialize root with NULL pointer */
myStruct->ptrToElements = NULL;

/* when you add the first list element */
myStruct->ptrToElements = malloc(sizeof(listElement));
myStruct->ptrToElements->data = someValue;
/* at least for the last element you add here you should initialize next */
myStruct->ptrToElements->next = NULL;

Don't forget to handle errors, eg malloc returning NULL .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM