copy strings into an arbitrary location !

Discussion in 'C++' started by alkhoudri, Aug 26, 2006.

  1. alkhoudri

    alkhoudri Guest

    Hello everybody,

    I am working on a project which contains in one of its files a code
    like the following :

    char * CurrentString = "\0";

    void function01()
    {
    ...
    strcpy(CurrentString, "aString");
    ...
    }

    And the application doesn't crash !!!
    Do you have any idea why ?
    For me, it's just out of the question to copy a string in an
    unallocated space (pointed to by CurrentString in the above example).

    For info, the compiler is CodeWarior for an embedded system.
    Thank you in advance.
     
    alkhoudri, Aug 26, 2006
    #1
    1. Advertising

  2. alkhoudri

    mlimber Guest

    alkhoudri wrote:
    > Hello everybody,
    >
    > I am working on a project which contains in one of its files a code
    > like the following :
    >
    > char * CurrentString = "\0";
    >
    > void function01()
    > {
    > ...
    > strcpy(CurrentString, "aString");
    > ...
    > }
    >
    > And the application doesn't crash !!!
    > Do you have any idea why ?
    > For me, it's just out of the question to copy a string in an
    > unallocated space (pointed to by CurrentString in the above example).


    You got lucky. What you've done is invoked undefined behavior, which
    might mean a crash or subtle corruption of your other data or the end
    of life on earth. It's *really* undefined, and you shouldn't do it for
    that reason. Moreover, you should use std::string if you can (yes, I
    know you're on an embedded system, but so am I; lots of embedded
    environments can thrive with the standard library). At the very least,
    you should use the safer strncpy.

    Cheers! --M
     
    mlimber, Aug 26, 2006
    #2
    1. Advertising

  3. alkhoudri posted:

    > char * CurrentString = "\0";



    It's illegal to alter a string literal; in light of this, it's best to use:

    char const *CurrentString = "\0";


    > void function01()
    > {
    > ...
    > strcpy(CurrentString, "aString");
    > ...
    > }
    >
    > And the application doesn't crash !!!
    > Do you have any idea why ?



    You're writing to memory which isn't yours to write to. Either of these
    things will happen:

    (1) The system will deny the request and continue on.
    (2) The system will deny the request and terminate the program.
    (3) The system will allow the request and continue on.
    (4) The system will allow the request and the program will crash.
    (5) Internet Explorer will be launched and it will go to
    www.howstuffworks.com.


    Bottom line: Undefined Behaviour.


    > For me, it's just out of the question to copy a string in an
    > unallocated space (pointed to by CurrentString in the above example).



    The Standard agrees.


    --

    Frederick Gotham
     
    Frederick Gotham, Aug 26, 2006
    #3
  4. alkhoudri

    Default User Guest

    mlimber wrote:

    > alkhoudri wrote:
    > > Hello everybody,
    > >
    > > I am working on a project which contains in one of its files a code
    > > like the following :
    > >
    > > char * CurrentString = "\0";
    > >
    > > void function01()
    > > {
    > > ...
    > > strcpy(CurrentString, "aString");
    > > ...
    > > }
    > >
    > > And the application doesn't crash !!!


    > You got lucky.


    You mean unlucky.




    Brian (crashes are help you didn't deserve)
     
    Default User, Aug 26, 2006
    #4
  5. alkhoudri

    alkhoudri Guest

    I wasn't excepting such detailed answers.
    Thank you very much all.


    Frederick Gotham a écrit :

    > alkhoudri posted:
    >
    > > char * CurrentString = "\0";

    >
    >
    > It's illegal to alter a string literal; in light of this, it's best to use:
    >
    > char const *CurrentString = "\0";
    >
    >
    > > void function01()
    > > {
    > > ...
    > > strcpy(CurrentString, "aString");
    > > ...
    > > }
    > >
    > > And the application doesn't crash !!!
    > > Do you have any idea why ?

    >
    >
    > You're writing to memory which isn't yours to write to. Either of these
    > things will happen:
    >
    > (1) The system will deny the request and continue on.
    > (2) The system will deny the request and terminate the program.
    > (3) The system will allow the request and continue on.
    > (4) The system will allow the request and the program will crash.
    > (5) Internet Explorer will be launched and it will go to
    > www.howstuffworks.com.
    >
    >
    > Bottom line: Undefined Behaviour.
    >
    >
    > > For me, it's just out of the question to copy a string in an
    > > unallocated space (pointed to by CurrentString in the above example).

    >
    >
    > The Standard agrees.
    >
    >
    > --
    >
    > Frederick Gotham
     
    alkhoudri, Aug 29, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Luke Dalessandro
    Replies:
    0
    Views:
    633
    Luke Dalessandro
    Jan 15, 2006
  2. Honestmath
    Replies:
    5
    Views:
    590
    Honestmath
    Dec 13, 2004
  3. Alex
    Replies:
    2
    Views:
    1,289
  4. Replies:
    26
    Views:
    2,177
    Roland Pibinger
    Sep 1, 2006
  5. akindo
    Replies:
    4
    Views:
    502
Loading...

Share This Page