473,386 Members | 1,846 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,386 software developers and data experts.

No compile-time error on modifying string literals

Hello.

The following is a summary of what I have understood from reading
Stroustrup's TCPL 3rd Edition's discourse on "string literals" (5.2.2).

String literals are of type const char[n], where n is the length of the
literal plus one. However, for compatibility reasons, assignment of such
a literal to a non-const char* is allowed, e.g.:

char* name = "Ney Zunino";

In spite of that assignment being allowed, it is not permitted to use
that pointer to modify the string literal. Next, Stroustrup adds that
this kind of error cannot generally be caught until runtime. As a matter
of fact, that's precisely what I have experienced with the following
test program:

int main()
{
char* s = "Ney André de Mello Zunino";
s[2] = 'i';
}

Both Microsoft Visual C++ 2003 and g++ 3.3.3 compiled it quietly and
both executables caused protection faults at runtime. Fine, it all went
just like Stroustrup had said, but what I don't understand is why. Why
is it so hard for a compiler to catch such violation? Isn't it clear
that the address assigned to 's' was that of a statically-allocated literal?

Thank you for your comments,

--
Ney André de Mello Zunino
Jul 23 '05 #1
4 1438
Ney André de Mello Zunino wrote:
The following is a summary of what I have understood from reading
Stroustrup's TCPL 3rd Edition's discourse on "string literals" (5.2.2).

String literals are of type const char[n], where n is the length of the
literal plus one. However, for compatibility reasons, assignment of such
a literal to a non-const char* is allowed, e.g.:

char* name = "Ney Zunino";

In spite of that assignment being allowed, it is not permitted to use
that pointer to modify the string literal. Next, Stroustrup adds that
this kind of error cannot generally be caught until runtime. As a matter
of fact, that's precisely what I have experienced with the following
test program:

int main()
{
char* s = "Ney André de Mello Zunino";
s[2] = 'i';
}

Both Microsoft Visual C++ 2003 and g++ 3.3.3 compiled it quietly and
both executables caused protection faults at runtime. Fine, it all went
just like Stroustrup had said, but what I don't understand is why. Why
is it so hard for a compiler to catch such violation? Isn't it clear
that the address assigned to 's' was that of a statically-allocated
literal?


Why bother catching it if the Standard doesn't require catching it? The
fact that you're trying to modify a literal becomes much less clear if
you pass the pointer into another function which will modify the contents
and even less clear if that function is in another translation unit. So,
instead of requiring the implementations to watch out for those, the C++
Standard simply says that an attempt to modify leads to undefined
behavoiur. It's up to you as a programmer to prevent it. There are tools
that go the extra mile to try to fish out those mistakes. Are they good?
Those who use them can probably tell. I don't use them. I simply don't
write code like in your example.

V
Jul 23 '05 #2
Ney André de Mello Zunino wrote:


Both Microsoft Visual C++ 2003 and g++ 3.3.3 compiled it quietly and
both executables caused protection faults at runtime. Fine, it all went
just like Stroustrup had said, but what I don't understand is why. Why
is it so hard for a compiler to catch such violation? Isn't it clear
that the address assigned to 's' was that of a statically-allocated literal?


In this case: yes.
In the general case: no

void foo( char* check )
{
check[2] = 'n';
}

Now, does the above function attempt to modify a string literal?
Nobody knows until runtime, when the caller of that function
passes an address.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #3
Karl Heinz Buchegger wrote:

[...]
void foo( char* check )
{
check[2] = 'n';
}

Now, does the above function attempt to modify a string literal?
Nobody knows until runtime, when the caller of that function
passes an address.


Of course you and Victor are right. Thanks for clarifying it.

--
Ney André de Mello Zunino
Jul 23 '05 #4

Karl Heinz Buchegger wrote:
Ney André de Mello Zunino wrote:


Both Microsoft Visual C++ 2003 and g++ 3.3.3 compiled it quietly and both executables caused protection faults at runtime. Fine, it all went just like Stroustrup had said, but what I don't understand is why. Why is it so hard for a compiler to catch such violation? Isn't it clear that the address assigned to 's' was that of a statically-allocated
literal?
In this case: yes.
In the general case: no

void foo( char* check )
{
check[2] = 'n';
}

Now, does the above function attempt to modify a string literal?
Nobody knows until runtime, when the caller of that function
passes an address.


To make the point more obvious, that same function could look like
this:

void foo(char check[])
{
check[2] = 'n';
}

and it wouldn't make a bit of difference in the end.

On the other hand, if your literals are declaired const the compiler
will generate a warning if you pass them as non-const parameters:

const char *x = "ABCD";
foo(x);

will generate a warning complaining about the discarding of const in
the call to foo...might even error in C++, not sure. The const keyword
will protect you in many ways.

Jul 23 '05 #5

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

Similar topics

12
by: Ghislain Benrais | last post by:
Hello, My "problem" : install php's XSLT API. 1)I installed Sablotron (with the rpm) : ok 2) I read every where that the next step was to compile php with option --with-xslt-sablot. Since I have...
1
by: Zean Smith | last post by:
I am working on a project to migrate a JavaBean + JSP application (running on Oracle application server) to Microsoft ASP.NET + SQL server. In order to see how the original code works, I need to...
12
by: jrefactors | last post by:
If the C programs have UNIX system calls such as fork(), alarm(), etc.., we should call it UNIX programs, not traditional C programs? We couldn't compile the programs with system calls using VC++...
8
by: Davy | last post by:
Hi all, I use VC and gcc/gdb to compile and debug C/C++ files. But I found some of the debug version of the compiled files are too large to be run in a small RAM. Can I compile C/C++ Debug...
10
by: Jean-David Beyer | last post by:
I have some programs running on Red Hat Linux 7.3 working with IBM DB2 V6.1 (with all the FixPacks) on my old machine. I have just installed IBM DB2 V8.1 on this (new) machine running Red Hat...
5
by: arkam | last post by:
Hi, I have ported an application from Unix on Windows. My problem are the compile times : UNIX (2x450MHz / 512MB RAM) Full compile with clean : 1h30 WINDOWS 2000 (P4 1.7GHz / 758MB RAM)
7
by: Arne | last post by:
I am porting a website to ASP.net 2.0. Temporarily I compile with Visual Studio 2003 and deploying to ASP.net 2.0. How do I compile my website under ASP.Net 2.0? I know it can compile each page as...
0
by: Fuzzyman | last post by:
Hello all, The following is a copy of a blog entry. It's asking a question about future statements and the built in compile function. I'd appreciate any pointers or comments about possible...
3
by: NvrBst | last post by:
Right now I have C99 code in .c extensions. I compile it in VSC++ and it complains about a lot of errors. I change the extensions to .cpp and compile in VSC++ and it succeeds. Is there a way...
4
by: Salad | last post by:
I have a database called MyApp.MDB. When I go into a module and want to compile the thing, it might have, on the menu bar line "Compile MyAppTest". IOW, the app name it's about to compile does...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.