473,396 Members | 1,789 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Possible C++ compiler bug?

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
4 1166

<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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Rob Williscroft | last post by:
I have the following code , part of a trait to detect wether a type has a member operator "Type" (), it works fine with g++ 3.4 (msvc compiles it but it doesn't work for template'd operator T ())...
13
by: Eric | last post by:
I have a template class that instantiates a variable of the template parameter type as follows: template <class T> struct TemplateClass { T t; }; struct TestClass
9
by: Vinodh Kumar P | last post by:
int main() { int aNormalInt = 0; short aShortInt = 0; aShortInt = aNormalInt; // Why I am not getting a warining for possible data lose in MS VC++6.0, even with warning level 4? return 0; } ...
4
by: veereshai | last post by:
i want to copy the functions from my source file into a new file...and convert each function into a new object file by compiling it. now, i want to invoke the function using the object file i have...
2
by: Bret Pehrson | last post by:
I just need to confirm now (after I've spent several days chasing mystery metadata warnings/errors) that this is *NOT* possible: Link a managed C++ static library into a (managed) C# application....
3
by: Chris Calzaretta | last post by:
From: "Chris Calzaretta" <ccalzaretta@hotmail.com> Subject: Re: Is It Possible? Date: Friday, February 04, 2005 11:44 AM Ok i am posting the code I need to create a form from this web service...
24
by: Arne Demmers | last post by:
Hi, As a project of mine I have to do some C programming, which in fact isn't the real problem. The problem it self is that I will have to write some code that is as minimal as possible, and as...
2
by: venkatbo | last post by:
Hi all, I have python2.4 running on ppc-linux 2.6.17. I'm attempting to get a TurboGears 0.9a9 (using CherryPy 2.2.1) based app running on it. During the TG-app startup sequence, it reaches...
1
by: Gerwin | last post by:
Hi, I have been using the interl IPP lib for some time on a microsoft compiler. Now i was wondering if I can switch to a gnu based compiler and still use the IPP. IPP uses .lib files where as...
71
by: Jack | last post by:
I understand that the standard Python distribution is considered the C-Python. Howerver, the current C-Python is really a combination of C and Python implementation. There are about 2000 Python...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.