Thread: strcat in windows crashes program

  1. #1
    Registered User Diamonds's Avatar
    Join Date
    Oct 2002
    Posts
    68

    Question strcat in windows crashes program

    Code:
    #include <windows.h>
    #include <string.h>
    
    char * one = "hello ";
    char * two = "world";
    
    int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int) 
    {
    	MessageBox(NULL, strcat(one,two), "Sample", MB_OK);
    	return 0; 
    }
    This will compile but crash when it's run. Why?

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    because there is not enough space in the string 'one' to hold both words.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    Registered User Diamonds's Avatar
    Join Date
    Oct 2002
    Posts
    68
    o, danit
    Last edited by Diamonds; 12-04-2002 at 01:37 AM.

  4. #4
    Registered User Diamonds's Avatar
    Join Date
    Oct 2002
    Posts
    68
    wait, what does it return then? a pointer to string 1 ?

  5. #5
    Registered User Diamonds's Avatar
    Join Date
    Oct 2002
    Posts
    68
    did a search, found the answer

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Not that I would ever defend windows. But why blame windows for code that shouldn't work on any OS?

  7. #7
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Can you really modify those strings? Aren't they constant?

    Wouldn't you need to do this at least?:

    char one[64] = "Hello";
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  8. #8
    Registered User Penguin of Oz's Avatar
    Join Date
    Dec 2002
    Posts
    16
    Also, some people may argue it's nicer to use sprintf(...);:

    Code:
    char one[64] = { "hello " };
    char two[6] = { "world" };
    
    sprintf(one, "%s%s", one, two);
    MessageBox(NULL, one, "Meep", MB_OK | MB_ICONINFORMATION);
    Just a thought.
    "I don't think there's anything else I can do... my shoes are tied"

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>some people may argue it's nicer to use sprintf(...);
    strncat() would be a better choice, imho.

    >>sprintf(one, "%s%s", one, two);
    Here you are using "one" as both a target and a source pointer, I'm going to guess that is undefined behaviour.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Registered User Penguin of Oz's Avatar
    Join Date
    Dec 2002
    Posts
    16
    I had my doubts about that working, I appreciate it's messy, but Dev-C++/cygwin/mingw32 lapped it up nicely.

    Personally, I usually keep a char szMessage[256]; or something in any functions that need to use message boxes with variables in.
    "I don't think there's anything else I can do... my shoes are tied"

  11. #11
    Registered User johnnie2's Avatar
    Join Date
    Aug 2001
    Posts
    186
    If you really wanted to force that to work, a plausible way would be:

    Code:
    char *one = new char[65];
    memset(one, 0, 65);
    memcpy(one, "hello", 5);
    
    char *two = new char[6];
    memset(two, 0, 6);
    memcpy(two, "world", 5);
    
    sprintf(one + strlen(one), "%s", two);
    MessageBox(NULL, one, "Meep", MB_OK | MB_ICONINFORMATION);
    Which would produce "helloworld".
    Last edited by johnnie2; 12-04-2002 at 09:17 PM.
    "Optimal decisions, once made, do not need to be changed." - Robert Sedgewick, Algorithms in C

  12. #12
    Registered User
    Join Date
    Dec 2002
    Posts
    119
    Quote
    Code:
    #include <windows.h>
    #include <string.h>
    
    char * one = "hello ";
    char * two = "world";
    
    int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int) 
    {
    	MessageBox(NULL, strcat(one,two), "Sample", MB_OK);
    	return 0; 
    }
    Here's how to do it (IMHO)
    Code:
    #include <windows.h>
    #include <string.h>
    
    char  one[30] = "hello ";
    char * two = "world";
    
    int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int) 
    {
            strcat(one, two);
    	MessageBox(NULL, one, "Sample", MB_OK);
    	return 0; 
    }

  13. #13
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    strncat() is much safer than strcat() and sprintf() for this, you should protect your buffers
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  14. #14
    Registered User
    Join Date
    Dec 2002
    Posts
    119
    strncat() should be used when you can't guarantee that the concatonated string will fit in your destination buffer. In this case we have complete control over the length of the strings and the size of the destination buffer, therefore no need for strncat(). Another use for strncat() would be if you want to copy only a certain number of chars or if the source string doesn't have a terminating null.

    -Futura

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 01-01-2007, 07:36 AM
  2. Replies: 2
    Last Post: 12-22-2006, 08:45 PM
  3. Virtual keys
    By Arkanos in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2005, 10:00 AM
  4. my first windows console program
    By Syneris in forum Windows Programming
    Replies: 3
    Last Post: 04-08-2002, 03:18 PM
  5. Program crashes and I can't figure out why
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 09-19-2001, 05:33 PM