Overlay

L

luserXtrog

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?
 
K

Keith Thompson

luserXtrog said:
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.
 
L

luserXtrog

luserXtrog said:
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?
 
L

luserXtrog

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top