Is it guaranteed safe/portable to use the address of a function parameter on a C89/C99-compliant compiler?
As an example, the AAPCS for 32-bit ARM uses registers r0-r3 for parameter passing if the function parameters meet specific size and alignment requirements. I would assume that using the address of a parameter passed through a register would yield unexpected results, but I ran a test on the ARM compiler I'm using and it appears to relocate these parameters to the stack if the code attempts to reference the addresses of these parameter. While it would appear safe in my particular application, I'm wondering if this is guaranteed across architectures (with an ANSI/ISO-compliant compiler) that can utilize registers directly to pass function parameters.
Do the standards define this behavior?
In C, the only lvalues you cannot take addresses of are bitfields (which cannot appear in function parameters) and variables or function parameters of register
storage class. It is perfectly safe to take the address of a parameter, but keep in mind that arguments are passed by value, thus you must make sure that you don't use the address of a local variable or parameter once its life time ends.
Generally, the compiler has a pass where it checks which local variables and parameters are operands to unary &
operators. These are then copied to a suitable piece of RAM when appropriate. The calling convention does not affect this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With