1753516 Members
5304 Online
108795 Solutions
New Discussion юеВ

Ansi C compiler warning

 
SOLVED
Go to solution
Andrew Blainey
New Member

Ansi C compiler warning

I'm using the Ansi C compiler to build my programs. There is a switch "+w1" which displays additional warnings. This switch is very useful for ensuring prototypes have been used, etc...
However, using this switch with code that contains const always produces the following warning:
"cc: warning 474: By default 'const' qualified objects are now stored in literal space. This may lead to run-time errors if the semantics of 'const' are violated.
Is this an indication of a problem in the code? I don't think it is. If not is there a way to suppress this warning?
2 REPLIES 2
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Ansi C compiler warning

Yes, you can add the +W 474 flag to suppress that warning. You can actually specify a comman separated list of suppressed warning numbers after the '+W' argument.

That warning is telling you that make sure you treat const as const in all files that may contain the specified variable; i.e. don't do anything in another source file to change it's value.
If it ain't broke, I can fix that.
Rich Homa
New Member

Re: Ansi C compiler warning

This can actually be a warning that requires a coding change. It came up in the course of a code migration that we're going through, and I wrote the following to the other people on the project:

Apparently, the 11.0 compiler will treat some literals as variables of type const even if you don't explicitly declare them as such, but it will do so in a way that takes effect at execution time only: rather than terminating the compilation, the compiler simply issues a warning

By default 'const' qualified objects are now stored in literal space using 474. This may lead to run-time errors if the semantics of 'const' are violated

and then when "the semantics of 'const' are violated" you get a bus error. Not a core dump, just a bus error. (To make the warning even more helpful, it doesn't always appear, and when it does there's no line number associated with it.).

The only good thing about the problem is that it only seems to arise with code of a sort that I hope doesn't get written very much. For example:

#include
#include
main(int argc, char *argv[])
{
void OverlayLiteral(char *);
OverlayLiteral("ORIGINALSTRING");
return(EXIT_SUCCESS);
}
void OverlayLiteral(char * arg)
{
sprintf(arg,"%s","NEWSTRING");
printf("arg is %s\n",arg);
}

On 11.0, the sprintf marked in red will trigger a bus error, even though it will execute without any problem on 10.2 and fall through to print "arg is NEWSTRING". Apparently, 11.0 saves "ORIGINALSTRING" in some kind of protected location.