linux-gcc.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Are string literals _always_ allocated in the RO section?
@ 2005-01-15 21:22 Denis Zaitsev
  2005-01-15 21:27 ` Dale Johannesen
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Denis Zaitsev @ 2005-01-15 21:22 UTC (permalink / raw)
  To: gcc, linux-gcc

This program:

void x(char *s)
{
    *s= 'x';
}

main()
{
    x("y");
}

is compiled plainly but cathes SIGSEGV in the 

        *s= 'x' 

line.  And it's because the string "y" is put in the .text section.
And the fact that the x()'s parm is explicitly declared as <char*>,
not <const char*> does nothing in this case.  Should it be considered
as a bug?

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Are string literals _always_ allocated in the RO section?
  2005-01-15 21:22 Are string literals _always_ allocated in the RO section? Denis Zaitsev
@ 2005-01-15 21:27 ` Dale Johannesen
  2005-01-15 21:41 ` Denis Zaitsev
  2005-01-15 23:18 ` Gabriel Dos Reis
  2 siblings, 0 replies; 5+ messages in thread
From: Dale Johannesen @ 2005-01-15 21:27 UTC (permalink / raw)
  To: Denis Zaitsev; +Cc: gcc, linux-gcc, Dale Johannesen


On Jan 15, 2005, at 1:22 PM, Denis Zaitsev wrote:

> This program:
>
> void x(char *s)
> {
>     *s= 'x';
> }
>
> main()
> {
>     x("y");
> }
>
> is compiled plainly but cathes SIGSEGV in the
>
>         *s= 'x'
>
> line.

That's what's supposed to happen.  Storing into a string
constant is invalid C.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Are string literals _always_ allocated in the RO section?
  2005-01-15 21:22 Are string literals _always_ allocated in the RO section? Denis Zaitsev
  2005-01-15 21:27 ` Dale Johannesen
@ 2005-01-15 21:41 ` Denis Zaitsev
  2005-01-15 23:18 ` Gabriel Dos Reis
  2 siblings, 0 replies; 5+ messages in thread
From: Denis Zaitsev @ 2005-01-15 21:41 UTC (permalink / raw)
  To: gcc, linux-gcc

On Sun, Jan 16, 2005 at 02:22:48AM +0500, Denis Zaitsev wrote:
> the string "y" is put in the .text section.

Oh, it's put into the .rodata section.  A'm sorry.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Are string literals _always_ allocated in the RO section?
  2005-01-15 21:22 Are string literals _always_ allocated in the RO section? Denis Zaitsev
  2005-01-15 21:27 ` Dale Johannesen
  2005-01-15 21:41 ` Denis Zaitsev
@ 2005-01-15 23:18 ` Gabriel Dos Reis
  2005-01-17 17:18   ` Jan-Benedict Glaw
  2 siblings, 1 reply; 5+ messages in thread
From: Gabriel Dos Reis @ 2005-01-15 23:18 UTC (permalink / raw)
  To: Denis Zaitsev; +Cc: gcc, linux-gcc

Denis Zaitsev <zzz@anda.ru> writes:

| This program:
| 
| void x(char *s)
| {
|     *s= 'x';
| }
| 
| main()
| {
|     x("y");
| }
| 
| is compiled plainly but cathes SIGSEGV in the 
| 
|         *s= 'x' 
| 
| line.  And it's because the string "y" is put in the .text section.
| And the fact that the x()'s parm is explicitly declared as <char*>,
| not <const char*> does nothing in this case.  Should it be considered
| as a bug?

In the program? Yes.

-- Gaby

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Are string literals _always_ allocated in the RO section?
  2005-01-15 23:18 ` Gabriel Dos Reis
@ 2005-01-17 17:18   ` Jan-Benedict Glaw
  0 siblings, 0 replies; 5+ messages in thread
From: Jan-Benedict Glaw @ 2005-01-17 17:18 UTC (permalink / raw)
  To: linux-gcc

[-- Attachment #1: Type: text/plain, Size: 1156 bytes --]

On Sun, 2005-01-16 00:18:20 +0100, Gabriel Dos Reis <gdr@integrable-solutions.net>
wrote in message <m3brbq1dtv.fsf@uniton.integrable-solutions.net>:
> Denis Zaitsev <zzz@anda.ru> writes:
> 
> | This program:
> | 
> | void x(char *s)
> | {
> |     *s= 'x';
> | }
> | 
> | main()
> | {
> |     x("y");
> | }

> | not <const char*> does nothing in this case.  Should it be considered
> | as a bug?
> 
> In the program? Yes.

Maybe you'd tell him more about the magic :)

char *mytext = "hello";

results in a pointer (which you may later on make pointing to a
different location) that points to a static, read-only string containing
"hello\0". However, if you need to change the text, you need to do it
like this:

char mytext[] = "hello";

Cf. section 6.4.5.6 of the C99 standard.

MfG, JBG

-- 
Jan-Benedict Glaw       jbglaw@lug-owl.de    . +49-172-7608481             _ O _
"Eine Freie Meinung in  einem Freien Kopf    | Gegen Zensur | Gegen Krieg  _ _ O
 fuer einen Freien Staat voll Freier Bürger" | im Internet! |   im Irak!   O O O
ret = do_actions((curr | FREE_SPEECH) & ~(NEW_COPYRIGHT_LAW | DRM | TCPA));

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 189 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2005-01-17 17:18 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-01-15 21:22 Are string literals _always_ allocated in the RO section? Denis Zaitsev
2005-01-15 21:27 ` Dale Johannesen
2005-01-15 21:41 ` Denis Zaitsev
2005-01-15 23:18 ` Gabriel Dos Reis
2005-01-17 17:18   ` Jan-Benedict Glaw

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).