This code doesn't really do anything. I am just testing some function prototyping - but have got myself rather stuck.
I get an error when I free the memory that I obtained for the list DLL *pMylist. It is as if the memory wasn't allocated or I have already freed it, but I cannot see where..
Could someone point out to me the error?
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#define MAX 50 // The maximum size of the arrays in the struct
typedef struct list DLL;
typedef struct node NODE;
struct node
{
void *data;
NODE *prior;
NODE *next;
};
struct list
{
long int count;
NODE *head;
NODE *tail;
};
typedef struct
{
long int id;
char name[MAX];
char address[MAX];
float salary;
} PERSON;
typedef struct
{
PERSON details;
char department[MAX];
char position[MAX];
} EMPLOYEE;
void init_list(void *, size_t);
void insert(DLL *, void*, size_t);
int main(void)
{
DLL *pMylist;
EMPLOYEE *pEmployees;
if ((pMylist = (DLL *) malloc (sizeof(DLL))) == NULL)
{
printf("Error: Unable to allocate memory");
exit(1);
}
if ((pEmployees = (EMPLOYEE *) malloc (sizeof(EMPLOYEE))) == NULL)
{
printf("Error: Unable to allocate memory");
exit(1);
}
init_list (pMylist, sizeof(DLL));
printf("Enter a number: ");
scanf("%d", &pEmployees->details.id);
insert(pMylist, pEmployees, sizeof(EMPLOYEE));
// When I free up pMylist I get the error
free (pMylist);
free (pEmployees);
return EXIT_SUCCESS;
}
void insert(DLL *pList, void *pData, size_t size)
{
NODE *pMynode;
if ((pMynode = (NODE *) malloc(sizeof(NODE))) == NULL)
{
printf("Error: Unable to allocate memory!");
exit(1);
}
if ((pMynode->data = malloc (sizeof(size))) == NULL)
{
printf("Error: Unable to allocate memory!");
exit(1);
}
memcpy(pMynode->data, pData, size);
free(pMynode->data);
free(pMynode);
}
void init_list(void *pMylist, size_t size)
{
memset(pMylist, NULL, size);
}