Overlay

Discussion in 'C Programming' started by luserXtrog, Jun 9, 2009.

  1. luserXtrog

    luserXtrog Guest

    Another gem from the archive, ISOfied.

    #include <stdio.h>
    int main() {
    char s[0];
    int i;

    i = 0x12345678;
    s[0] = 'a';

    printf("i = 0x%08x\n",i);
    }

    625(1)01:16 AM:~ 0> vi overlay.c
    626(1)01:16 AM:~ 0> make overlay
    cc overlay.c -o overlay
    627(1)01:17 AM:~ 0> overlay
    i = 0x12345661
    628(1)01:17 AM:~ 15>

    Looks like I'm little-endian. Flip those eggs!

    Surely this is a violation of something. But doesn't
    it pretty much have to be equivalent to assigning
    through a reinterpreted pointer?

    --
    likes tea
    luserXtrog, Jun 9, 2009
    #1
    1. Advertising

  2. luserXtrog <> writes:
    > Another gem from the archive, ISOfied.
    >
    > #include <stdio.h>
    > int main() {
    > char s[0];
    > int i;
    >
    > i = 0x12345678;
    > s[0] = 'a';
    >
    > printf("i = 0x%08x\n",i);
    > }
    >
    > 625(1)01:16 AM:~ 0> vi overlay.c
    > 626(1)01:16 AM:~ 0> make overlay
    > cc overlay.c -o overlay
    > 627(1)01:17 AM:~ 0> overlay
    > i = 0x12345661
    > 628(1)01:17 AM:~ 15>
    >
    > Looks like I'm little-endian. Flip those eggs!
    >
    > Surely this is a violation of something. But doesn't
    > it pretty much have to be equivalent to assigning
    > through a reinterpreted pointer?


    The declaration of s violates a constraint (C99 6.7.5.2p1), and
    referring to s[0] invokes undefined behavior. The actual behavior of
    the program might tell you something about data layout, given some
    assumptions about how the compiler works, but the program could
    validly print "Hello, world".

    With various compiler options, the output I've gotten (on a
    little-endian system) is "i = 0x12345678", with or without a
    segmentation fault.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Jun 9, 2009
    #2
    1. Advertising

  3. luserXtrog

    luserXtrog Guest

    On Jun 9, 1:55 am, Keith Thompson <> wrote:
    > luserXtrog <> writes:
    > > Another gem from the archive, ISOfied.

    >
    > > #include <stdio.h>
    > > int main() {
    > >         char s[0];
    > >         int i;

    >
    > >         i = 0x12345678;
    > >         s[0] = 'a';

    >
    > >         printf("i = 0x%08x\n",i);
    > > }

    >
    > > 625(1)01:16 AM:~ 0> vi overlay.c
    > > 626(1)01:16 AM:~ 0> make overlay
    > > cc     overlay.c   -o overlay
    > > 627(1)01:17 AM:~ 0> overlay
    > > i = 0x12345661
    > > 628(1)01:17 AM:~ 15>

    >
    > > Looks like I'm little-endian. Flip those eggs!

    >
    > > Surely this is a violation of something. But doesn't
    > > it pretty much have to be equivalent to assigning
    > > through a reinterpreted pointer?

    >
    > The declaration of s violates a constraint (C99 6.7.5.2p1), and
    > referring to s[0] invokes undefined behavior.  The actual behavior of
    > the program might tell you something about data layout, given some
    > assumptions about how the compiler works, but the program could
    > validly print "Hello, world".
    >
    > With various compiler options, the output I've gotten (on a
    > little-endian system) is "i = 0x12345678", with or without a
    > segmentation fault.


    Bad bad bad. got it.

    Ooo. As a constraint violation, ought there not to have been
    a diagnostic?

    --
    lxt
    luserXtrog, Jun 9, 2009
    #3
  4. luserXtrog

    luserXtrog Guest

    On Jun 9, 2:04 am, luserXtrog <> wrote:

    > Bad bad bad. got it.
    >
    > Ooo. As a constraint violation, ought there not to have been
    > a diagnostic?


    Ooops. forgot -pedantic. neb' mind.

    --
    sleepy
    luserXtrog, Jun 9, 2009
    #4
  5. luserXtrog

    luserXtrog Guest

    On Jun 9, 9:24 pm, Jack Klein <> wrote:
    > On Mon, 8 Jun 2009 23:21:01 -0700 (PDT), luserXtrog
    > <> wrote in comp.lang.c:
    >
    >
    >
    > > Another gem from the archive, ISOfied.

    >
    > > #include <stdio.h>
    > > int main() {
    > >         char s[0];
    > >         int i;

    >
    > >         i = 0x12345678;
    > >         s[0] = 'a';

    >
    > >         printf("i = 0x%08x\n",i);
    > > }

    >
    > > 625(1)01:16 AM:~ 0> vi overlay.c
    > > 626(1)01:16 AM:~ 0> make overlay
    > > cc     overlay.c   -o overlay
    > > 627(1)01:17 AM:~ 0> overlay
    > > i = 0x12345661
    > > 628(1)01:17 AM:~ 15>

    >
    > > Looks like I'm little-endian. Flip those eggs!

    >
    > > Surely this is a violation of something. But doesn't
    > > it pretty much have to be equivalent to assigning
    > > through a reinterpreted pointer?

    >
    > No, it does not.  Nor does it have to be equivalent to anything else
    > you can think of.  Even aside from the required diagnostic and
    > recommended rejection of the program for violating a constraint on the
    > declaration of an array with a size that is not greater than zero.
    >
    > There is no requirement whatsoever that the two local objects exist in
    > any particular relationship to each other in memory.  Or even that
    > they be adjacent to each other.


    Understood. It works as well as assigning through a reinterpreted
    pointer that could be pointing anywhere or nowhere at all.
    Do not do this.

    --
    lxt
    luserXtrog, Jun 10, 2009
    #5
    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. Brian Lowe

    overlay text on uploaded picture

    Brian Lowe, Jun 21, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    643
    Brian Lowe
    Jun 24, 2004
  2. image overlay

    , Sep 9, 2005, in forum: Java
    Replies:
    1
    Views:
    518
    jan V
    Sep 9, 2005
  3. Lee Harris  web
    Replies:
    2
    Views:
    3,742
    Lee Harris
    Jan 31, 2004
  4. Ross
    Replies:
    1
    Views:
    618
    Martin Johansen
    Jan 6, 2005
  5. Fabri

    Overlay table...

    Fabri, Feb 21, 2005, in forum: HTML
    Replies:
    0
    Views:
    828
    Fabri
    Feb 21, 2005
Loading...

Share This Page