Thread: memory leak

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    58

    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:

    Code:
    #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;
    }wordNode;
    
    /***********************************************************************************
    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*/
    	newWordNode=(wordNode*)malloc(sizeof(wordNode));
    
    	/*allocate memory for the word data*/
    	newWordNode->word=malloc(100*sizeof(char));
    
    	/*assign the string to the wordNode*/
    	strcpy(newWordNode->word,string);	
    	
    	/*assign numspaces to word node*/
    	newWordNode->numSpaces=numSpaces;		
    	
    	newWordNode->nextWord=NULL;
    	return newWordNode;
    }
    
    void freeNode(wordNode* node){
    	free(node->word);
    	free(node);
    }
    int main(void){
    	wordNode *a;
    	a=makeWordNode("first",1);	 
        return 1;
    
    }
    i compiled it in unix.it 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 clueless.do guide.

    Thanking you,
    Rahul SK

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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).

    --
    Mats
    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
    Location
    Cambridge, UK
    Posts
    1,732
    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.

    ssharish

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