Sign in to follow this  
Silly_con

about ansi/iso C++

Recommended Posts

Silly_con    136
Is sizeof(bool) = 1 defined in standart ? it would be more efficient sizeof(bool) = sizeof(int) in almost all systems. Actually, if new get out of memory, is standart that is return 0 and throw exception std::bad_alloc, or only throw exception std::bad_alloc ? thnx

Share this post


Link to post
Share on other sites
Enigma    1410
I believe it is sizeof(bool) == sizeof(char), though I wouldn't stake my life on that. If you want a four byte bool on a standard 32 bit architecture then just use an int. 'Efficiency' is ill-defined. Certainly four-byte aligned datatypes are often faster on 32 bit architectures, but memory-wise single byte types are much more 'efficient'.

If new is unable to allocate memory then it will throw an exception, according to the standard. There is also a nothrow form on new that will return null. I believe VisualC++ may use the non-throwing form by default (I'm a little out of touch with this though, it may have changed). A function can never throw and return. Either it throws or it returns (or it sits in an infinite loop or it crashes horrible, but we prefer to think that it'll be one of the first two alternatives).

Enigma

Share this post


Link to post
Share on other sites
noVum    170
At least VC++ does use sizeof(int) for bool, so I think the standard doesn't specify a size.

Quote:
Actually, if new get out of memory, is standarD that is return 0 and throw exception std::bad_alloc, or only throw exception std::bad_alloc ?

The standard says that it only throws an exception, which makes sense. If you catch the exception you know that something is wrong with the pointer and you couldn't use it anyway.

Share this post


Link to post
Share on other sites
Enigma    1410
Quote:
Original post by noVum
At least VC++ does use sizeof(int) for bool, so I think the standard doesn't specify a size.


Upon doing a little investigating I think you're right there.

Enigma

Share this post


Link to post
Share on other sites
Jingo    582
The standard specifically says its implementation defined:

Quote:
5.3.3
...in particular, sizeof(bool) and sizeof(wchar_t) are implementation-defined.


It may be 1 in VC++ 6 and .NET, but you cannot rely on it being so.

Share this post


Link to post
Share on other sites
Washu    7829
Quote:
Original post by Silly_con
Is sizeof(bool) = 1 defined in standart ? it would be more efficient sizeof(bool) = sizeof(int) in almost all systems.

Implementation defined.
Quote:

Actually, if new get out of memory, is standart that is return 0 and throw exception std::bad_alloc, or only throw exception std::bad_alloc ?

thnx

You can't return AND throw an exception. standard new will throw an exception, unless you use the std::nothrow variant, in which case it returns a 0.

Share this post


Link to post
Share on other sites
noVum    170
Quote:
Original post by Silly_con
I do sizeof(bool) in vc6 and vc.net2003 and I get 1, sizeof(char)

You are right. It's the other way around, GCC uses 4 bytes for bool.

Share this post


Link to post
Share on other sites
sit    170
regardless of the sizeof a particular data type, it will probably be stored in 4 bytes if smaller than 4 [and not in an array]

for example:

struct whatever {
bool bar;
int foo;
} ;

cout << sizeof(whatever) << " " << sizeof(bool) << endl;

in visual studio .NET 2003 it printed "8 1", regardless of the sizeof a bool being a byte, it essentially took up 4

what really happens is that the start of variables by default occur on word [4 byte, 32 bit] boundries.

so, this struct would have the same size [and no unused bytes]:
struct whatever2 {
bool bar[4];
int foo;
} ;

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this