By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 2,294 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

Possible C++ compiler bug?

P: n/a
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;
}

---

Jun 7 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a

<ws********@gmail.comwrote in message
news:11**********************@k79g2000hse.googlegr oups.com...
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)));
}
};

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

Jun 7 '07 #2

P: n/a
On 2007-06-07, Jim Langston <ta*******@rocketmail.comwrote:
>
<ws********@gmail.comwrote in message
news:11**********************@k79g2000hse.googlegr oups.com...
>//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).
&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.
Jun 7 '07 #3

P: n/a
On Jun 7, 7:15 am, wschlan...@gmail.com wrote:
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::ostream works a lot better.
--
#include <stdio.h>
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.
}
};
--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 7 '07 #4

P: n/a
ws********@gmail.com wrote:
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;
Jun 7 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.