This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Compiling GCC With a C++ Compiler (g++)


dk@artimi.com (Dave Korn)  wrote on 14.10.04 in <NUTMEGD3zCParqjPikM00000488@NUTMEG.CAM.ARTIMI.COM>:

>   Wow, we really seem to me to be entering the realm of the bizarre here.

Not really.

> What about this function: is it valid, conformant C?
>
> void fubar (const char *x)
> {
>     *(char *)x = '?';
> }
>
>   Is it not implicit from what you've just told me that the answer to the
> question of whether or not that function is valid conformant C depends on
> whether the value of the pointer you pass into it was originally derived
> from a call to malloc (valid) or by taking the address-of a local or static
> variable (not valid)?

Nope, malloc or variable is the wrong distinction.

The argument is really very simple. Essentially, it reduces to where x  
points.

x == NULL                           -> invalid
x points into nonmodifiable storage -> invalid
x points into modifiable storage    -> valid

Rule of thumb: assuming the compiler (and os) did this optimally right,  
it's invalid exactly if it would get a SIGSEGV. Or at least that seems to  
be the intention behind these rules.

Of course, the closest C can come to this is to look if the object was  
defined with const. (malloc() results are never defined with const.)  
That's defined, not declared or cast to, and that's the object, not a  
pointer to it, or something the object is embedded in. (But obviously, if  
you can't modify a subobject, then you can't modify the object AS A WHOLE  
either, as that would modify the subobject.)

MfG Kai


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]