Memory leak in a linked list. 
Author Message
 Memory leak in a linked list.

Hello,

        I have code (condensed version included below) that works very
well, except for memory leaks! Oxymoron? I've included only the input
and output portions of my code because I believe this is where the error
resides. Because of the input()ing process, the program takes up ten
times the size of the input and nothing seems to be freed until the
program terminates. I think it's a case of locally malloc()d and
declared variables being returned. But I submit this situation to the
think tank of comp.lang.c..

// It uses a linked-list..
struct node {
        char *entry;
        struct node *link;

Quote:
};

//Application: headNode = input(headNode, string);
//..where 'string' is a NULL terminated array of characters.

struct node * input(struct node * headNode, char *input) {
        int length;
        struct node *newNode;

        newNode = (struct node *) malloc(sizeof(struct node));

        length = strlen(input)+1;
        newNode->entry = malloc(length);
        memcpy(newNode->entry, input, length);
        newNode->link = headNode;

        // The new headNode should be assigned directly?
        return newNode;

Quote:
}

//Application: output(headNode);

void output(struct node * headNode) {
        struct node *currNode;
        struct node *tmpNode;

        currNode = headNode;
        while(currNode != NULL) {
                printf("%s\n", currNode->entry);
                tmpNode = currNode;
                currNode = currNode->link;
                /*At this point the memory is effectively freed, however
the amount of memory consumed by the program isn't reduced. I assume
this is one of the wonders of Windows NT, where it decides to keep the
freed memory for future calls to malloc()?*/
                //free((char *)tmpNode->entry);
                free((struct node *)tmpNode);
        }

Quote:
}

Thanks,

Daniel Tremblay



Tue, 16 Sep 2003 03:13:18 GMT  
 Memory leak in a linked list.
See my comments below:


Quote:
> Hello,

> I have code (condensed version included below) that works very
> well, except for memory leaks! Oxymoron? I've included only the input
> and output portions of my code because I believe this is where the error
> resides. Because of the input()ing process, the program takes up ten
> times the size of the input and nothing seems to be freed until the
> program terminates. I think it's a case of locally malloc()d and
> declared variables being returned. But I submit this situation to the
> think tank of comp.lang.c..

> // It uses a linked-list..
> struct node {
> char *entry;
> struct node *link;
> };

> //Application: headNode = input(headNode, string);
> //..where 'string' is a NULL terminated array of characters.

> struct node * input(struct node * headNode, char *input) {
> int length;
> struct node *newNode;

> newNode = (struct node *) malloc(sizeof(struct node));

> length = strlen(input)+1;
> newNode->entry = malloc(length);
> memcpy(newNode->entry, input, length);

If you mean strcpy, say it so.

- Show quoted text -

Quote:
> newNode->link = headNode;

> // The new headNode should be assigned directly?
> return newNode;
> }

> //Application: output(headNode);

> void output(struct node * headNode) {
> struct node *currNode;
> struct node *tmpNode;

> currNode = headNode;
> while(currNode != NULL) {
> printf("%s\n", currNode->entry);
> tmpNode = currNode;
> currNode = currNode->link;
> /*At this point the memory is effectively freed, however
> the amount of memory consumed by the program isn't reduced. I assume
> this is one of the wonders of Windows NT, where it decides to keep the
> freed memory for future calls to malloc()?*/

Nothing is freed here. Anyway, malloc is implemented in the C runtime
library that you use.  NT's memory management is at a higher level.

Quote:
> //free((char *)tmpNode->entry);

Why did you comment this line ? You need to free the entry, you allocated it
in input().

- Show quoted text -

Quote:
> free((struct node *)tmpNode);
> }
> }

> Thanks,

> Daniel Tremblay




Tue, 16 Sep 2003 03:26:04 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Memory leak using template linked list????

2. Memory leak using template Linked List???

3. Clearing Memory || Linked List INSIDE of a Linked List

4. link list of a linked list (chained links?)

5. Memory leak in stl::list

6. Memory leak using typedef<list>

7. Incompatible NULL Assignments || Linked List inside Linked List

8. Freeing a Linked List inside of a Linked List

9. Linked List of Linked Lists

10. Define a linked list of a linked list

11. Link List to Link List, HELP friends

12. load file into memory w/ linked list of buffers

 

 
Powered by phpBB® Forum Software