Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type casting a variable from jBoolean to bool

When using JNI to interface between Java and C, javah parses a boolean value in Java to jBoolean in the JNI header file. When I use bool in the C file, the Visual studio compiler throws a warning that

warning C4800: 'jboolean' : forcing value to bool 'true' or 'false' (performance warning)

Is there any other data type that should be used? Or if bool is the only data type here, what exactly are the performance problems that I might face?

like image 570
Balanivash Avatar asked Jun 13 '11 04:06

Balanivash


2 Answers

The "performance problem" is that the cast isn't completely free. Casting to bool essentially means forcing all non-zero values to 1, which takes a tiny bit of code, as opposed to a "free" cast like widening char to int. The "performance problem" is a few extra machine instructions. If you're doing this a million times in a tight loop, OK, maybe you care -- otherwise, no, you shouldn't care at all. This is IMO a silly compiler warning; that small added cost is simply part of the compromise you make when using bool and the compiler shouldn't be bothering you about it.

like image 63
Ernest Friedman-Hill Avatar answered Nov 09 '22 06:11

Ernest Friedman-Hill


Try casting to BOOL instead. You might find this thread interesting too: Difference between bool and BOOL.

I'm not quite sure this is what you are looking for however. BOOL is 16 bits, not 8 bits. If that is okay then it is closer to what you're looking for than bool. Otherwise you could use byte. Note if you do end up casting to any of these you won't be able to do == true anymore. This is also explained in the link I provided.

like image 31
Mike Kwan Avatar answered Nov 09 '22 06:11

Mike Kwan