CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2001
    Posts
    1,029

    why am I crashing when I try to strcpy into a char* ?

    I have:
    char* filename;
    filename[0] = '\0';
    filename = "untitled.cis";

    char filename2[256];
    filename2[0] = '\0';
    strcpy(filename2, dlg.GetFileTitle());
    filename = ""; // to clear it out?
    strcpy(filename, filename2);

    it crashes on this last line.... why?

    Thanks!

  2. #2
    Join Date
    Dec 2001
    Location
    Israel
    Posts
    30
    Hi,
    char* filename;

    is a pointer u need to allocate in order to use it.
    you have generated an access violation.

    to make it work

    char filename[MAX_PATH]="\0";
    strcpy( filename, "untitled.cis" );

    char filename2[MAX_PATH]="\0";
    strcpy( filename2, dlg.GetFileTitle() );

    // to clear it out?
    memset( filename , 0, MAX_PATH*sizeof(char) );

    strcpy( filename, filename2 );

  3. #3
    Join Date
    Jul 2001
    Location
    UK
    Posts
    329

    Thumbs down

    Aaaarrrggghhhh.

    How can ANYONE code like that!

    I reckon it is someone who learnt C++ first, and then tried to code strings the old fashioned C way

  4. #4
    Join Date
    Dec 2001
    Location
    Israel
    Posts
    30

    Wink

    What do u mean?!?
    If U do not ask u cann't know can u


  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: why am I crashing when I try to strcpy into a char* ?

    Originally posted by lab1
    I have:
    char* filename;
    filename[0] = '\0';
    filename = "untitled.cis";

    char filename2[256];
    filename2[0] = '\0';
    strcpy(filename2, dlg.GetFileTitle());
    filename = ""; // to clear it out?
    strcpy(filename, filename2);

    it crashes on this last line.... why?

    Thanks!
    If this is 'C', then the solution provided by Avi is satisfactory. If this is a C++ program, you could do a lot better than this. But first, here are the things wrong with your program:

    1) How do you know that the file title will be less than 256 characters? The first strcpy() will blow up if you happen to get a file > 256 characters.

    2) The filename = "untitled.cis" and filename = "" line: These assign pointers to a string literal to filename. A string literal is implied to be a const char * (i.e. the buffer being pointed to shouldn't be modified). It is undefined behavior when you assign to a constant, which is what you are doing in the last strcpy(). This is why it blows up. Yes, "filename" may have originally been declared as a "char *", but when you assign a string literal to it, you cannot (or more accurately, shouldn't) write to it.

    Note that if you ran this program on another compiler (or maybe even another version of VC++), the strcpy() may not have blown up. This is the sign of undefined behavior, so you never write constructs that invoke it. Writing to a string literal is one such way of invoking undefined behavior.

    Here is an alternative, C++ solution, without the errors that you are getting now:
    Code:
    #include <string>
    std::string filename;
    filename = "untitled.cis";
    
    std::string filename2;
    filename2 = dlg.GetFileTitle();
    filename = filename2;
    a) There is no danger of filename2 being too small for the return of dlg.GetFileTitle(). The std::string class is a dynamic string, and can accommodate strings of varying sizes.

    b) You assign to a string with a simple "=". There is no need for strcpy() or messy char * pointers to deal with.

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured