gcc bug?

Discussion in 'C Programming' started by Victor Irzak, Nov 7, 2003.

  1. Victor Irzak

    Victor Irzak Guest

    Hello!

    This program causes seg fault on gcc, but executes fine on icc and VC7.
    Is there a reason for it or is it a bug?
    Note: if "char * const str" is changed to "char * str", the gcc problem disappears.

    #include "stdio.h"

    int main() {
    char * const str="ya";
    char *first = &str[0], *second =&str[1];
    char tmp;

    printf("%s\n", str);

    tmp = *first;
    *first = *second;
    *second = tmp;

    printf("%s\n", str);

    return 0;
    }
    Victor Irzak, Nov 7, 2003
    #1
    1. Advertising

  2. Victor Irzak

    Artie Gold Guest

    Victor Irzak wrote:
    > Hello!
    >
    > This program causes seg fault on gcc, but executes fine on icc and VC7.
    > Is there a reason for it or is it a bug?
    > Note: if "char * const str" is changed to "char * str", the gcc problem disappears.
    >
    > #include "stdio.h"
    >
    > int main() {
    > char * const str="ya";
    > char *first = &str[0], *second =&str[1];
    > char tmp;
    >
    > printf("%s\n", str);
    >
    > tmp = *first;
    > *first = *second;


    You're trying to mutate a string literal -- which is not necessarily
    mutable. Hence, in the gcc case, BOOM!

    > *second = tmp;
    >
    > printf("%s\n", str);
    >
    > return 0;
    > }


    See, for example:

    http://www.eskimo.com/~scs/C-faq/q1.32.html

    HTH,
    --ag
    --
    Artie Gold -- Austin, Texas
    Oh, for the good old days of regular old SPAM.
    Artie Gold, Nov 7, 2003
    #2
    1. Advertising

  3. On Fri, 7 Nov 2003, Victor Irzak wrote:
    >
    > This program causes seg fault on gcc, but executes fine on icc and VC7.
    > Is there a reason for it or is it a bug?


    Yes. It's a bug in your program.

    > Note: if "char * const str" is changed to "char * str",
    > the gcc problem disappears.


    The bug is still present even with the 'const' removed.

    > #include "stdio.h"


    Should be
    #include <stdio.h>

    >
    > int main() {
    > char * const str="ya";


    'str' points to the string "ya", which is stored somewhere
    off in memory, possibly in ROM (think: a segment to which your
    program cannot write).

    > char *first = &str[0], *second =&str[1];


    'first' and 'second' also point into that same string, in
    read-only memory. By the way, do you understand that

    char *first = str, *second = str+1;

    would be equivalent to the line above?

    > char tmp;
    >
    > printf("%s\n", str);


    Prints the string "ya". This is fine, so far.

    > tmp = *first;
    > *first = *second;


    Here's your bug. You try to assign a new value to
    the char object pointed to by 'first' -- and that
    object is off in read-only memory. You can't modify
    string literals in C.
    This is where the program segfaults with GCC.

    > *second = tmp;


    A second bug.

    >
    > printf("%s\n", str);
    >
    > return 0;
    > }



    To make your program work as expected, and remove
    the undefined behavior, you could create an array
    local to 'main' in which to store your string:

    char str[] = "ya";

    (then proceed as above). See this newsgroup's
    FAQ for more information.

    -Arthur
    Arthur J. O'Dwyer, Nov 7, 2003
    #3
  4. Victor Irzak

    Eric Sosman Guest

    Victor Irzak wrote:
    >
    > Hello!
    >
    > This program causes seg fault on gcc, but executes fine on icc and VC7.
    > Is there a reason for it or is it a bug?
    > Note: if "char * const str" is changed to "char * str", the gcc problem disappears.
    >
    > #include "stdio.h"
    >
    > int main() {
    > char * const str="ya";
    > char *first = &str[0], *second =&str[1];
    > char tmp;
    >
    > printf("%s\n", str);
    >
    > tmp = *first;
    > *first = *second;
    > *second = tmp;
    >
    > printf("%s\n", str);
    >
    > return 0;
    > }


    It's a bug ... in your code. You are trying to
    modify the contents of the string literal "ya", and
    this produces undefined behavior.

    <off-topic>

    It's likely that gcc is taking the `const' as a
    hint to put "ya" in read-only memory. The compilers
    are not obliged to do this (the others you mention
    apparently do not). On the other hand, they are free
    to use read-only memory for "ya" even if `const' is
    not present.

    </off-topic>

    --
    Eric Sosman, Nov 7, 2003
    #4
  5. [OT] Re: gcc bug?

    On Fri, 7 Nov 2003, Eric Sosman wrote:
    >
    > Victor Irzak wrote:
    > >


    > > char * const str="ya";


    > <off-topic>
    >
    > It's likely that gcc is taking the `const' as a
    > hint to put "ya" in read-only memory. The compilers
    > are not obliged to do this (the others you mention
    > apparently do not). On the other hand, they are free
    > to use read-only memory for "ya" even if `const' is
    > not present.


    Just for curiosity's sake, can anyone verify whether
    gcc actually will put the target of a 'char * const'
    pointer into a read-only area (but not the target of
    a non-const-qualified pointer)?

    I had thought of giving the explanation Eric did,
    but then I noticed that it wasn't the *target* string
    that was 'const' in Victor's example, it was the value
    of the *pointer* itself! I really wouldn't expect
    gcc to make that optimization based on the constness
    of the *pointer*!
    </OT>

    -Arthur
    Arthur J. O'Dwyer, Nov 7, 2003
    #5
  6. Re: [OT] Re: gcc bug?

    "Arthur J. O'Dwyer" <> wrote in
    news:p:

    >
    >> > char * const str="ya";

    >
    >> <off-topic>
    >>
    >> It's likely that gcc is taking the `const' as a
    >> hint to put "ya" in read-only memory. The compilers
    >> are not obliged to do this (the others you mention
    >> apparently do not). On the other hand, they are free
    >> to use read-only memory for "ya" even if `const' is
    >> not present.

    >
    > Just for curiosity's sake, can anyone verify whether
    > gcc actually will put the target of a 'char * const'
    > pointer into a read-only area (but not the target of
    > a non-const-qualified pointer)?
    >
    > I had thought of giving the explanation Eric did,
    > but then I noticed that it wasn't the *target* string
    > that was 'const' in Victor's example, it was the value
    > of the *pointer* itself! I really wouldn't expect
    > gcc to make that optimization based on the constness
    > of the *pointer*!
    > </OT>


    You seek the -fwritable-strings flag. Without it strings will be
    non-writable and you are correct that the pointer being const would not
    influence gcc's placement of the string.


    --
    - Mark ->
    --
    Mark A. Odell, Nov 7, 2003
    #6
  7. Re: [OT] Re: gcc bug?

    "Arthur J. O'Dwyer" wrote:
    >
    > Just for curiosity's sake, can anyone verify whether
    > gcc actually will put the target of a 'char * const'
    > pointer into a read-only area (but not the target of
    > a non-const-qualified pointer)?


    For GCC vesion 3 and later, all string literals (const
    or otherwise) are read only unless the -fwriteable-strings
    compiler swithc is used.

    There is also a -Wwrite-strings to force a warning when
    and attempt is made to write a string literal.

    Erik
    --
    +-----------------------------------------------------------+
    Erik de Castro Lopo (Yes it's valid)
    +-----------------------------------------------------------+
    Orcad Express 9: "Its a nice demo but I wouldn't want to use it
    on a day-to-day basis" -- me
    Erik de Castro Lopo, Nov 7, 2003
    #7
  8. Victor Irzak

    Alex Guest

    Victor Irzak <> wrote:
    > Hello!


    > This program causes seg fault on gcc, but executes fine on icc and VC7.
    > Is there a reason for it or is it a bug?
    > Note: if "char * const str" is changed to "char * str", the gcc problem disappears.


    > #include "stdio.h"


    > int main() {
    > char * const str="ya";
    > char *first = &str[0], *second =&str[1];
    > char tmp;


    > printf("%s\n", str);


    > tmp = *first;
    > *first = *second;
    > *second = tmp;


    > printf("%s\n", str);


    > return 0;
    > }


    You are trying to modify the string literal "ya". This is
    forbidden as it may reside in read-only memory. The following,
    however, is legal:

    char str[] = "ya";

    Alex
    Alex, Nov 8, 2003
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    8
    Views:
    427
  2. Kevin P. Fleming

    C99 structure initialization in gcc-2.95.3 vs gcc-3.3.1

    Kevin P. Fleming, Nov 6, 2003, in forum: C Programming
    Replies:
    2
    Views:
    642
    Kevin P. Fleming
    Nov 6, 2003
  3. Replies:
    5
    Views:
    357
    Nathan Addy
    Sep 17, 2005
  4. ashnin

    GCC 3.4.3 and GCC 4.1.2

    ashnin, Jul 7, 2008, in forum: C++
    Replies:
    1
    Views:
    515
    Michael DOUBEZ
    Jul 7, 2008
  5. kas
    Replies:
    1
    Views:
    315
    red floyd
    Apr 22, 2010
Loading...

Share This Page