pete wrote:
>
pete wrote:
pete wrote:
>
pa********@hotmail.com wrote:
Why is gcc allowing the following compilations
#include <stdio.h>
#include <stdlib.h>
main(){
const char* str;
str =(char*) malloc(12);
strcpy(str,"partas");
*str= 'q';
printf(" the string is %s\n", str);
}
It is compiling with a warning only .
>
Only one?
>
why is it not giving a compile time error
>
Because the compiler is allowed to do
whatever it wants to do, with code like that.
>
N869
6.7.3 Type qualifiers
>
[#5] If an attempt is made to modify an object defined with
a const-qualified type through use of an lvalue with non-
const-qualified type, the behavior is undefined.
I don't think that's the right reason.
*str isn't an lvalue with non-const-qualified type
OK, here it is.
N869
6.3.2.1 Lvalues and function designators
[#1]...
A modifiable lvalue is an lvalue that does not
have array type, does not have an incomplete type, does not
have a const-qualified type...
6.5.16 Assignment operators
[#2] An assignment operator shall have a modifiable lvalue
as its left operand.
The explanation needs these parts too:
N869
3. Terms and definitions
3.18
[#1] undefined behavior
behavior, upon use of a nonportable or erroneous program
construct, of erroneous data, or of indeterminately valued
objects, for which this International Standard imposes no
requirements
4. Conformance
[#1] In this International Standard, ``shall'' is to be
interpreted as a requirement on an implementation or on a
program; conversely, ``shall not'' is to be interpreted as a
prohibition.
[#2] If a ``shall'' or ``shall not'' requirement that
appears outside of a constraint is violated, the behavior is
undefined. Undefined behavior is otherwise indicated in
this International Standard by the words ``undefined
behavior'' or by the omission of any explicit definition of
behavior. There is no difference in emphasis among these
three; they all describe ``behavior that is undefined''.
--
pete