BOOL is a GHOUL, was:MEMORY corruption and windows programming ???

new topic     » topic index » view thread      » older message » newer message

Matt Lewis wrote:
> 
> 
> > From: Bernie Ryan [mailto:xotron at bluefrognet.net]
> 
> > Below is what MSDN states about BOOL type.
> > Does anyone have any idea about how we can write windows code
> > that will be compatible and not cause memory corruption errors
> > when runing euphoria code.
> > What size does everyone use for bool in a structure that will
> > always work ??  
> > 
> > Bernie
> > 
> > " Microsoft Specific
> > In Visual C++4.2, the Standard C++ header files contained a 
> > typedef that equated bool with int. In Visual C++ 5.0 and
> > later, bool is implemented as a built-in type with a size
> > of 1 byte. That means that for Visual C++ 4.2, a call of
> > sizeof(bool) yields 4, while in Visual C++ 5.0 and later, the
> > same call yields 1. This can cause memory corruption problems 
> > if you have defined structure members of type bool in Visual
> > C++ 4.2 and are mixing object files (OBJ) and/or DLLs built
> > with the 4.2 and 5.0 or later compilers. 
> > END Microsoft Specific "
> 
> IIRC, the only time I've had to worry about a BOOL being 1 byte is when
> there are multiple BOOL's next to each other, since the compiler seems 
> to
> like to pad extra bytes in order to get everything else 4-byte aligned.  
> By
> default, I assume a 4-byte BOOL, until I crash and burn. :)
> 
> Matt Lewis
> 

The old rule of thumb when dealing with Windows bool types:

    "BOOL is a GHOUL"

blink


Hello again Matt, Bernie, and Igor,

My previous post didnt make it through, sorry about that.

'BOOL' is 4 byte type long,
 while
'boolean' is 1 byte.

As far as i know, the type used in Win API calls is
always type BOOL, not boolean.  All of the h files ive examined
all have BOOL, not boolean.

Since there is a host of other "bools" out there too,
the only way to be sure is to double check the documentation
(ouch) and see what value is being returned, and not to
take it for granted that any bool, BOOL, _BOOL, __BOOL,
_BOOL_, or any other type like that will only return 0 or 1.

Sometimes the type will really be BOOL, but it returns 
0,1, or -1 
as in the famous
"GetMessage()"

This means if you use a pointer, you'll have to check
for #FFFFFFFF (same if you use C_LONG).
Probably we should have been using type C_LONG
for BOOL when dealing with the GetMessage function.

I've been using C_POINTER or C_ULONG and never had a problem,
but my message loops were always set up so there could
never (?) be an error with GetMessage().

Microsofts documentation sometimes bites as you probably know smile
I guess on a percentage basis, it's pretty good though.

BTW, Igor, i wanted to discuss your philosophical issues
in more depth, but it takes so much time on here.

Also, we 'Germans' have the math thing down pat smile hee hee.


Take care and good luck with your ghouls, err, ..., um, ...,
i mean 'bools'.

Al

new topic     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu