473,405 Members | 2,279 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

why is this bad

char* s=" ";

int main()
{
strcpy(s, "Hello World");
}

This program crashes nicely. I think s is a pointer to a string that
is stored in the executable memory, so the strcpy will overwrite part
of the program, right?

I would not do this, but I found this error and I want to be able to
explain it to the one who wrote it.
Thank you,

--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}

________________________________________
Looking for a good game? Do it yourself!
GLBasic - you can do
www.GLBasic.com
Jul 22 '05 #1
13 1278
I guess the string literal is stored in read-only memory. Try
char sz[256] = " ";
//etc.

Jul 22 '05 #2

"Gernot Frisch" <Me@Privacy.net> wrote in message
news:31*************@individual.net...
char* s=" ";

int main()
{
strcpy(s, "Hello World");
}

This program crashes nicely. I think s is a pointer to a string that is
stored in the executable memory, so the strcpy will overwrite part of the
program, right?

I would not do this, but I found this error and I want to be able to
explain it to the one who wrote it.
Thank you,

--
-Gernot
int main(int argc, char** argv) {printf ("%silto%c%cf%cgl%ssic%ccom%c",
"ma", 58, 'g', 64, "ba", 46, 10);}

________________________________________
Looking for a good game? Do it yourself!
GLBasic - you can do
www.GLBasic.com


String literalts are really of type const char* (even though you declared
yours as just char*), that's probably the reason for the crash.
So when declaring string literals always use type const char* so the
compiler can catch such errors...if you want to modify the string, declare
it as:
char foo[] = { "hello" };

/ WP
Jul 22 '05 #3

"William Payne" <er*************@yahoo.com> wrote in message
news:cp**********@news.island.liu.se...

"Gernot Frisch" <Me@Privacy.net> wrote in message
news:31*************@individual.net...
char* s=" ";

int main()
{
strcpy(s, "Hello World");
}

This program crashes nicely.


String literalts are really of type const char* (even though you declared
yours as just char*), that's probably the reason for the crash.


The interesting bit, of course, is why does C++ type mechanism
allow you to initialize an object of type <char *> with something
that really is a <const char *> ?!?

IMO that's a flaw in C++ type mechanism.

- Risto -
Jul 22 '05 #4

"Risto Lankinen" <rl******@hotmail.com> skrev i en meddelelse
news:fQ*******************@news2.nokia.com...

"William Payne" <er*************@yahoo.com> wrote in message
news:cp**********@news.island.liu.se...

"Gernot Frisch" <Me@Privacy.net> wrote in message
news:31*************@individual.net...
> char* s=" ";
>
> int main()
> {
> strcpy(s, "Hello World");
> }
>
> This program crashes nicely.


String literalts are really of type const char* (even though you declared
yours as just char*), that's probably the reason for the crash.


The interesting bit, of course, is why does C++ type mechanism
allow you to initialize an object of type <char *> with something
that really is a <const char *> ?!?

IMO that's a flaw in C++ type mechanism.

- Risto -

It sure is! Well - it sure isn't! It is a left-over from C, kept for
portability reasons.

/Peter
Jul 22 '05 #5
Risto Lankinen wrote:
String literalts are really of type const char* (even though you declared
yours as just char*), that's probably the reason for the crash.
The interesting bit, of course, is why does C++ type mechanism
allow you to initialize an object of type <char *> with something
that really is a <const char *> ?!?


Usually, it doesn't. You cannot remove constness without a cast, but...
since there is so much C code (and maybe old C++ code) that lets non-const
char* point to string literals, those are an exception to that rule.
IMO that's a flaw in C++ type mechanism.


It's not a bug, it's a feature.

Jul 22 '05 #6
Risto Lankinen wrote:

"William Payne" <er*************@yahoo.com> wrote in message
news:cp**********@news.island.liu.se...

"Gernot Frisch" <Me@Privacy.net> wrote in message
news:31*************@individual.net...
char* s=" ";

