Thread: memory leak

  1. #1
    Registered User
    Join Date
    Feb 2006

    memory leak

    Hi i got a program to store words and number of spaces after each word.
    Words would be in a word node.the code is:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    wordNode stores the word,and the number of spaces after the word.
    Also stores a link to the next word node.
    typedef struct WordNode{
    	struct WordNode *nextWord;
    	char *word; 
    	int numSpaces;
    makeWordNode() creates a newWord node.The parameters are string,the data to be assigned to the 
    wordNode.The other parameter is numSpaces,it assigns the number of spaces after a word
    wordNode *makeWordNode(char* string,int numSpaces){
    	wordNode *newWordNode;
    	/*allocate memory for node*/
    	/*allocate memory for the word data*/
    	/*assign the string to the wordNode*/
    	/*assign numspaces to word node*/
    	return newWordNode;
    void freeNode(wordNode* node){
    int main(void){
    	wordNode *a;
        return 1;
    i compiled it in work fine.when i use the bcheck memory leak checker,i get 2 errors.It seems the above code leaks memory at makeWordNode()

    Can someone please help me find what's the error.I am guide.

    Thanking you,
    Rahul SK

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    You never call "freeNode", so I expect it to say that you have two objects allocated (one for the node itself, and one for the string inside the node).

    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Cambridge, UK
    And I suppose you should be calling that in the main after makenode .So that once u create the node and fill int he node. But make sure u free them after doing all your node manipulation. other u will get a seg fault.


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory leak in this case?
    By George2 in forum C++ Programming
    Replies: 3
    Last Post: 03-22-2008, 05:05 AM
  2. memory leak in the code?
    By George2 in forum C++ Programming
    Replies: 20
    Last Post: 01-13-2008, 06:50 AM
  3. Is this code memory leak free? ---> POSIX Threads
    By avalanche333 in forum C++ Programming
    Replies: 9
    Last Post: 04-13-2007, 03:19 PM
  4. Any Memory Leak Checking Tool?
    By George2 in forum C Programming
    Replies: 4
    Last Post: 06-21-2006, 11:02 PM
  5. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM