Allocating Array of Pointers to Struct Nodes Causing Crash?

  • Thread starter Thread starter camel-man
  • Start date Start date
  • Tags Tags
    Array Pointers
Click For Summary

Discussion Overview

The discussion revolves around a coding issue related to memory allocation for an array of pointers to struct nodes in C. Participants are examining a specific implementation of a function intended to create an employee list, which is crashing when executed. The focus includes code structure, memory management, and initialization of pointers within the context of a hash table program.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant notes that the line allocating memory for the array of pointers may be problematic and questions the necessity of multiplying the size by 2.
  • Another participant clarifies that the array needs to be twice the size for the hash table specifications.
  • A participant points out that the current implementation does not allocate memory for the nodes themselves, only for the pointers, suggesting that this could lead to crashes when dereferencing uninitialized pointers.
  • There is a suggestion to initialize the pointers to NULL instead of trying to access their members before allocating memory for the nodes.
  • Another approach is proposed where nodes are allocated within a loop, ensuring that each pointer in the array points to a valid node structure.

Areas of Agreement / Disagreement

Participants express differing views on the correct approach to memory allocation and initialization. While there is agreement on the need for proper memory management, there is no consensus on the best method to implement it.

Contextual Notes

Participants highlight limitations in the current implementation, particularly regarding the allocation of memory for the nodes themselves and the handling of pointers before they are initialized.

camel-man
Messages
76
Reaction score
0
I need some help with this piece of code I wrote earlier, it is compiling correctly but when I try to use my tester program it crashes.


I am trying to create an array of pointers to struct Nodes
Code:
typedef struct Node *NodeP;

struct Node
{
	NodeP next;
	EmployeeP info;	
};


struct EmployeeList
{
	NodeP* root;
	int size;
};


EmployeeListP newEmployeeList(int size)
{
	//Creating employeelist and allocating memory for the array or pointers to nodes
	EmployeeListP e=malloc(sizeof(struct EmployeeList));
	e->root=malloc(sizeof(NodeP)* (size*2));
	int i;
	if(e==NULL || e->root==NULL)
	{
		fprintf(stderr,"Failed to allocate memory!\n");
		exit(1);
	}
	//Initializing list size and all pointers to NULL
	e->size=size;
	for(i=0;i<size*2;i++)
	{
		e->root[i]->info=NULL;
		e->root[i]->next=NULL;
	}

	return e;

}


test program where it crashes
Code:
#include <stdio.h>
#include <stdlib.h>
#include"Employee.h"
#include"EmployeeList.h"

int main(void)
{
	EmployeeListP e= newEmployeeList(10);

	return 0;
}
 
Technology news on Phys.org
This line in your newEmployeeList function looks suspect to me.
Code:
e->root=malloc(sizeof(NodeP)* (size*2));
What's the purpose of 2 in the line above?
Also, I think you should cast the value returned by malloc as NodeP.
 
I have to make the array twice the size of the parameter passed in by the user. I am creating a hash table program and that is one of the specifications.
 
During initialization, you haven't allocated any nodes for e->root to use, only the pointers, so all you can do at this point is:

Code:
    for(i=0;i<size*2;i++)
    {
        e->root[i] = NULL;
    }

or you could allocate nodes:

Code:
    for(i = 0; i < size*2; i++)
    {
        e->root[i] = (NodeP) malloc(sizeof(Node));
        e->root[i]->info = NULL;
        e->root[i]->next = NULL;
    }
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 17 ·
Replies
17
Views
4K
  • · Replies 8 ·
Replies
8
Views
5K
  • · Replies 6 ·
Replies
6
Views
8K
  • · Replies 23 ·
Replies
23
Views
2K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
11K
  • · Replies 4 ·
Replies
4
Views
3K