Thread: Two "equivalent" functions

  1. #1
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174

    Two "equivalent" functions

    I made an attempt to shorten my code in a particular function because it had lines that repeated themselves.

    The function that has duplicate lines but works

    Code:
    static Line newLines (char text[]) {
        Line list = NULL, curr;
    	int lineCount = 1;
    	int length = strlen(text);
    	int subLength;
    	int i=0, prevPos = i;
    	
    	if (*text == '\0')
    	    return NULL;
    	
    	list = malloc(sizeof(struct line));
    	if (list == NULL)
    	    memoryError();
    	    
        curr = list;
    	
    	for (; i<length; i++) {
    	    if (text[i] == '\n') {
                //line text:
    	        subLength = i-prevPos+1; //+1 = null terminator
                curr->text = malloc(sizeof(char)*subLength);
                if (curr->text == NULL)
                    memoryError();
    	        strncpy(curr->text, &text[prevPos], subLength);
    	        curr->text[subLength-1] = '\0';
    	        //line number:
    	        curr->lineNum = lineCount;
    	        //next line:
    	        curr->next = malloc(sizeof(struct line));
    	        if (curr->next == NULL)
    	            memoryError();
    	        
    	        curr = curr->next;
    	        prevPos = i+1;
    	        lineCount++;
            }
    	}
    	
        //line text:
        subLength = i-prevPos+1;
        curr->text = malloc(sizeof(char)*subLength);
        if (curr->text == NULL)
            memoryError();
        strncpy(curr->text, &text[prevPos], subLength);
    	//line number:
        curr->lineNum = lineCount;
        //next line:
        curr->next = NULL;
    
        return list;
    }

    Notice that the first few lines in the for loop are equivalent to the bottom of the function.

    Now, this is what I did to try and simplify things:

    Code:
    static Line newLines (char text[]) {
        Line list = NULL, curr;
    	int lineCount = 1;
    	int length = strlen(text);
    	int subLength;
    	int i=0, prevPos = i;
    	
    	if (*text == '\0')
    	    return NULL;
    	
    	list = malloc(sizeof(struct line));
    	if (list == NULL)
    	    memoryError();
    	    
        curr = list;
    	
    	for (; i<=length; i++) {
    	    if (text[i] == '\n' || text[i] == '\0') {
    	    printf("text[i] = %c\n", text[i]);
                //line text:
                subLength = i-prevPos+1; // +1 = null terminator
                curr->text = malloc(sizeof(char)*subLength);
                if (curr->text == NULL)
                    memoryError();
                strncpy(curr->text, &text[prevPos], subLength);
    	        //line number:
                curr->lineNum = lineCount;
                
    	        //next line:
                if (text[i] == '\n') {
    	            curr->next = malloc(sizeof(struct line));
    	            if (curr->next == NULL)
    	                memoryError();
    	            
    	            curr = curr->next;
    	            prevPos = i+1;
    	            lineCount++;
                } else
                    curr->next = NULL;
            }
    	}
    
        return list;
    }
    And I just cannot figure out why it doesn't work. It works fine for single line text (no \n chars) but gives me some crazy memory error for multiple line text.

    Could someone please give me a little insight into this?

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    It would help if you told us what that crazy memory error was. It would also help if we had some more code, so we could actually compile and test this ourselves. Last crucial thing would be the input you provide to the program, the (incorrect) output you get if any, and the expected (correct) output.

    This line seems to be missing from your new version:
    Code:
    curr->text[subLength-1] = '\0';
    EDIT: I did it the hard way, just by reading. Try using a diff tool if your system has one, or something like Diff Checker - Online diff tool to find the difference between two text files, to find the difference between two pieces of text.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174
    The missing line that you pointed out was the problem... That's pretty embarrassing haha

    That diff tool is a good idea. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 08-22-2011, 08:39 AM
  2. creating a "to the power" equivalent in c
    By Irony in forum C Programming
    Replies: 16
    Last Post: 10-06-2009, 10:46 AM
  3. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  4. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM
  5. Equivalent of Java's "super" keyword
    By JasonLikesJava in forum C++ Programming
    Replies: 6
    Last Post: 05-31-2002, 10:21 AM