C allocating array of pointers

In summary, the conversation is discussing a problem with a piece of code that creates an array of pointers to struct Nodes. The code is compiling correctly, but when the tester program is used, it crashes. The code is for creating an employee list and allocating memory for the array of pointers to nodes. The conversation also includes suggestions for improvements, such as casting the value returned by malloc and allocating nodes during initialization.
  • #1
camel-man
76
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
  • #2
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.
 
  • #3
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.
 
  • #4
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;
    }
 
  • #5


Hello,

Thank you for reaching out for help with your code. From what I can see, the code you have provided looks correct and should be working. However, without the full code and error message, it is difficult to determine what is causing the crash in your tester program.

Here are a few suggestions that may help you troubleshoot and fix the issue:

1. Make sure you are passing the correct size when calling the newEmployeeList function. If the size is too large, it could cause a crash due to memory allocation issues.

2. Check that you are properly accessing the elements of the array in your for loop. It is possible that you are accessing an element out of bounds, which could cause a crash.

3. Ensure that the EmployeeList and NodeP structures are defined correctly in your header files. If there are any discrepancies in the definitions, it could cause issues when accessing the structures in your code.

4. Consider using a debugger to step through your code and see where the crash is occurring. This can help you pinpoint the exact location of the issue.

I hope these suggestions help you resolve the issue with your code. If you continue to experience problems, please provide more information and the full code so I can better assist you. Good luck!
 

What is "C allocating array of pointers"?

"C allocating array of pointers" refers to the process of dynamically allocating memory for an array that contains pointers in the C programming language. This allows for flexible and efficient use of memory in a program.

How do I declare an array of pointers in C?

To declare an array of pointers in C, you can use the following syntax:
type *arrayName[size];
For example:
int *numArray[5];
This declares an array of 5 integer pointers.

How do I allocate memory for an array of pointers in C?

To allocate memory for an array of pointers in C, you can use the malloc() function. This function takes in the size of the array (in bytes) as its parameter and returns a pointer to the allocated memory. For example:
int *numArray = (int*)malloc(5*sizeof(int*));
This allocates memory for an array of 5 integer pointers.

How do I access elements in an array of pointers in C?

To access elements in an array of pointers in C, you can use square bracket notation:
arrayName[index];
For example:
numArray[2];
This will return the third element in the array. You can also use pointer arithmetic to access elements in the array.

How do I free the allocated memory for an array of pointers in C?

To free the allocated memory for an array of pointers in C, you can use the free() function. This function takes in the pointer to the allocated memory as its parameter and deallocates the memory. It is important to free allocated memory when it is no longer needed to prevent memory leaks in your program.

Similar threads

  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
23
Views
1K
  • Programming and Computer Science
Replies
3
Views
727
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
5
Views
884
  • Programming and Computer Science
Replies
8
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
20
Views
1K
Back
Top