Memory allocation purity

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Thu May 15 06:47:48 PDT 2014


On Thu, 15 May 2014 09:40:03 -0400, Manu via Digitalmars-d  
<digitalmars-d at puremagic.com> wrote:

> On 15 May 2014 22:30, Steven Schveighoffer via Digitalmars-d
> <digitalmars-d at puremagic.com> wrote:
>> On Thu, 15 May 2014 07:52:20 -0400, Manu via Digitalmars-d
>> <digitalmars-d at puremagic.com> wrote:
>>
>>> On 15 May 2014 10:50, Walter Bright via Digitalmars-d
>>> <digitalmars-d at puremagic.com> wrote:
>>>>
>>>> On 5/14/2014 5:03 PM, Meta wrote:
>>>>>
>>>>>
>>>>> Allocating memory through new and malloc should always be pure, I  
>>>>> think,
>>>>> because
>>>>> failure either returns null in malloc's case,
>>>>
>>>>
>>>>
>>>> malloc cannot be pure if, with the same arguments, it returns null
>>>> sometimes
>>>> and not other times.
>>>
>>>
>>> Even if it doesn't fail, malloc called with identical arguments still
>>> returns a different result every time.
>>> You can't factor malloc outside a loop and cache the result because
>>> it's being called repeatedly with the same argument like you're
>>> supposed to be able to do with any other pure function.
>>> You shouldn't be able to do that with gcalloc either... how can  
>>> gcalloc be
>>> pure?
>>
>>
>> That only applies to strong-pure functions. malloc would be weak-pure,  
>> since
>> it returns a mutable pointer.
>
> Why should returning a mutable pointer imply weak purity?

Because if it were strong-pure, the compiler could optimize two sequential  
calls as returning the same exact thing. Imagine if a constructor to a  
mutable object always returned the same object because the constructor's  
parameters were immutable. It would be useless.

-Steve


More information about the Digitalmars-d mailing list