Error when freeing memory

This is a discussion on Error when freeing memory within the C Programming forums, part of the General Programming Boards category; This code doesn't really do anything. I am just testing some function prototyping - but have got myself rather stuck. ...

  1. #1
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343

    Error when freeing memory

    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);
    }

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,859
    Could it be, as I have not used memset()

    memset(pMylist, NULL, size);

    This is setting all of the char to NULL. Not a char struct (may make error with terminator) and you are setting even the struct padding to null. Can't play with the padding as far as I know with out causing errors.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    Thats a good question - anyone know? I was under the assumption I could use memset to initialise all of the members of a structure to NULL. Is this not the case?

  4. #4
    Unregistered
    Guest
    No, its not the problem. Though my complier generates two warinings on that memset() line.

    memcpy(pMynode->data, pData, size);

    is the problem, if I comment it out no problem. Seems legal to me.

  5. #5
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,859
    Forgot to log in.

    I commented out the memset() line and then get an error on the

    free(pMynode->data);
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  6. #6
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,859
    DOHHH!!!

    Apart from not setting the pointers to NULL for the malloc tests and using calloc to zero the mem

    its

    if ((pMynode->data = malloc (sizeof(size))) == NULL)

    Now you tell me why. Sometimes you can't see the forrest for the trees.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  7. #7
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    Oohh crap.. Your right!
    That was a rather silly mistake.
    Thanks for your time all.

    But I am rather intrigued as to why the warnings on memset line. It seems to be complaining that NULL is of the wrong type..

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,673
    For a start, the 2nd arg to memset is an integer - in your compiler, NULL is obviously something like (void*)0

    Just
    memset(pMylist, 0, size);

    And your insert function doesn't do anything useful, since it doesn't actually touch pList.

  9. #9
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    For a start, the 2nd arg to memset is an integer - in your compiler, NULL is obviously something like (void*)0
    Good point. Thanks for the tip Salem.
    And your insert function doesn't do anything useful, since it doesn't actually touch pList.
    Yeah, that was intentional, rather than post the entire program that may have put a lot of people off of reading it - I wrote a shorter program to demonstrate the error.

    Come to think of it, there are probaby a lot of people who have read this and thought.. "Well that doesn't do anything useful!"

  10. #10
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    In c, at least on my system, NULL is defined to be
    ((void*)0). In c++ NULL is defined to be 0.

    You really shoudn't use memset in that way.
    http://www.eskimo.com/~scs/C-faq/s5.html

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory freeing function
    By johndoe in forum C Programming
    Replies: 4
    Last Post: 02-17-2006, 01:08 AM
  2. Freeing memory for non-pointer variables
    By SuperGodAntMan in forum C++ Programming
    Replies: 7
    Last Post: 02-11-2006, 12:30 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  4. memory allocation and freeing
    By Jase in forum Linux Programming
    Replies: 1
    Last Post: 05-25-2003, 06:26 AM
  5. freeing memory
    By mart_man00 in forum C Programming
    Replies: 1
    Last Post: 04-27-2003, 08:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21