switch for controlling string literals

Started by
6 comments, last by TEUTON 17 years, 10 months ago
I was reading comp.lamg.c FAQ. Came across this Some compilers have a switch controlling whether string literals are writable or not (for compiling old code)... I just wanted to know does any of the version of VC has that switch. I want compiler to catch the error at compile time when we try to modify string literals. Plz tell me is there any switch for DEV C++(gcc) also?
Advertisement
Quote:Original post by TEUTON
I just wanted to know does any of the version of VC has that switch. I want compiler to catch the error at compile time when we try to modify string literals.

There is no compiler (that I'm aware of, at least) can tell you if you try to modify a string at compile time.

On an x86 system, usually what happens is if the switch is enabled, strings are put into a .rodata section in the EXE. This section is then set to read-only, and under Windows, mapped such that any write to the pages it contains will raise an exception. Whether that exception is handled or ignored is a different story all together...

Interestingly, I can't seem to find a switch for it in VS2005. Perhaps VC6 or VS2003 have support for it?
See this
http://www.la.utexas.edu/lab/software/devtool/gnu/gcc/gcc_7.html

It talks about -traditional

but I want opposite of that.
I would presume that simply not specifying -traditional would be the opposite. :)
Actually I am using -Wall -ansi -pedantic in DEV...so I was thinking that -traditional might be included in that.
Quote:Original post by bpoint
On an x86 system, usually what happens is if the switch is enabled, strings are put into a .rodata section in the EXE...

What switch are you talking about?


I was speaking in general.

I can't seem to find a particular switch for gcc to automatically put strings into the .rodata section, but it is possible to explicitly declare them in code:
static const char myString[] __attribute__ ((section (".rodata"))) = "Hello World!";

You will, of course, have to declare the string as a const.
So, basically we need to be const correct.

This topic is closed to new replies.

Advertisement