Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > copy strings into an arbitrary location !

Reply
Thread Tools

copy strings into an arbitrary location !

 
 
alkhoudri
Guest
Posts: n/a
 
      08-26-2006
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.

 
Reply With Quote
 
 
 
 
mlimber
Guest
Posts: n/a
 
      08-26-2006
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

 
Reply With Quote
 
 
 
 
Frederick Gotham
Guest
Posts: n/a
 
      08-26-2006
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
 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      08-26-2006
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)

 
Reply With Quote
 
alkhoudri
Guest
Posts: n/a
 
      08-29-2006

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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Iterating Over Dictionary From Arbitrary Location akindo Python 4 06-07-2009 08:25 PM
Location, location, location =?Utf-8?B?VHJhY2V5?= Wireless Networking 2 02-17-2007 08:37 PM
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
How to trick a page into running at a location different from it'sphysical location? Luke Dalessandro ASP .Net 0 01-15-2006 05:59 AM
calling an arbitrary function w/ arbitrary arguments Honestmath C++ 5 12-13-2004 06:18 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57