C++ member pointers and anonymous structs/unions broken?

U

Udo Steinberg

Hi,

The program below stores the 5 unsigned ints a, b, c, x and y in an
anonymous union, so that they share storage space with state[].
Additionally x and y share storage space with nums[]. The main
function displays the address of each of the 5 integers twice, first
using the member pointer table "members" and then using an instance of
myclass. The results are surprising: the values for x and y are
incorrect when using the member pointers.

laptop:~/scratch> g++ -W -Wall foo.cc -o foo
laptop:~/scratch> ./foo
a -> 0x8049718 0x8049718
b -> 0x804971c 0x804971c
x -> 0x8049718 0x8049720
y -> 0x804971c 0x8049724
c -> 0x8049728 0x8049728

This is g++ version 3.4.0 from the GNU compiler collection.

Any ideas?
-Udo.



#include <stdio.h>

class myclass
{

public:
union
{
unsigned state[];
struct
{
unsigned a;
unsigned b;
union
{
unsigned nums[];
struct
{
unsigned x, y;
};
};
unsigned c;
};
};
};

unsigned myclass::*members[] =
{
&myclass::a,
&myclass::b,
&myclass::x,
&myclass::y,
&myclass::c,
};

myclass foo;

int
main (void)
{
printf ("a -> %p %p\n", &(foo.*members[0]), &foo.a);
printf ("b -> %p %p\n", &(foo.*members[1]), &foo.b);
printf ("x -> %p %p\n", &(foo.*members[2]), &foo.x);
printf ("y -> %p %p\n", &(foo.*members[3]), &foo.y);
printf ("c -> %p %p\n", &(foo.*members[4]), &foo.c);

return 0;
}
 
J

John Harrison

Udo Steinberg said:
Hi,

The program below stores the 5 unsigned ints a, b, c, x and y in an
anonymous union, so that they share storage space with state[].
Additionally x and y share storage space with nums[]. The main
function displays the address of each of the 5 integers twice, first
using the member pointer table "members" and then using an instance of
myclass. The results are surprising: the values for x and y are
incorrect when using the member pointers.

[snip]

Your code contains two non-standard features.
class myclass
{

public:
union
{
unsigned state[];

Missing array bounds.

Anonymous structs are not legal C++.
{
unsigned a;
unsigned b;

So not surprisingly the C++ standard has nothing to say on whether your
output is correct or not. Suggest you take this up on a GNU group.

john
 
J

JKop

The "myclass" you'd written was not C++. Here's *my* adaptation:


class myclass
{
public:

union
{

unsigned int state[5];

struct
{
unsigned int a;
unsigned int b;

union
{
unsigned int nums[2];

struct
{
unsigned int x;
unsigned int y;
};
};

unsigned c;

};

};
};
 
U

Udo Steinberg

JKop said:
The "myclass" you'd written was not C++. Here's *my* adaptation:

[snip]

Right. However, using state[5] and nums[2] does not make a difference
wrt. the wrong results for member pointers. What produces correct
results is using named unions/structs. I still consider it a bug that
g++ compiles the code without warning (unless -pedantic is used) and
computes wrong pointers.

-Udo.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top