• Advertisement
Sign in to follow this  

switch for controlling string literals

This topic is 4285 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Actually I am using -Wall -ansi -pedantic in DEV...so I was thinking that -traditional might be included in that.

Share this post


Link to post
Share on other sites
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?


Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement