Jump to content
  • Advertisement
Sign in to follow this  
Silly_con

about ansi/iso C++

This topic is 5142 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement
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
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
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
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
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
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!