wierd struct error

This is a discussion on wierd struct error within the C Programming forums, part of the General Programming Boards category; Hi, I was reading a text file,and storing each word in a struct: Code: typedef struct WordNode{ struct wordNode *nextWord; ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    24

    wierd struct error

    Hi,
    I was reading a text file,and storing each word in a struct:
    Code:
    typedef struct WordNode{
    	struct wordNode *nextWord;
    	char *word;  //store the word
    	int whiteSpaces; //num of spaces between words
    }wordNode;
    and i got a function makeWordNode function to create a word node

    Code:
    wordNode *makeWordNode(char* string,int numSpaces){
    	wordNode *newWordNode;
    	newWordNode=(wordNode*)malloc(sizeof(wordNode));  
    	strcpy(newWordNode->word,string); 
    	newWordNode->whiteSpaces=numSpaces;
    	newWordNode->nextWord=NULL;
    	
    	printf("#%s->%d#",newWordNode->word,newWordNode->whiteSpaces);
    	return newWordNode;
    }
    interesting this code wont work and crashes.so i added printing of # char to check where the code hangs:
    Code:
    wordNode *makeWordNode(char* string,int numSpaces){
    	wordNode *newWordNode;
    	newWordNode=(wordNode*)malloc(sizeof(wordNode)); printf("#");
    	strcpy(newWordNode->word,string); 
    	newWordNode->whiteSpaces=numSpaces;
    	newWordNode->nextWord=NULL;
    	
    	printf("#%s->%d#",newWordNode->word,newWordNode->whiteSpaces);
    	return newWordNode;
    }
    Interestingly the 2nd version works fine and prints correct output.I cant figure out the reason for this wierd error.

    help me!! Thanks

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Where do you allocate space for 'newWordNode->word' before you strcpy to it?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    9
    yaah..!!!!........ memory has not been allocated

  4. #4
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    Could you post the whole code? Then , maybe I can see , what you could not. Maybe.

  5. #5
    Registered User
    Join Date
    May 2007
    Posts
    24
    Thanks for helping out.no memory allocation is the main problem.But I wonder how come the 2nd code worked as it didn't have memory allocation.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,420
    > But I wonder how come the 2nd code worked as it didn't have memory allocation.
    Sometimes, despite your best efforts to screw things up, the code still manages to do what you expect without crashing.
    On other occasions, you get absolutely no margin at all for error. Even very minor mistakes can be punished harshly.

    It's just Murphy at work. You just have to learn to not equate "working" with "bug free".
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. Replies: 16
    Last Post: 10-29-2006, 04:04 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM

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