C Is this function "pure"?

Crivens Indeed that would work and I briefly thought about it ;) I wouldn't call it cheating either ... but defining a new type just for the chance to make this one function "pure"? Maybe not ;) I'll just not add the attribute...
 
But anyways, if you drop this "pointer issue", you're just confirming what I concluded in post #15. An "output parameter" breaks guarantees this attribute is supposed to grant.
No? Your use of the term "output parameter" is obscuring the word "pointer." Theoretical term vs technical term.
 
Oh, you're referencing text from discussing a concrete example. That can easily be misleading (e.g. because in the example, we ARE talking about an actual parameter the function works on).

But in this case, as now already stated multiple times, it probably applies. There is the "access" attribute telling the compiler that something is an output parameter, but as it isn't referenced in "pure", you can't rely on it.

You wrote multiple times "no pointers". That's not correct. Pointers aren't a problem. Modifying memory from within the function is.
 
I mean, what do you even want here? An award for rules lawyering the phrase "modifying the state of the program that is observable by means other than inspecting the function’s return value"? You should have absolutely switched to the method Crivens suggested to achieve simple compliance.

I'm saying don't use pointers with pure and you're saying you can use them if you follow some rules the compiler uses that you've found out aren't even particularly clear in the documentation, your own example violates the explicit exception to a no-pointers rule outlined in the documentation, it probably require analysis of the compiler to determine if your example even can work the way you want it to, and any discussion involving clearing this up would probably be more helpful on the GCC mailing lists.

Simple would probably be a better use of time.
  1. Don't use pure (premature optimization fallacy)
  2. Your program has a hotspot in this location? Use the struct and then mark it as pure.
  3. Whip GCC into doing what you want by submitting a patch... wait, you already solved your problem in a simpler fashion with #2
 
msplsh if you have no interest in a somewhat theoretical topic (that btw, repeating myself again, is IMHO fully solved and correctly explained by now), why do you participate?

Just throwing in simple truths that, on a closer look, aren't that simple after all doesn't really add something here.
 
I think it was pretty clear I wanted an answer to a question, not to solve some concrete problem... and it's a question with potential for some "debate", which I think is interesting, but not everyone has to agree :eek:

What might not have been clear enough was the scope (specifically the GNU-style attribute), which I tried to fix after getting pretty diverse answers. Yes, that's probably my fault. It turns out that, although a "pure function" is well-defined in a mathematical way, you can come to very different interpretations looking at (technical) code, depending on the abstraction level you use.

I also think that the example I found one day later that could lead to an incorrect program when the guarantees expected by the "pure" attribute are applied answers the initial question, but maybe someone still wants to comment on that...

Just regarding "premature optimization": Yes, changing the function in a way the "pure" attribute can be safely applied in order to enable compiler optimizations would be an example, but applying attributes in general isn't. Attributes help in several ways:
  • The programmer using an API to better understand its semantics
  • The compiler to better detect opportunities for optimizations
  • Both of them by enabling the compiler to issue better diagnostics
I actually started to apply attributes for the third reason. I once accidentally introduced a bug with a refactoring: Some function for formatted logging suddenly had an argument of the wrong type. With printf() and friends, compilers already know their semantics and issue warnings about that, but for other functions, an attribute ("format") is needed to let the compiler know.
 
Back
Top