Boolean Values

On 06/09/2013 at 06:27, xxxxxxxx wrote:

User Information:
Cinema 4D Version:   R14 
Platform:   Windows  ;   
Language(s) :     C++  ;

Hi folks,

I was under the impression that a Boolean's value would be either TRUE or FALSE - or numerically 1 or 0.

I'm printing a bool value and it's returning -1, after it's been defined as FALSE. I haven't yet found anything in my code that seems to change this value anywhere else (but I'm still checking up on this).

So my query here is, under what circumstances might a bool's value return -1, if it's not changed code-wise or by the user somehow? Or should this simply not happen?


On 06/09/2013 at 06:37, xxxxxxxx wrote:

Any non-zero value is a TRUE value. A Cinema 4D boolean is a 4-byte integer (just like LONG). It is, however, weird that it prints out -1 when you defined it as FALSE. Could you share the exact lines?


On 06/09/2013 at 11:15, xxxxxxxx wrote:

Hi Niklas,

yeah - I thought non-zero values were meant to be TRUE too. But in my code I've got an if statement like:

if(MyStr.mybool == TRUE){...}   

and it's probably not running correctly because TRUE itself is defined as 1, and not 'a number other than 0'. (Is this thinking right?)

I think I may know where the problem lies in my code though... It seems as though some trickery in the way I've done some of my coding is letting some things display (giving the impression that the struct data the user areas are getting is fine) when the pointers to them might actually not be right. Not sure if that explains the -1 or not...

I'm going to be away the next few days due to travel, I'll see if I can rectify this in the meantime and conclude if this is where the problem is lying... Thanks again,


On 08/09/2013 at 10:08, xxxxxxxx wrote:

Yes, comparing against TRUE is dangerous as any non-zero value is defined being a boolean TRUE
value, but the constant TRUE itself is defined 1. Comparing against FALSE however is perfectly safe.

if (MyStr.mybool != FALSE)

This reads much harder in any case, imho it's best practice to just write

if (MyStr.mybool)

I do also prefer this style for pointers

Blahrg* some_pointer = // ...
if (some_pointer) ...
if (!some_pointer) ...

Best regards,

On 08/09/2013 at 23:08, xxxxxxxx wrote:

While there is no standard definition in computer programming, as long as I have programmed computers, FALSE and NULL were associated with 0 (0x00 to as many bits as the type, NULL representing a memory address reserved specifically as 0x00000000 (32-bit) and so on).  You can practically never go wrong checking for FALSE or NULL.  This follows in the use of ASCII and related character sequences (strings) which are 'NULL-terminated' using a 0-valued character.  Simplest check that just about every compiler or interpreter for most languages understands:

if (!pMyPointer) ... // Pointer to NULL  
if (!bMyBoolean) ... // bMyBoolean equals FALSE