__attribute__((const)) vs __attribute__((pure)) in GNU C

From the documentation for the ARM compiler (which is based on gcc):

__attribute__((pure)) function attribute
Many functions have no effects except to return a value, and their return value depends only on the parameters and global variables. Functions of this kind can be subject to data flow analysis and might be eliminated.

__attribute__((const)) function attribute
Many functions examine only the arguments passed to them, and have no effects except for the return value. This is a much stricter class than __attribute__((pure)), because a function is not permitted to read global memory. If a function is known to operate only on its arguments then it can be subject to common sub-expression elimination and loop optimizations.

So, TL;DR: __attribute__((const)) is the same as __attribute__((pure)) but without any access to global variables.

Leave a Comment