Thread: Is there a problem in my code?

  1. #1
    Registered User laughing_man's Avatar
    Join Date
    Jun 2009
    Location
    Bombay, India
    Posts
    26

    Is there a problem in my code?

    I made this C function for easy reading of strings from the console. Can someone please help and find if there are any faults(like a memory leak) in the following code? Which line would the fault be in? I dont know assembly programming and all.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    //global structures and variables
    typedef char* PtrString;
    
    //function declarations
    PtrString readString(void);
    
    //function definitions
    int main(int argc, char *argv[]){
    	PtrString name;
    	
    	puts("What is your name?");
    	name = readString();
    	printf("Hello %s\n",name);
    	return 0;
    }
    
    PtrString readString(void)
    {
    	//declarations
    	struct ele{char ch; struct ele *next;};
    	typedef struct ele Ele;
    	Ele *first = NULL, *last = NULL, *curr = NULL;
    	int ch = 0;
    	long int count = 0;
    	char *str = NULL;
    	int index = 0;
    
    	//dummy node
    	first = (Ele*)malloc(sizeof(Ele));
    	if(first == NULL)
    	{puts("Error: Cannot read characters as there is no memory available."); goto EndIt;}
    	first->ch = '\0';
    	first->next = NULL;
    	last = first;
        
    	//read user input one character at a time into a linked list
    	while(1)
    	{
    		ch = getchar();
    		if(ch == '\n')
    		{
    			break;
    		}
    		else
    		{
    			curr = (Ele*)malloc(sizeof(Ele));
    			if(curr==NULL)
    				{
    					puts("Error: Cannot read characters as there is no memory available.");
    					goto EndIt;
    				}
    			curr->ch = ch;
    			curr->next = NULL;
    			last->next = curr;
    			last = curr;
    			curr = NULL;
    			count++;//we also count the number of characters read in
    		}
    	}
    	printf("count = %d\n",count);
    
    	//create array
    	str = (char *)malloc(sizeof(char)*(count+1));
    	if(str != NULL)
    	{
    		//copy characters from linked list into array
    		index = 0;
    		curr = first->next;
    		while(curr != NULL)
    		{
    			str[index] = curr->ch;
    			curr = curr->next;
    			index++;
    		}
    		str[index] = '\0';
    	}
    	else
    	{
    		puts("Error: Cannot make string as there is no memory available.");
    		goto EndIt;
    	}
        
        
    	EndIt:
    	//destroy linked list, if necessary
    	curr = first;
    	while(curr != NULL)
    	{
    			first = first->next;
    			free(curr);
    			curr=first;
    	}
    	//return actual string or NULL
    	return str;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I do not recommend creating a typedef for a non-opaque pointer type, i.e., get rid of:
    Code:
    //global structures and variables
    typedef char* PtrString;
    Next, reading character by character, storing each character in a node of a linked list, seems like a pretty inefficient way to go about implementing "easy reading of strings from the console". Of course, you could tweak this implementation to have the node store arrays of char instead, but it might be simpler and possibly more efficient to just have a single dynamic array that you keep expanding and then appending to: by keeping track of the number of characters stored, you can append efficiently. You can read the substrings to append using fgets in a loop until a newline is detected.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User laughing_man's Avatar
    Join Date
    Jun 2009
    Location
    Bombay, India
    Posts
    26
    Okay, Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HELP I need to do this problem, can anyone help with the code
    By AcuteApathy in forum C++ Programming
    Replies: 8
    Last Post: 03-30-2011, 04:21 PM
  2. Having a problem with my code :(
    By vivec45 in forum C++ Programming
    Replies: 4
    Last Post: 02-09-2011, 06:06 AM
  3. problem with this code....
    By Huskar in forum C Programming
    Replies: 0
    Last Post: 03-30-2009, 10:38 AM
  4. code problem
    By maritos in forum C Programming
    Replies: 1
    Last Post: 11-27-2005, 05:22 PM
  5. code problem
    By kashifk in forum C Programming
    Replies: 6
    Last Post: 04-22-2003, 05:22 PM

Tags for this Thread