Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting bool from C to C++ and back

Tags:

c++

c

When designing data structures which are to be passed through a C API which connects C and C++ code, is it safe to use bool? That is, if I have a struct like this:

struct foo {   int bar;   bool baz; }; 

is it guaranteed that the size and meaning of baz as well as its position within foo are interpreted in the same way by C (where it's a _Bool) and by C++?

We are considering to do this on a single platform (GCC for Debian 8 on a Beaglebone) with both C and C++ code compiled by the same GCC version (as C99 and C++11, respectively). General comments are welcome as well, though.

like image 381
sbi Avatar asked Oct 13 '16 12:10

sbi


People also ask

Can you return a bool in C?

In C, most things we think of as boolean are actually int (0 or 1). We prefer to use bool return type for functions which have 2 return values ( true or false ).

Why is there no bool in C?

C does not have boolean data types, and normally uses integers for boolean testing. Zero is used to represent false, and One is used to represent true. For interpretation, Zero is interpreted as false and anything non-zero is interpreted as true.

Are there Boolean functions in C?

In C the terminology of boolean is associated with data type and Boolean is termed as one of the data types in the C Standard Library. This data type stores one of the two possible values denoted by true or false, which represents two truth values of logic and Boolean Algebra.

When did bool add C?

An introduction to how to use booleans in C C99, the version of C released in 1999/2000, introduced a boolean type. To use it, however, you need to import a header file, so I'm not sure we can technically call it “native”.


1 Answers

C's and C++'s bool type are different, but, as long as you stick to the same compiler (in your case, gcc), it should be safe, as this is a reasonable common scenario.

In C++, bool has always been a keyword. C didn't have one until C99, where they introduced the keyword _Bool (because people used to typedef or #define bool as int or char in C89 code, so directly adding bool as a keyword would break existing code); there is the header stdbool.h which should, in C, have a typedef or #define from _Bool to bool. Take a look at yours; GCC's implementation looks like this:

/*  * ISO C Standard:  7.16  Boolean type and values  <stdbool.h>  */  #ifndef _STDBOOL_H #define _STDBOOL_H  #ifndef __cplusplus  #define bool        _Bool #define true        1 #define false        0  #else /* __cplusplus */  /* Supporting <stdbool.h> in C++ is a GCC extension.  */ #define _Bool        bool #define bool        bool #define false        false #define true        true  #endif /* __cplusplus */  /* Signal that all the definitions are present.  */ #define __bool_true_false_are_defined        1  #endif        /* stdbool.h */ 

Which leads us to believe that, at least in GCC, the two types are compatible (in both size and alignment, so that the struct layout will remain the same).

Also worth noting, the Itanium ABI, which is used by GCC and most other compilers (except Visual Studio; as noted by Matthieu M. in the comments below) on many platforms, specifies that _Bool and bool follow the same rules. This is a strong garantee. A third hint we can get is from Objective-C's reference manual, which says that for Objective-C and Objective-C++, which respect C's and C++'s conventions respectively, bool and _Bool are equivalent; so I'd pretty much say that, though the standards do not guarantee this, you can assume that yes, they are equivalent.

Edit:

If the standard does not guarantee that _Bool and bool will be compatible (in size, alignment, and padding), what does?

When we say those things are "architecture dependent", we actually mean that they are ABI dependent. Every compiler implements one or more ABIs, and two compilers (or versions of the same compiler) are said to be compatible if they implement the same ABI. Since it is expected to call C code from C++, as this is ubiquitously common, all C++ ABIs I've ever heard of extend the local C ABI.

Since OP asked about Beaglebone, we must check the ARM ABI, most specifically the GNU ARM EABI used by Debian. As noted by Justin Time in the comments, the ARM ABI indeed declares C++'s ABI to extend C's, and that _Bool and bool are compatible, both being of size 1, alignment 1, representing a machine's unsigned byte. So the answer to the question, on the Beaglebone, yes, _Bool and bool are compatible.

like image 112
paulotorrens Avatar answered Sep 22 '22 15:09

paulotorrens