<ma******@gmail .com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
Hello,
I saw in some open source projects a use of "!!" in "C" code;
for example:
in some header file
#define event_pending(v ) \
(!!(v)->vcpu_info->evtchn_upcall_ pending & \
!(v)->vcpu_info->evtchn_upcall_ mask)
whereas evtchn_upcall_p ending is of type unsigned char
(and also evtchn_upcall_m ask is of type unsigned char).
What does "!!" operator do in this case ? Any ideas?
MR
!! is a good way of turning a scalar value into 1 or 0.
For example, this
printf("%p\n", (void *)"boo");
will output some value that's not 0, and very unlikely to be 1, e.g.,
0040300
However, this
printf("%p\n", (void *)!!"boo");
will output 000001, e.g., '1'
I've seen/used !! in functions that allocate memory, and return 1 if the
memory was allocated ok, else 0.
A routine like this would look something like ...
void * p;
int allocStuff(size _t n)
{
p = malloc(n);
return !!p;
}
int main(void)
{
if(allocStuff(. ..))
{
}
else
{
}
}
Although the code above would work if allocStuff simply returned the actual
pointer value, it would stop someone from finding out that fact, and doing
something like this ...
int main(void)
{
void * p1;
if(p1 = allocStuff(...) )
{
// Aha, we've now got a pointer to the memory allocated!!!
}
else
{
}
}
Yes, in the code above [first], you'd also get the same thing by accessing
p, but I hope you get the gist of this simple example, and see how it could
be extended?