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

bool

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

Similar topics

3
by: Pierre Espenan | last post by:
A have a long integer class. The built integer type within a conditional statement returns bool false for int i=0 and bool true for any other non zero value. I want my long integer class to have...
4
by: Nomak | last post by:
Hello, With this code: $ cat -n ifs.cc 1 #include <vector> 2 #include <iostream> 3 4 using std::vector; 5 using std::cin;
19
by: daniel | last post by:
1) is C++ smart enough to automatically use "bits" for bool or will a bool have the size of a charcter (byte). 2) The index of a vector is it an integer (4 byte) or a "long long" with 8 bytes or...
4
by: ORC | last post by:
Is the bool type actually an Int32 with 0 as false and non zero as true? The reason for my question is that I've seen a lot of API calls that return a Int32 implemented in C# as an bool like: ...
10
by: Mark Jerde | last post by:
I'm trying to learn the very basics of using an unmanaged C++ DLL from C#. This morning I thought I was getting somewhere, successfully getting back the correct answers to a C++ " int SumArray(int...
1
by: Bern McCarty | last post by:
What do you make of this? I cannot tell for sure but it almost seems as the the transition thunk to get back from the native bool method to the managed caller is looking at eax and, if any bit is...
4
by: gpg | last post by:
I am using a legacy DLL and need to marshal some structures for use in the DLL. For the most part, I have figured out my needs except for one small item. I have a structure that contain, among...
6
by: zl2k | last post by:
hi, there I am using a big, sparse binary array (size of 256^3). The size may be changed in run time. I first thought about using the bitset but found its size is unchangeable. If I use the...
64
by: shaanxxx | last post by:
I have code which says #define MYBOOL int This code is very old. people who have written is not avaible. I was thinking what could be reason. 1) bool datatype was not available that time (10...
3
by: markb | last post by:
Hi My C# app is being called from a callback from an unmanaged DLL. One of the parameters of the callback is of type BOOL. I am using PInvoke to marshal this to a (managed) bool. The problem is...
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: 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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.