Possible C++ compiler bug?

W

wschlanger

Hi, the following code works as expected, e.g. it prints 1234. How can
this be if the address of C2::x is 0 or 1?? Why does the printf that
prints the address of C2::x, print a 0 or 1 instead of its real
address? What am I missing here?

Thanks in advance,
Willow

--

#include <stdio.h>

class C2
{
public:
int x;
};

class C1 :
public C2
{
public:
void f()
{
C2::x = 0x1234;
printf("%x\n", C2::x);

//The following line prints 0 or 1
printf("%x\n", &C2::x);

//The following line will not even compile. WHY???
//printf("%x\n", (unsigned)(&C2::x));
}
};

int main()
{
C1 a;
a.f();
return 0;
}

---
 
J

Jim Langston

Hi, the following code works as expected, e.g. it prints 1234. How can
this be if the address of C2::x is 0 or 1?? Why does the printf that
prints the address of C2::x, print a 0 or 1 instead of its real
address? What am I missing here?

Thanks in advance,
Willow

--

#include <stdio.h>

class C2
{
public:
int x;
};

class C1 :
public C2
{
public:
void f()
{
C2::x = 0x1234;
printf("%x\n", C2::x);

//The following line prints 0 or 1
printf("%x\n", &C2::x);

printf("%x\n", &(C2::x));

Don't ask me why. Putting parenthesis any other place won't compile. But
it gives a hex value using &(C2::x).
//The following line will not even compile. WHY???
//printf("%x\n", (unsigned)(&C2::x));

Same thing.

printf("%x\n", (unsigned)(&(C2::x)));
 
C

Charles Bailey

printf("%x\n", &(C2::x));

Don't ask me why. Putting parenthesis any other place won't compile. But
it gives a hex value using &(C2::x).

&C2::x is a pointer to member and can only be dereferenced with a .*
or ->* member in conjunction with an instance of C2. It can, however,
be converted to bool (4.12).

On the other hand &(C2::x) is the address of the member variable in
"this" instance of C2. See section 5.3.1 para 3 of the standard for details.
 
J

James Kanze

Hi, the following code works as expected, e.g. it prints 1234. How can
this be if the address of C2::x is 0 or 1?? Why does the printf that
prints the address of C2::x, print a 0 or 1 instead of its real
address? What am I missing here?

That the code has undefined behavior.

This is typical of the mess you get into if you use printf.
Don't. std::eek:stream works a lot better.
class C2
{
public:
int x;
};
class C1 :
public C2
{
public:
void f()
{
C2::x = 0x1234;
printf("%x\n", C2::x);

Undefined behavior. The %x formatter requires an unsigned int,
and not an int.

In practice, it's hard to imagine an implementation where this
won't work, but formally, you need to write:

printf( "%x\n", (unsigned int)C2::x ) ;
//The following line prints 0 or 1
printf("%x\n", &C2::x);

Undefined behavior. About the only way to legally output the
contents of a pointer to member is to assign it to a variable
and then use some sort of ugly type punning using reinterpret
cast:

int C2::*pi = &C2::i ;
printf( "%x\n", reinterpret_cast< unsigned int& >( pi ) ) ;

Even this will not work if the size of the pointer to member is
not the same as the size of an int.

Typically, a pointer to a data member will be implemented as
some integral type (probably a size_t) containing the offset of
the member from the start of the class. Sometimes, this offset
will be incremented by one so that the null pointer
representation can be all bits 0; historically, there's a lot of
code out there that assumes that memset(...,0,...) will cause
any pointers to be null pointer. Such code is wrong, but
compiler vendors don't like proving their customers to be
idiots.
//The following line will not even compile. WHY???
//printf("%x\n", (unsigned)(&C2::x));

Because the standard doesn't define any such conversion.
 
R

Ron Natalie

C2::x = 0x1234;
printf("%x\n", C2::x);

This is normal.
//The following line prints 0 or 1
printf("%x\n", &C2::x);

This is undefined behavior. You are passing a pointer to member
to a vararg'd function (which was expecting an integer anyhow).
//The following line will not even compile. WHY???
//printf("%x\n", (unsigned)(&C2::x));

There's no conversion between pointer-to-member and unsigned.



What you seem to be missing is that the expression
&C2::x

does not have type pointer to int, but pointer to member of type int.
Pointers to members don't work like regular pointers, they are
essentially an offset that when applied to a different class pointer
yield the member.

&x

would give you a pointer to the specific instance of x in the "this"
object, add would a fully qualified
&this->C2::x;
 

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,780
Messages
2,569,611
Members
45,273
Latest member
DamonShoem

Latest Threads

Top