C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

about "volatile int*"

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
bruceteen@gmail.com
Guest





PostPosted: Fri Sep 09, 2005 3:26 pm    Post subject: about "volatile int*" Reply with quote



I am a chinese٬My english is poor, sorry.

// use g++3.4.2

#include <iostream>
using namespace std;

int main( void )
{
volatile int k=0;
cout<< &k <
cout << typeid(bool).name() << endl; // b
cout << typeid(int*).name() << endl; // Pi
cout << typeid(volatile int*).name() << endl; // PVi
cout << typeid(&k).name() << endl; // PVi

system("PAUSE");
return EXIT_SUCCESS;
}

^_^


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Thomas Tutone
Guest





PostPosted: Sat Sep 10, 2005 12:39 am    Post subject: Re: about "volatile int*" Reply with quote



[email]bruceteen (AT) gmail (DOT) com[/email] wrote:

Quote:
#include <iostream
using namespace std;

int main( void )
{
volatile int k=0;
cout<< &k <

Replace the above line with this:

cout << static_cast
The problem, apparently, is that there is no overloaded operator<< that
accepts a volatile int*, but there is an overloaded operator<< that
takes a bool. So, your pointer gets converted to a bool, and unless
you change the formatting flags, a bool true gets output as "1".

To fix it, you first have to cast away the volatile part, using
const_cast<>, then cast it into a void*, using static_cast<>.

Best regards,

Tom


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Ron Natalie
Guest





PostPosted: Sat Sep 10, 2005 12:41 am    Post subject: Re: about "volatile int*" Reply with quote



[email]bruceteen (AT) gmail (DOT) com[/email] wrote:
Quote:
I am a chinese٬My english is poor, sorry.

// use g++3.4.2

#include <iostream
using namespace std;

int main( void )
{
volatile int k=0;
cout<< &k <


Because the thing that prints addresses for pointers takes a void*.
You can't implicity convert from volatile int* to void* (violates
the CV qualification).

cout << (void*)&k << endl;

will hack it (or prehaps better):
cout << const_cast cast away the volatile.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Ben Hutchings
Guest





PostPosted: Sat Sep 10, 2005 12:42 am    Post subject: Re: about "volatile int*" Reply with quote

[email]bruceteen (AT) gmail (DOT) com[/email] <bruceteen (AT) gmail (DOT) com> wrote:
Quote:
I am a chinese٬My english is poor, sorry.

// use g++3.4.2

#include <iostream
using namespace std;

int main( void )
{
volatile int k=0;
cout<< &k < snip


There is a standard conversion from volatile int * to bool, but not
from volatile int * to const void *, so the only viable candidate for
operator<< here is the one which takes a bool. Since &k is non-null,
the result of conversion to bool is true, which is printed as 1.

Ben.

--
Ben Hutchings
Having problems with C++ templates? Your questions may be answered by

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.