Thread: this function crashes my program, help pls

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    120

    this function crashes my program, help pls

    hi there

    this function:
    Code:
    char* strmerge(const char str1[], char *str2)
    {
        int x=0, chars=0;
        char* str_merged;
    
        while (str1[x]!='\0')
        {
            str_merged[chars]=str1[x];
            x++;
            chars++;
        }
    
        x=0;
    
        while (str2[x]!='\0')
        {
            str_merged[chars]=str2[x];
            x++;
            chars++;
        }
    
        str_merged[chars+1]='\0';
    
        return str_merged;
    }
    crashes my program and i cant seem to figure out why, however if i put a "getch()" anywhere in the function it doesnt crash, for example this:
    Code:
    char* strmerge(const char str1[], char *str2)
    {
        int x=0, chars=0;
        char* str_merged;
    
        getch();
    
        while (str1[x]!='\0')
        {
            str_merged[chars]=str1[x];
            x++;
            chars++;
        }
    
        x=0;
    
        while (str2[x]!='\0')
        {
            str_merged[chars]=str2[x];
            x++;
            chars++;
        }
    
        str_merged[chars+1]='\0';
    
        return str_merged;
    }
    would not crash.

    my main just calls this function, like so:
    Code:
    int main()
    {
           strmerge("hi ", "there");
    }
    i dont see any reason for this function to crash the program, and even more strange is that it gets fixed with a simple randomply placed getch();

    Help pls

  2. #2
    Registered User
    Join Date
    Jul 2010
    Posts
    55
    of course it's gonna crash if you have bad pointers (str_merged), and the getch() simply waits for you to input a character before all hell breaks loose

    Also, you could clean your code by changing this condition (str1[x]!='\0') to this one : (str1[x])
    Last edited by thefeedinghand; 07-30-2010 at 05:09 PM.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    120
    the problem is that when i put the getch() the hell doesnt break loose... at all, when i click a key the program ends normaly, i even tryed to put a cout and the function returns the right strings.

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    55
    just allocate some memory for char* str_merged, using new char[]. And change this str_merged[chars+1]='\0'; to this str_merged[chars]='\0'; so the string is properly ended without any garbage characters.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    Quote Originally Posted by thefeedinghand View Post
    just allocate some memory for char* str_merged, using new char[]. And change this str_merged[chars+1]='\0'; to this str_merged[chars]='\0'; so the string is properly ended without any garbage characters.
    Better yet, pass a char *str_merged as argument to your function.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    120
    Ok, i solved the problem and i think i know why, heres the function now:
    Code:
    char* strmerge(char *str1, char *str2)
    {
        int x=0, chars=0;
        char str_merged[40];
    
        while (str1[x]!='\0')
        {
            str_merged[chars]=str1[x];
            x++;
            chars++;
        }
    
        x=0;
    
        while (str2[x]!='\0')
        {
            str_merged[chars]=str2[x];
            x++;
            chars++;
        }
    
        str_merged[chars]='\0';
    
        return str_merged;
    }
    I think this solved the problem because maybe when i had only the pointer to the first char it would ,at a certain point, overwrite used space on the ram, but correct me if im wrong.

    anyway tks for all your help

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by shiroaisu View Post
    Ok, i solved the problem and i think i know why, heres the function now:
    Code:
    char* strmerge(char *str1, char *str2)
    {
        int x=0, chars=0;
        char str_merged[40];
    
        while (str1[x]!='\0')
        {
            str_merged[chars]=str1[x];
            x++;
            chars++;
        }
    
        x=0;
    
        while (str2[x]!='\0')
        {
            str_merged[chars]=str2[x];
            x++;
            chars++;
        }
    
        str_merged[chars]='\0';
    
        return str_merged;
    }
    I think this solved the problem because maybe when i had only the pointer to the first char it would ,at a certain point, overwrite used space on the ram, but correct me if im wrong.

    anyway tks for all your help
    that's not going to solve anything because you're returning the address of a local stack variable that just went out of scope. Secondly, what do you think will happen if the combined length of your input strings (plus one for the null terminator) is greater than 40?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM