Home > Enterprise >  Is 'bool' 1 or 2 bytes in C Builder? I'm getting conflicting results
Is 'bool' 1 or 2 bytes in C Builder? I'm getting conflicting results

Time:04-13

The following code returns a bool in the AL CPU register:

bool Exiting;
bool WINAPI IsExiting()
{
   return Exiting;
}

which compiles to:

00488BA4 55               push ebp
00488BA5 8BEC             mov ebp,esp
00488BA7 6843475300       push $00534743
00488BAC 83C4F4           add esp,-$0c
00488BAF C745F8CC8B4800   mov [ebp-$08],$00488bcc
00488BB6 C745F001000000   mov [ebp-$10],$00000001
00488BBD A0B0C0A900       mov al,[$00a9c0b0] <------------------ AL has the result
00488BC2 896DFC           mov [ebp-$04],ebp
00488BC5 896DFC           mov [ebp-$04],ebp
00488BC8 8BE5             mov esp,ebp
00488BCA 5D               pop ebp
00488BCB C3               ret 

But I've found when calling it:

if (!IsExiting()) {
  task1
}
if (!IsExiting()) {
  task2
}

task1 works, task2 doesn't always. The reason is:

if (!IsExiting())

compiles down to:

0040419E E8014A0800       call IsExiting()
004041A3 6685C0           test ax,ax     <------ bool is stored in AL, but test is on AX
004041A6 755C             jnz $00404204

Am I missing something? AX is 16 bits, AL is 8 bits.

Is this a compiler error, or am I being stupid somehow?

CodePudding user response:

Turns out, I had a difference in the function declaration. For reasons I was not able to include the header file of the IsExiting, which correctly declared it as

bool WINAPI IsExiting();

But the function was declared (incorrectly) locally in the calling file as

short WINAPI IsExiting();

I was being stupid somehow. (funny how by asking the question on StackOverflow, you are then able to find the answer).

CodePudding user response:

Short answer. Stop using WINAPI in your functions.

Long answer. You should avoid using WINAPI in your functions. You may use a similar macro only for functions of a public API and should use another than WINAPI name. The calling convention for private functions should be set up in the project/compiler settings.

  • Related