473,486 Members | 1,932 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

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;
}
Jul 22 '05 #1
3 2034

"Udo Steinberg" <so*****@gmx.de> wrote in message
news:98**************************@posting.google.c om...
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.
struct
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
Jul 22 '05 #2

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;

};

};
};
Jul 22 '05 #3
JKop <NU**@NULL.NULL> wrote in message news:<ZG******************@news.indigo.ie>...
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.
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2715
by: JKop | last post by:
You know how from time to time, you want to have an array which you can access via: array_name; But also, you'd like the more user-friendly option: array_name.element1 array_name.element2
42
2109
by: x-pander | last post by:
Is is guaranteed, that a pointer to any object in C points exactly at the lowest addressed byte of this object? Specifcally is it possible for any platform/os/compiler combination that: (char...
47
2585
by: sunglo | last post by:
Some time a go, in a discussion here in comp.lang.c, I learnt that it's better not to use a (sometype **) where a (void **) is expected (using a cast). Part of the discussion boiled down to the...
11
2175
by: junky_fellow | last post by:
Do all double pointers have same size on a particular implementation. eg char **c_ptr; int **i_ptr; Is the size of c_ptr and i_ptr always same (on same implementation) or it may be different...
1
1733
by: John | last post by:
Hi, What is the struct member aligment of a C# program? Are there any issues with P/Invoke when passing structs to 3rd party C dlls? Thanks
18
11284
by: chankl | last post by:
Can anyone explain what's an opaque pointer and how it's implemented in C? I read about this concept in the book "C interfaces and implementations". Here's an example from the book (list.h -...
15
2255
by: Avinash | last post by:
Hi, I just wanted to confirm that sizeof for a pointer always returns the same value on a machine. I would appreciate if somebody would respond to this. Thanks.
38
2558
by: James Brown | last post by:
All, I have a quick question regarding the size of pointer-types: I believe that the sizeof(char *) may not necessarily be the same as sizeof(int *) ? But how about multiple levels of pointers...
7
6397
by: Peter Olcott | last post by:
Why can a union have a member with a copy constructor?
0
7099
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6964
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7123
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7175
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5430
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4559
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1378
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
598
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
262
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.