int main()
{
strcpy(s, "Hello World");
}

This program crashes nicely.


String literalts are really of type const char* (even though you declared
yours as just char*), that's probably the reason for the crash.


The interesting bit, of course, is why does C++ type mechanism
allow you to initialize an object of type <char *> with something
that really is a <const char *> ?!?

IMO that's a flaw in C++ type mechanism.


It is, but it is there for a (once) good reason:
For compatibility with its ancestor: C
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #7
>> IMO that's a flaw in C++ type mechanism.

It is, but it is there for a (once) good reason:
For compatibility with its ancestor: C


IMO: if it's bound to crash, it should give a warning at compile time.
Jul 22 '05 #8

Gernot Frisch wrote:
IMO that's a flaw in C++ type mechanism.


It is, but it is there for a (once) good reason:
For compatibility with its ancestor: C


IMO: if it's bound to crash, it should give a warning at compile

time.

It does - at least at all compilers I use. You probably have your
warning level set way too low - but if you're maintaining older
code, that may be required to ignore all the bugs in the code.
Regards,
Michiel Salters

Jul 22 '05 #9
William Payne wrote:
char* s=" ";

int main()
{
strcpy(s, "Hello World");
}

This program crashes nicely. I think s is a pointer to a string that is
stored in the executable memory, so the strcpy will overwrite part of the
program, right?

String literalts are really of type const char* (even though you declared
yours as just char*), that's probably the reason for the crash.


Firstly, string literals in C++ are really of type 'const char[N+1]'
(where N is the length of the string), not 'const char*'

Secondly, casting away constness and modifying anything through a
pointer of type 'const char*' alone is not enough to cause a crash.
Whether the crash (or more formally, undefined behavior) will occur
depends solely on the modifiability of the object pointed by the pointer.

In this particular case the object pointed by the pointer is not
modifiable. That's why the code causes undefined behavior (manifested as
a crash is this case).

--
Best regards,
Andrey Tarasevich
Jul 22 '05 #10
Gernot Frisch wrote:
IMO that's a flaw in C++ type mechanism.


It is, but it is there for a (once) good reason:
For compatibility with its ancestor: C


IMO: if it's bound to crash, it should give a warning at compile time.
...


It is not necessarily bound to crash. For example, forceful removal of
constness is a well-established implementational idiom in C language and
no one is saying that it is "bound to crash". There's no reason why it
should suddenly be "bound to crash" in C++. One just needs to learn to
use such things with proper care. The real problem here is that some
people use such features without even noticing, without understanding
what they've just done.

Don't get me wrong though, I'm all against this deprecated feature of
C++ language. I'm just against perceiving such things as something
necessarily "bound to crash".

--
Best regards,
Andrey Tarasevich
Jul 22 '05 #11
On Tue, 07 Dec 2004 10:44:27 GMT, "Risto Lankinen"
<rl******@hotmail.com> wrote in comp.lang.c++:

"William Payne" <er*************@yahoo.com> wrote in message
news:cp**********@news.island.liu.se...

"Gernot Frisch" <Me@Privacy.net> wrote in message
news:31*************@individual.net...
char* s=" ";

int main()
{
strcpy(s, "Hello World");
}

This program crashes nicely.


String literalts are really of type const char* (even though you declared
yours as just char*), that's probably the reason for the crash.


The interesting bit, of course, is why does C++ type mechanism
allow you to initialize an object of type <char *> with something
that really is a <const char *> ?!?

IMO that's a flaw in C++ type mechanism.

- Risto -


As others have pointed out, it is for comparability with existing C
code. And as for why C allowed and still allows it, there were string
literals in C for about 15 years before there was a 'const' keyword.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #12
On Tue, 07 Dec 2004 11:00:41 -0800, Andrey Tarasevich
<an**************@hotmail.com> wrote in comp.lang.c++:
Gernot Frisch wrote:
IMO that's a flaw in C++ type mechanism.

It is, but it is there for a (once) good reason:
For compatibility with its ancestor: C


