[LLVMdev] Reference Manual Clarifications 2

Jon Sargeant delta17 at cox.net
Sun Apr 20 09:34:25 PDT 2008


Joachim Durchholz wrote:
> Am Samstag, den 19.04.2008, 16:24 -0700 schrieb Jon Sargeant:
>> First, I can assign -1 to 
>> the count to indicate an invalid or unknown value.
> 
> This is a C-ism. In a language that supports discriminated unions well,
> you'd do something like
>   type AllocaCount = Invalid | Unknown | Known int
> (where Invalid, Unknown and Known are the constants that do the
> distinction between union variants).

Not necessarily.  Using -1 for an invalid integer is analogous to using 
null for an invalid pointer.

>> Second, if I attempt 
>> to allocate a negative count, I can print an assertion failure and abort 
>> the program.  Had I interpreted the count as an unsigned value, the 
>> program would attempt to allocate anywhere from 2 gigabytes to 4 
>> gigabytes.
> 
> Which might be exactly what it's supposed to do. Suppose you're writing
> heap management code.

Perhaps, but very unlikely.  An allocation of 2 gigabytes or more is 
probably a bug.

>> I'm not necessarily saying that NumElements should be 
>> signed, only that the choice between signed and unsigned is not obvious.
> 
> Obviously, obviousness is in the eye of the beholder :-)
> (SCNR)

Yes.  But consider that there are many people who agree with me.  Search 
for "unsigned vs signed - Is Bjarne Mistaken?" in comp.lang.c++.moderated.

Best Regards,
Jon



More information about the llvm-dev mailing list