public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* pure / const on function "return"ing an array?
@ 2011-01-17 23:06 tom fogal
  2011-01-17 23:57 ` Jonathan Wakely
  0 siblings, 1 reply; 4+ messages in thread
From: tom fogal @ 2011-01-17 23:06 UTC (permalink / raw)
  To: gcc-help

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.

Is there anything else I could do in such a situation?

Thanks,

-tom

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: pure / const on function "return"ing an array?
  2011-01-17 23:06 pure / const on function "return"ing an array? tom fogal
@ 2011-01-17 23:57 ` Jonathan Wakely
  2011-01-18 12:07   ` Kevin P. Fleming
  0 siblings, 1 reply; 4+ messages in thread
From: Jonathan Wakely @ 2011-01-17 23:57 UTC (permalink / raw)
  To: tfogal; +Cc: gcc-help

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.

> Is there anything else I could do in such a situation?

If the array bounds are fixed (as in your example above) you could
change the function to return a struct with an array member, but you'd
have to replace the dims pointer parameter too, and I have no idea if
the optimizer will do anything for const functions with struct
parameters and return types.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: pure / const on function "return"ing an array?
  2011-01-17 23:57 ` Jonathan Wakely
@ 2011-01-18 12:07   ` Kevin P. Fleming
  2011-01-18 14:15     ` Ian Lance Taylor
  0 siblings, 1 reply; 4+ messages in thread
From: Kevin P. Fleming @ 2011-01-18 12:07 UTC (permalink / raw)
  To: gcc-help

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'.

-- 
Kevin P. Fleming
Digium, Inc. | Director of Software Technologies
445 Jan Davis Drive NW - Huntsville, AL 35806 - USA
skype: kpfleming | jabber: kfleming@digium.com
Check us out at www.digium.com & www.asterisk.org

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: pure / const on function "return"ing an array?
  2011-01-18 12:07   ` Kevin P. Fleming
@ 2011-01-18 14:15     ` Ian Lance Taylor
  0 siblings, 0 replies; 4+ messages in thread
From: Ian Lance Taylor @ 2011-01-18 14:15 UTC (permalink / raw)
  To: Kevin P. Fleming; +Cc: gcc-help

"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

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2011-01-18 14:15 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-01-17 23:06 pure / const on function "return"ing an array? tom fogal
2011-01-17 23:57 ` Jonathan Wakely
2011-01-18 12:07   ` Kevin P. Fleming
2011-01-18 14:15     ` Ian Lance Taylor

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).