Weird string output only for first run

This is a discussion on Weird string output only for first run within the C++ Programming forums, part of the General Programming Boards category; When calling the following function for the first time, the string it prints out is some weird ANSII characters. However, ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    6

    Question Weird string output only for first run

    When calling the following function for the first time, the string it prints out is some weird ANSII characters. However, when I call on the function subsequently with the exact same variables, it gives the answer.

    I understand that my code may not be the best way to do the required task. However I would like to understand why the function does not work on the first run but works on the subsequent runs.

    Does this have anything to do with fflush ?

    Code:
    /* function call */
    printf("\"%s\" is the midstr of \"%s\" from the position %d with length %d.\n" , midstr( str2 , str1 , start , len ) , str1 , start , len );
    
    char *midstr( char *str2 , const char *str1 , int start , int len )
    /* This function aims to copy a string of length len from str1 to str2 starting from start */ 
    {
            char string[80] = {0};
            int count = 1 , reduce , j;
    
            while ( *str1 != '\0' ){
                    count++;
                    str1++;
            }
    
            if ( start > count || (start + len) > count ) {
                    *str2 = NULL ;
                    return str2;
            }
    
            else {
                    reduce = count - start - len + 1;
                    str1 -= reduce;
                    string[len] = '\0';
    
                    while ( len > 0 ) {
                            printf("1%c\n" , *str1);
                            string[len - 1] = *str1 ;
                            len--;
                            str1--;
                            printf("%c\t" , string[len]);
                    }
    
                   }
    
            str2 = string;
     
            return str2;
    }
    Last edited by Evilelmo; 03-04-2003 at 04:01 AM.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,673
    Your local array "string" is allocated on the stack and is "destroyed" once the function returns. When you say "str2 = string;", str2 is pointing to invalid memory once the function returns. You should store you results directly into str2 or copy the results out of string and into str2 before returning.
    There are other "off-by-1" errors in the code, but you should be able to debug those once you're getting results that make some sense.

    gg

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    6

    Wink

    Thank you. I'll store the char directly into str2 as you suggested.

    I understood what you said about the memory location being invalid but why does this function work when it is called upon the second time?

    Cheers

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,673
    Well, it should never work - you may of gotten lucky and the expected results were sitting on the (cleaned up) stack in the right place when the funtion returned the second time.
    About the "off-by-1" error I mentioned earlier, what I meant was that midstr(str,"Hello cruel world",6,5) returns " crue" - which may be what you intended, but in C, things like to start at 0.

    gg

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    6
    Yup, that was what i intended. Good point though cos I neva took into consideration a broken up string...
    Thanks for all the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 04-21-2006, 08:49 PM
  2. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  3. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM
  4. string handling
    By lessrain in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 07:36 PM

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