Allocating Array of Pointers to Struct Nodes Causing Crash?

  • Thread starter Thread starter camel-man
  • Start date Start date
  • Tags Tags
    Array Pointers
AI Thread Summary
The discussion centers on a coding issue related to a crashing program that involves creating an array of pointers to struct Nodes. The code compiles correctly, but the tester program crashes during execution. The main focus is on the `newEmployeeList` function, where memory allocation for an array of pointers to nodes is performed. A key point raised is the necessity of allocating memory for the nodes themselves, not just the pointers. The suggestion is made to either initialize the pointers to NULL or to allocate memory for each node in the array. The use of a size multiplier (2) for the array is explained as a requirement for a hash table implementation, but it is emphasized that without proper node allocation, the program will encounter issues.
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;
    }
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Back
Top