tinycc-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Tinycc-devel] char points and char array


From: Dave Dodge
Subject: Re: [Tinycc-devel] char points and char array
Date: Wed, 23 Dec 2009 22:38:02 -0500
User-agent: Mutt/1.5.17 (2008-05-15)

On Thu, Dec 24, 2009 at 09:58:50AM +0800, ???? wrote:
> char *s='hello,world';
> *(s+2) = '\0';
> 
> which in ansi C are not allowed cause *s is consider as read only

The exact wording is "if the program attempts to modify such an array,
the behavior is undefined".

This is not a constraint violation, so the compiler is not required to
detect the error or enforce any particular outcome.  Because the
behavior is undefined, _any_ result is considered valid, including
placing the string literal in writable storage and modifying its
content.  The compiler can assume that no valid program will ever
contain code such as the above, and it is the programmer's
responsibilty to avoid writing undefined code in the first place.

> and i compile the code with gcc it show me segment error while running
> but tcc accept this act

That's because gcc normally places string literals into read-only
storage and the system detects the attempt to write there.  The
compiler is not required to do this.  Older versions of gcc even had a
command-line option to control whether strings were placed in
read-only or writable storage.

> btw,how to generate the asm code using tcc?

I believe tcc compiles directly to machine code without an assembly
stage.  If you want to see what it produces, you'll need to use a
disassembler on the resulting binary.

                                                  -Dave Dodge




reply via email to

[Prev in Thread] Current Thread [Next in Thread]