pure / const on function "return"ing an array?

Ian Lance Taylor iant@google.com
Tue Jan 18 14:15:00 GMT 2011


"Kevin P. Fleming" <kpfleming@digium.com> writes:

> On 01/17/2011 05:56 PM, Jonathan Wakely wrote:
>> On 17 January 2011 23:04, tom fogal wrote:
>>> I have a function with this prototype:
>>>
>>>   void to_3d(hsize_t index, const hsize_t dims[3], hsize_t outdex[3]);
>>>
>>> The effect of the function is to store appropriate values in the
>>> "outdex" array.  It does not read or write global memory.
>>>
>>> I would like to mark this as __attribute__((const)), but it doesn't
>>> quite fit the definition, as it's a void function.  Obviously I can't
>>> just change it to make `outdex' the return value, since outdex is an
>>> array.
>>
>> Isn't outdex actually a pointer? That's another reason the function
>> can't be marked const.
>
> It's a pointer, but the pointer is caller-provided, so I would think
> the compiler could make a reasonable assumption that it "knows" the
> only memory locations the called function is going to be able to
> modify if it is marked 'const'.

The documentation is clear that a function with a pointer argument may
not be declared as const, but it may be declared as pure.  E.g., the
strlen function is pure but not const.

Ian



More information about the Gcc-help mailing list