access violation with strcat() and strncat

This is a discussion on access violation with strcat() and strncat within the C++ Programming forums, part of the General Programming Boards category; Why on earth am I getting access violations from the following two prorams, the first is using strcat(), and the ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    3

    access violation with strcat() and strncat

    Why on earth am I getting access violations from the following two prorams, the first is using strcat(), and the last one is using strncat()....

    // code
    #include <iostream.h>
    #include <string.h>

    int main()
    {
    char *FName = "John";
    char *LName = "Doe";


    strcat(FName, LName);

    cout << FName << endl;


    return 0;
    }


    // code

    #include <iostream.h>
    #include <string.h>

    int main()
    {
    char *FName = "John";
    char *LName = "Doe";

    strncat(FName, LName, 3); // will only print the first 3 char's

    cout << " " << FName << endl;


    return 0;
    }

    any ideas?

  2. #2
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Because you aren't allocating enough memory to hold the bigger string.

    Code:
    #include <iostream.h>
    #include <string.h>
    
    int main()
    {
        char FName[8] = "John";
        char *LName = "Doe"; 
    
        strcat(FName, LName);
        cout << FName << endl;
    
        return 0;
    }

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    3
    my bad, thanks

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>Because you aren't allocating enough memory to hold the bigger string.
    You mean no memory. This:
    >char *FName = "John";
    is not safe to modify as its a pointer to data that could be in read only memory. You need to use an array, like Poly showed.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Yeah, I think they should make it a syntax error when you do that.

    It should only be valid to do something like:

    const char* String = "Blah";

    That would make a lot more sense

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    It should only be valid to do something like:
    const char* String = "Blah";
    This would make a great deal of sense, but it would break Soooooo much code.
    It's even more fun because when the compiler lays out space for the string litterals it likes to stick them just before all the sensitive fiddily bits.

  7. #7
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by grib
    This would make a great deal of sense, but it would break Soooooo much code.
    Yeah, it should've been like that from the start. Doesn't make any sense why it wasn't. Too late now, unfortunately.

  8. #8
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Embrace C++!
    Code:
    #include <string>
    
    // ...
    
    using std::string;
    string fname = "John";
    fname += "Doe";
    Now you don't have to worry about any of that bullcock.

  9. #9
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    There's no fun in that!

Popular pages Recent additions subscribe to a feed

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