A bool is only allowed to be zero or one. The C++ standard states that the
behavior of a program when a bool has any other value is undefined.
Therefore, the behavior you are seeing -- where branches for if(x) and
if(!x) are both taken -- is actually standard-compliant compiler behavior
given that the bool has an invalid value.
The protobuf implementation will never assign such an invalid value to a
boolean, though. So, I suspect memory corruption (probably in your code) is
at work here. If you believe otherwise, please give us a small
self-contained example program demonstrating the problem. (Please reduce
your code to as few lines as possible -- while keeping it able to compile
and run -- before sending it.)
On Tue, Jun 15, 2010 at 5:50 PM, mo wrote:
> This is simplified version of my code:
>
>if ( !message.ParsePartialFromArray((void *) buffer, len ) ||
> !message.IsInitialized() ) {
> // error
> return;
>}
> std::cout << "is_valid value: " << message.is_valid() << "\n";
>if( ! message.has_is_valid() ) {
> std::cout << " does not have is_valid\n";
>}
>if( message.is_valid() ) {
> std::cout << " IS valid\n";
>}
>if( ! message.is_valid() ) {
> std::cout << " IS NOT valid\n";
>}
>
> and the proto buf that I have is something similar to:
>
> message foo {
> ...
>optional bool is_valid = 7 [default = true];
> }
>
> and the generated code for it is:
>
> static const int kIsValidFieldNumber = 7;
> inline bool is_valid() const;
> ..
>
> The messages that I am trying to parse do not have the attribute
> is_valid set in them. The output of my code is:
>
> is_valid value: 15
> does not have is_valid
> IS valid
> IS NOT valid
>
> so:
> 1) how does the is_valid() function return 15, as opposed to 1 or 0
> (bool).
> 2) how could both is_valid() and !is_valid() be true? (I guess this is
> more like a c question :) ). Even if we assume that there is a memory
> corruption somewhere, it is hard to imagine that the compiler is
> looking in to 2 different memory locations for is_valid() and !
> is_valid() calls, so how does it return true for both?
>
> -M
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Protocol Buffers" group.
> To post to this group, send email to proto...@googlegroups.com.
> To unsubscribe from this group, send email to
> protobuf+unsubscr...@googlegroups.com
> .
> For more options, visit this group at
> http://groups.google.com/group/protobuf?hl=en.
>
>
--
You received this message because you are subscribed to the Google Groups
"Protocol Buffers" group.
To post to this group, send email to proto...@googlegroups.com.
To unsubscribe from this group, send email to
protobuf+unsubscr...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/protobuf?hl=en.