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

bool

P: n/a
I have written a small main function .
I have defined one bool variable and cout this variable , I found
the value of this bool variable is 64.

Since, I am using 32-bit addressing .

My program is
int main()
{
bool ab;
cout<<ab<<endl;

}

output is 64.

Why it is...?
It should be 32.
May be I am wrong.

Yashwant

Mar 14 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
yashwant pinge wrote:
I have written a small main function .
I have defined one bool variable and cout this variable , I found
the value of this bool variable is 64.
That would be very strange.
Since, I am using 32-bit addressing .
What does the value of a bool have to do with the size of an address?
My program is
int main()
{
bool ab;
cout<<ab<<endl;

}
cout and endl aren't defined. Also your bool is not initialized, so the
result is undefined.
output is 64.

Why it is...?
It should be 32.
May be I am wrong.
I have no idea why you would think it should be 32. The only values your
program should print, once you change your definition of ab to initialize
it), are 1 (if ab's value is 'true') and 0 (if it's 'false').

Mar 14 '07 #2

P: n/a
On 14 Mar, 12:17, "yashwant pinge" <yashwantpi...@gmail.comwrote:
I have written a small main function .
I have defined one bool variable and cout this variable , I found
the value of this bool variable is 64.

Since, I am using 32-bit addressing .

My program is

int main()
{
bool ab;
cout<<ab<<endl;

}

output is 64.

Why it is...?
It should be 32.
May be I am wrong.

Yashwant
here as you dont't have assigned any explicit value to it than the
value is random
and don't confusing with the addressing in fact:

bool ab;
int cc;

cout<< "not assigned value of ab= " << ab <<endl;
cout<< "not assigned value of cc= " << cc <<endl;

cout<< "sizeof ab= " << sizeof ab <<endl;
cout<< "sizeof cc= " << sizeof cc <<endl;

on my 32 bit machine output this:
>>not assigned value of ab= 191
not assigned value of cc= 8822772
sizeof ab= 1
sizeof cc= 4
Joshua


Mar 14 '07 #3

P: n/a
"yashwant pinge" <ya***********@gmail.comwrote in message
news:11**********************@y80g2000hsf.googlegr oups.com...
>I have written a small main function .
I have defined one bool variable and cout this variable , I found
the value of this bool variable is 64.

Since, I am using 32-bit addressing .

My program is
int main()
{
bool ab;
cout<<ab<<endl;

}

output is 64.

Why it is...?
It should be 32.
May be I am wrong.

Yashwant
your bool variable ab is not initialized. So it contains anything that was
in the memory where it is allocated. In your case it happened to contain
the value 32.

Try this instead:

bool ab = true;
cout << ab << endl;

it should output the value 1.

If you want to know the size of the variable, use sizeof.

bool ab = true;
cout << sizeof ab << endl;
Mar 14 '07 #4

P: n/a
On Mar 14, 5:08 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:
"yashwant pinge" <yashwantpi...@gmail.comwrote in message

news:11**********************@y80g2000hsf.googlegr oups.com...
I have written a small main function .
I have defined one bool variable and cout this variable , I found
the value of this bool variable is 64.
Since, I am using 32-bit addressing .
My program is
int main()
{
bool ab;
cout<<ab<<endl;
}
output is 64.
Why it is...?
It should be 32.
May be I am wrong.
Yashwant

your bool variable ab is not initialized. So it contains anything that was
in the memory where it is allocated. In your case it happened to contain
the value 32.

Try this instead:

bool ab = true;
cout << ab << endl;

it should output the value 1.

If you want to know the size of the variable, use sizeof.

bool ab = true;
cout << sizeof ab << endl;
since the bool is internally converted as 1 and 0. So why the size of
bool is one?

Mar 14 '07 #5

P: n/a
On 2007-03-14 14:05, yashwant pinge wrote:
On Mar 14, 5:08 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:
>"yashwant pinge" <yashwantpi...@gmail.comwrote in message

news:11**********************@y80g2000hsf.googleg roups.com...
>I have written a small main function .
I have defined one bool variable and cout this variable , I found
the value of this bool variable is 64.
Since, I am using 32-bit addressing .
My program is
int main()
{
bool ab;
cout<<ab<<endl;
}
output is 64.
Why it is...?
It should be 32.
May be I am wrong.
Yashwant

your bool variable ab is not initialized. So it contains anything that was
in the memory where it is allocated. In your case it happened to contain
the value 32.

Try this instead:

bool ab = true;
cout << ab << endl;

it should output the value 1.

If you want to know the size of the variable, use sizeof.

bool ab = true;
cout << sizeof ab << endl;

since the bool is internally converted as 1 and 0. So why the size of
bool is one?
Because 1 is the smalles possible value. Theoretically a bool does not
need to take more space than on bit, but due to alignment requirements
and such that would leave at least 7 bits in that byte unused, so the
effective space used would still be at least one byte. Note though that
the 1 of sizeof(bool) does not mean 1 byte, but rather a piece of memory
as large as a char (which is the smallest addressable unit in C++),
which just happens to be one byte on most machines.

--
Erik Wikström
Mar 14 '07 #6

P: n/a
On 14 Mar, 13:53, Erik Wikström <Erik-wikst...@telia.comwrote:
Note though that
the 1 of sizeof(bool) does not mean 1 byte,
Yeah it does. sizeof returns a number of bytes ...
but rather a piece of memory
as large as a char (which is the smallest addressable unit in C++),
.... and a char is 1 byte
which just happens to be one byte on most machines.
Maybe there could be a difference between the size of a byte as
understood by the C++ memory model (section 1.7 of the standard) and a
byte as understood by the people who designed the target processor -
then it would be the compiler's job to map between the two in a manner
opaque to the programmer. But within the scope of the C++ standard,
sizeof returns a number of bytes dy definition and a char is 1 byte by
definition.

Gavin Deane

Mar 14 '07 #7

P: n/a
Erik Wikström wrote:
>
Because 1 is the smalles possible value. Theoretically a bool does not
need to take more space than on bit, but due to alignment requirements
and such that would leave at least 7 bits in that byte unused, so the
effective space used would still be at least one byte. Note though that
the 1 of sizeof(bool) does not mean 1 byte, but rather a piece of memory
as large as a char (which is the smallest addressable unit in C++),
which just happens to be one byte on most machines.
The above is wrong on several accounts.

First, byte in C++ is defined in terms of char. A char
variable by definition in the language is one byte.
This unfortunately is a major defect in C++ portability.
char plays double duty:

1. It's the native character type.
2. It's the smallest addressable storage unit.

When all the world is eight bit ascii this is not an
issue. It's problematic when the native character
is LARGER than the minimal addressable storage
(for example a machine with addressable units
of 8 bits, but the native char wants to be 16).
wchar_t is a poor substitute as there are a number
of important interfaces that do not have wchar_t
analogs. You have to kludge it by inventing
a multibyte (conversion of the wchar_t type to
a sequence of chars) even when such a thing
doesn't uniquely exist.

Further, no C++ type can be less than the byte
size (sizeof char). This is also fundamanetal.
It has nothing to do with alignment. It has to
do with the fact that C++'s pointer behavior
assumes that things aren't smaller than char.
Mar 15 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.