IMO: if it's bound to crash, it should give a warning at compile time.
...


It is not necessarily bound to crash. For example, forceful removal of
constness is a well-established implementational idiom in C language and
no one is saying that it is "bound to crash". There's no reason why it
should suddenly be "bound to crash" in C++. One just needs to learn to
use such things with proper care. The real problem here is that some
people use such features without even noticing, without understanding
what they've just done.

Don't get me wrong though, I'm all against this deprecated feature of
C++ language. I'm just against perceiving such things as something
necessarily "bound to crash".


There is no forceful removal of "constness" involved when this code is
compiled as C.

Unlike C++, in C the type of a string literal is NOT "array of const
char", it is "array of char". So there is no need to remove the const
qualifier, because it neither exists nor is implied.

Attempting to modify a string literal in C is undefined behavior not
because the chars are const, but because the C standard specifically
states that it is undefined.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #13
Jack Klein wrote:
>>> IMO that's a flaw in C++ type mechanism.
>>
>> It is, but it is there for a (once) good reason:
>> For compatibility with its ancestor: C
>
> IMO: if it's bound to crash, it should give a warning at compile time.
> ...


It is not necessarily bound to crash. For example, forceful removal of
constness is a well-established implementational idiom in C language and
no one is saying that it is "bound to crash". There's no reason why it
should suddenly be "bound to crash" in C++. One just needs to learn to
use such things with proper care. The real problem here is that some
people use such features without even noticing, without understanding
what they've just done.

Don't get me wrong though, I'm all against this deprecated feature of
C++ language. I'm just against perceiving such things as something
necessarily "bound to crash".


There is no forceful removal of "constness" involved when this code is
compiled as C.


I'm not saying that there is one in this particular code. All I'm trying
to say is that existence of a modification-allowing access path to
non-modifiable data does not necessarily lead to a problem. It is not a
good thing most of the time, but it has its uses.

--
Best regards,
Andrey Tarasevich
Jul 22 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: James | last post by:
I have a from with 2 fields: Company & Name Depening which is completed, one of the following queries will be run: if($Company){ $query = "Select C* From tblsample Where ID = $Company...
5
by: Scott D | last post by:
I am trying to check and see if a field is posted or not, if not posted then assign $location which is a session variable to $location_other. If it is posted then just assign it to...
2
by: Nick | last post by:
Can someone please tell me how to access elements from a multiple selection list? From what ive read on other posts, this is correct. I keep getting an "Undefined variable" error though... Form...
2
by: Alexander Ross | last post by:
I have a variable ($x) that can have 50 different (string) values. I want to check for 7 of those values and do something based on it ... as I see it I have 2 options: 1) if (($x=="one") ||...
0
by: Dan Foley | last post by:
This script runs fine, but I'd like to know why it's so slow.. Thanks for any help out there on how i can make it faster (it might take up to 5 min to write these 3 export files whith 15 records...
5
by: Lee Redeem | last post by:
Hi there I've created abd uploaded this basic PHP script: <html> <head> <title>PHP Test</title> </head> <body> <H1 align="center">
5
by: christopher vogt | last post by:
Hi, i'm wondering if there is something like $this-> to call a method inside another method of the same class without using the classname in front. I actually use class TEST { function...
6
by: Phil Powell | last post by:
Ok guys, here we go again! SELECT s.nnet_produkt_storrelse_navn FROM nnet_produkt_storrelse s, nnet_produkt_varegruppe v, nnet_storrelse_varegruppe_assoc sv, nnet_produkt p WHERE...
1
by: Michel | last post by:
a site like this http://www.dvdzone2.com/dvd Can you make it in PHP and MySQL within 6 weeks? If so, send me your price 2 a r a (at) p a n d o r a . b e
11
by: Maciej Nadolski | last post by:
Hi! I can`t understand what php wants from me:( So: Cannot send session cache limiter - headers already sent (output started at /home/krecik/public_html/silnik.php:208) in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.