views:

157

answers:

3

I've just found this function definition in some embedded code:

float round_float_to_4(static float inputval);

I'm familiar with other uses for static (global variables, functions and local variables), but this is the first time I see it as specifier for function parameter. I assume that this forces compiler to use fixed memory location for inputval instead of stack?

+4  A: 

This is non standard. I'd guess the same thing as you, and I'm not surprised of such extension in compilers having an embedded target.

AProgrammer
You are right. I checked the compiler's (Microchip C18) manual and found this: "Function parameters can have storage class auto or static. An auto parameter is placed on the software stack, enabling reentrancy. A static parameter is allocated globally, enabling direct access for generally smaller code."
Josip
Nice. +1 for the correct answer :)
Johannes Schaub - litb
+1  A: 

That's not valid. The only valid place where static may be used in a function parameter i'm aware of is in an array dimension

float round_float_to_4(float inputval[static 4]);

Saying that inputval will, in all calls to this function, point to memory providing at least 4 floats (this is a C99 addition, it doesn't appear in C89).

Johannes Schaub - litb
Interesting. But `float x(int y[const 4])` also compiles (with `gcc -std=c99 -pedantic`. Does it has any special meanings?
KennyTM
@KennyTM, yes it means that the parameter declaration is equivalent to `int * const y`: So it changes the toplevel qualification. The `4` in absence of `static`, however, has no real meaning this time.
Johannes Schaub - litb
A: 

Many embedded devices have a seriously limited stack, such a feature would be of great benefit in reducing the chances of stack overflow, while still giving you the opportunity for re entrant code.

Smaller chips don't have any opportunity to put variables on the stack, so all parameters are implicitly memory locations.

David Sykes