473,386 Members | 1,736 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,386 software developers and data experts.

casting between pointers

hi,

why does the following produce an output of 64769 instead of 1

int main()
{
bool* pBool = new bool(false);

unsigned short* pUS = (unsigned short*)pBool;

cout << *pUS;

delete pBool;

return 0;
}

I expected the output to be 1 since the size of a bool and the size of
an unsigned int is 1
Mar 29 '06 #1
8 2184
scroopy <sc*****@nospam.com> wrote:
why does the following produce an output of 64769 instead of 1
Because you get undefined behaviour when casting one pointer type to
another and using it as that. You know .. the
demons-fly-out-of-your-nose issues ..
int main()
{
bool* pBool = new bool(false);

unsigned short* pUS = (unsigned short*)pBool;

cout << *pUS;

delete pBool;

return 0;
}

I expected the output to be 1 since the size of a bool and the size of
an unsigned int is 1


hth
--
jb

(reply address in rot13, unscramble first)
Mar 29 '06 #2
"scroopy" <sc*****@nospam.com> wrote in message
news:vl********************************@4ax.com...
hi,

why does the following produce an output of 64769 instead of 1

int main()
{
bool* pBool = new bool(false);

unsigned short* pUS = (unsigned short*)pBool;

cout << *pUS;

delete pBool;

return 0;
}

I expected the output to be 1 since the size of a bool and the size of
an unsigned int is 1


On what system? The size of an unsigned char would be 1, unsigned short
would probably be 2?
Mar 29 '06 #3
Hi scroopy:

scroopy wrote:
hi,

why does the following produce an output of 64769 instead of 1

int main()
{
bool* pBool = new bool(false);

unsigned short* pUS = (unsigned short*)pBool;

cout << *pUS;

delete pBool;

return 0;
}

I expected the output to be 1 since the size of a bool and the size of
an unsigned int is 1


C++ Standard says "sizeof(bool) is not required to be 1."
I think size of bool type depends on your compiler.

Are you sure that in your compiler sizeof(bool)==sizeof(unsigned short)

Why do you think the result must be 1?
Supossing that for your compiler, sizeof(bool)==sizeof(unsigned short),
the result will be 0.
Mar 29 '06 #4
On Wed, 29 Mar 2006 10:30:48 +0100, scroopy <sc*****@nospam.com>
wrote:
I expected the output to be 1 since the size of a bool and the size of
an unsigned int is 1


This is implementation-defined. On 16-bit Intel x86 platforms, for
example, sizeof(int) is 2; on 32-bit platforms it is usually 4, and on
64-bit platforms 8. Other platforms exist where this is different.

bool can also be implemented using a single bit, even if sizeof(bool)
implies something else. So even if a bool variable might occupy the
same storage as an int, the compiler might just ignore all but one of
the bits (which one, again, would be implementation-defined).

--
Bob Hairgrove
No**********@Home.com
Mar 29 '06 #5
It seems you are writing 1 byte and reading 2. Second byte is
undefined. Add the following line:

assert( sizeof(bool) == sizeof(unsigned short) );

before casting your pointers. And you will get an error message if
casting is not possible.

And IMHO "false" is not equals to 1. I suppose it fills all
sizeof(bool) bytes by "1" bits. It is 0xFF for sizeof(bool)==1 and so
on.

Raider

Mar 29 '06 #6
Jakob Bieling <ar****************@rot13.com> wrote:
scroopy <sc*****@nospam.com> wrote:
why does the following produce an output of 64769 instead of 1


Because you get undefined behaviour when casting one pointer type


Wee correction: it is not undefined, but implementation defined. So
no demons now ;) But you would have to check your compiler manual or a
newsgroup dedicated for your compiler to find out why you get that
result.

On my system, I would get a similar result to yours. I have
sizeof(bool) == 1 and sizeof (unsigned short) == 2 .. by just observing
what is happening, I can see that the short pointer lets me read memory,
that does not belong to the bool anymore, ie. it contains junk. The
bool-part is 1 tho. Here at least.

regards
--
jb

(reply address in rot13, unscramble first)
Mar 29 '06 #7
In article <vl********************************@4ax.com>,
scroopy <sc*****@nospam.com> wrote:
hi,

why does the following produce an output of 64769 instead of 1

int main()
{
bool* pBool = new bool(false);

unsigned short* pUS = (unsigned short*)pBool;

cout << *pUS;

delete pBool;

return 0;
}

I expected the output to be 1 since the size of a bool and the size of
an unsigned int is 1


On my system, sizeof short is 2, while sizeof bool is 4. What the sizeof
unsigned int has to do with the above program I have no idea...
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Mar 29 '06 #8
Daniel T. wrote:
scroopy <sc*****@nospam.com> wrote:
why does the following produce an output of 64769 instead of 1

bool* pBool = new bool(false);
unsigned short* pUS = (unsigned short*)pBool;
cout << *pUS;

I expected the output to be 1 since the size of a bool and the size of
an unsigned int is 1


On my system, sizeof short is 2, while sizeof bool is 4. What the sizeof
unsigned int has to do with the above program I have no idea...


If sizeof(unsigned int) == 1, it would imply that sizeof(unsigned
short) is also 1, because short cannot be bigger than int.

To answer the original question, and assuming the quoted sizes are
correct: probably the bool only uses one bit of storage, so the other
bits could contain garbage values. The result of trying to read *pUS
will be:
- undefined, if sizeof(unsigned short) > sizeof(bool)
- undefined, if the content of the memory region in question does
not represent a valid unsigned short
- well-defined otherwise.

ISTR that unsigned types are not allowed to have trap representations,
so the second case might never happen.

Mar 30 '06 #9

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

Similar topics

4
by: Jacob Jensen | last post by:
This question has probably been asked a million time, but here it comes again. I want to learn the difference between the three type cast operators: static_cast, reinterpret_cast, dynamic_cast. A...
16
by: He Shiming | last post by:
Hi, I'm having a little bit of trouble regarding pointer casting in my program. I don't understand why the following two cases produce different results. Case 1: IInterface *pInterface = new...
8
by: rihad | last post by:
Hi, I've this question: suppose we have two differently typed pointers: struct foo *foo; char **array; Becase one can always legally cast any pointer to (void *), and becase (void *) is...
35
by: ytrama | last post by:
Hi, I have read in one of old posting that don't cast of pointer which is returned by the malloc. I would like to know the reason. Thanks in advance, YTR
14
by: Mirko | last post by:
Hello, I'm new to this list and to Usenet in general, so please forgive (and advice) me, if I do something wrong. Anyway. I am a bit confused, because I always thought one _should_ explicitly...
4
by: Xavier Roche | last post by:
Hi folks, I have a probably rather silly question: is casting a char array in a char* a potential source of aliasing bug ? Example: a fonction returning a buffer taken in a circular buffer ...
9
by: Jess | last post by:
Hello, It seems both static_cast and dynamic_cast can cast a base class pointer/reference to a derived class pointer/reference. If so, is there any difference between them? In addition, if I...
9
by: Taras_96 | last post by:
Hi everyone, I was experimenting with static_cast and reinterpret cast #include <iostream> struct A1 { int a; }; struct A2 { double d; }; struct B : public A1, A2
5
by: jason.cipriani | last post by:
There have been some recent threads about casting pointers to and from void* that have me rethinking some of my usual practices. I have a couple of questions. 1. What is the purpose of C++'s...
101
by: Tinkertim | last post by:
Hi, I have often wondered if casting the return value of malloc() (or friends) actually helps anything, recent threads here suggest that it does not .. so I hope to find out. For instance : ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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
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
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,...

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.