The following program initializes some structs, and prints them.
#include <stdio.h>
typedef struct {
unsigned short w, h;
} Dim_t;
typedef struct {
unsigned short x, y;
} Point_t;
main() {
int i;
Dim_t dim = {160,200};
Point_t l_pos[2] = { {30, 15}, {210, 15} };
Point_t i_pos[2] = { {30, 45}, {210, 45} };
Point_t list_pos[2] = { {30, 75}, {210, 75} };
for (i=0; i<2; i++) {
printf (“pos: x=%d, y=%d\n”, l_pos_.x, l_pos.y);
printf (“pos: x=%d, y=%d\n”, i_pos.x, i_pos.y);
printf (“pos: x=%d, y=%d\n\n”, list_pos.x, list_pos.y);
}
printf (“dim w=%d h=%d\n”, dim.w, dim.h);
}
------------------------------------------------------------------------
Compiling it as .cpp-file (run cc) and running it makes the following wrong
output:
pos: x=0, y=0
pos: x=0, y=0
pos: x=30, y=75
pos: x=210, y=15
pos: x=210, y=45
pos: x=210, y=75
dim w=0 h=0
Compiling it as .c-file (run cc) and running it makes the following right
output:
pos: x=30, y=15
pos: x=30, y=45
pos: x=30, y=75
pos: x=210, y=15
pos: x=210, y=45
pos: x=210, y=75
dim w=160 h=200
This is how the c-compiler does the initialisation (got that from the
…s-file)
subl $40,%esp
movw $160,-8(%ebp)
movw $200,-6(%ebp)
movw $30,-16(%ebp)
movw $15,-14(%ebp)
movw $210,-12(%ebp)
movw $15,-10(%ebp)
movw $30,-24(%ebp)
movw $45,-22(%ebp)
movw $210,-20(%ebp)
movw $45,-18(%ebp)
movw $30,-32(%ebp)
movw $75,-30(%ebp)
movw $210,-28(%ebp)
movw $75,-26(%ebp)
movl $0,-4(%ebp)
.align 4
and that is how the c++ compiler does it
subl $40,%esp
…LCFI2:
movl $0,-8(%ebp)
movw $160,-8(%ebp)
movw $200,-6(%ebp)
movl $0,-16(%ebp)
movl $0,-12(%ebp)
movw $30,-16(%ebp)
movw $15,-14(%ebp)
movl $0,-12(%ebp)
movl $0,-8(%ebp)
movw $210,-12(%ebp)
movw $15,-10(%ebp)
movl $0,-24(%ebp)
movl $0,-20(%ebp)
movw $30,-24(%ebp)
movw $45,-22(%ebp)
movl $0,-20(%ebp)
movl $0,-16(%ebp)
movw $210,-20(%ebp)
movw $45,-18(%ebp)
movl $0,-32(%ebp)
movl $0,-28(%ebp)
movw $30,-32(%ebp)
movw $75,-30(%ebp)
movl $0,-28(%ebp)
movl $0,-24(%ebp)
movw $210,-28(%ebp)
movw $75,-26(%ebp)
movl $0,-4(%ebp)
.align 4
Obviously it init’s the structs with 0 by default and then fills in the
numbers, but by that it destroys numbers already filled in.
A workaround for the problem is to make the structs static!
Compiler-Version (from the .s-file): .ident “GCC: (GNU) 2.95.2 19991024
(release)”
I found this problem some time before, and I hoped, it would be solved with
the new version (from friday 19.01), but it still exists.
Regards Nobert_