Reputation: 39
bool SomeFunction()
{
}
I cannot run the Borland C++ on my machine but I need to convert from C++ to VB so need help with this function.
Upvotes: 2
Views: 1392
Reputation: 633
I've compiled the following code on Borland XE2:
bool SomeFunction()
{
}
int main()
{
bool x = SomeFunction();
// ...
}
SomeFunction()
translated to the following x86 assembler code:
push ebp
mov ebp,esp
pop ebp
ret
The assignment in main()
translated to:
call SomeFunction()
mov [ebp-$31],al
where [ebp-$31]
is the location of x
. That means, the content of register al
will end up in bool x
. If al
was 0, x
will be false, otherwise x
will be true. On my system, this was always true, but this depends on the context. Also you may get different results for debug and release.
The conclusion is, of course, x is undefined. The given code is a bit like writing
bool x;
if (x)
{
// ...
}
I am inclined to think that the definition of SomeFunction()
should trigger not just a compiler warning, but an error. Visual C++ does so, I don't know about other compilers.
Upvotes: 1
Reputation: 8587
It should return true;
or return false;
bool SomeFunction()
{
return true;
// or
return false;
}
If your compiler does not have bool built in then you can do this:
typedef int bool;
#define true 1
#define false 0
int main(void)
{
bool answer;
answer = true;
while(true)
{
}
return 0;
}
Upvotes: -1
Reputation: 227578
The function claims it returns a bool
but it returns nothing. This should result in a compiler warning. If you use it to assign to something call the function, the result will be undefined behaviour:
bool b = SomeFunction(); // UB, SomeFunction is failing to return.
SomeFunction(); // still undefined behaviour
Only main()
is allowed not to explicitly return, in which case it implicitly returns 0
.
See here:
§6.6.3/2:
Flowing off the end of a function is equivalent to a return with no value; this results in undefined behavior in a value-returning function.
Upvotes: 10