Where are Parameter variables stored in memory?
I am writing some code in C and when coming across a method I wondered where parameter variables were stored in memory. I know the following:
global variables -> stored in code section of static
static varables ->
local auto variables (inside methods) -> stored on the stack
local static variables -> stored on the stack
local const variables -> stored on the stack
Assuming my assumptions are correct.
but where are parameter variables stored? ex: int *(int x, char *c);
Thanks!
EDIT: I know that when I malloc something, it is placed on the heap, but say I dereference the pointer to get the value at the pointer location, is that also stored in the heap or is it now in the stack?
It depends on many different things, the calling convention is the main thing to look into. This x86
page goes into various calling conventions and how parameters are passed to a function and this reference goes over calling conventions used by various C++ compilers and platforms. In general it is either going to be on the stack of through registers.
My answer to C++ (nested) function call instructions - registers is also relevant and has some more useful links.
There are no methods in C, ITYM functions.
Global variables, as well as static ones, are stored in the .data or .bss section (in the following referred to as "data segment").
Local static variables are stored in the data segment as well.
Parameter values to functions are stored on the stack as well, pushed immediately before the return address.
Everything what lives on the stack (local variables, parameters etc.) can live in registers as well. That's why the C standard doesn't state explicitly what to store where.
Function arguments ("parameters") need not be stored at all. Remember: C uses call by value. A value need not be stored in a memory location, it could live in a register, or its value may be concluded from the program state in some way.
For example, library functions like strlen() or sin() , cos(), sqrt() could be implemented in hardware. sin(x) could be implemented by storing some value(s) that correspond to x
into special-function registers, issuing a special-function instruction and pulling the result out. (floating point hardware sometimes even has pseudo-instructions to represent for instance Pi in the best possible precision. The Pi value is never passed, only the instruction: "use Pi/2")
Even for "normal" user-defined functions, the arguments could be passed via registers, multiple arguments could be combined into a large register, maybe even optimised out.
This is best visible with inlined functions, these can totally disappear, because their functionality has been combined with that of the caller.
Things get different when the function takes the address of one of its arguments (for instance int func(int arg) { int *p = &arg; }
will force arg to have an address), but this case is relatively rare.
WRT the standard: a C function has an activation record. (Non-standard people would probably call it a "stack frame"). And the function behaves as if the arguments are part of its local variables, and they go out of scope once the function returns. How they get their values is totally irrelevant, theoretically it could even be done by message passing.
链接地址: http://www.djcxy.com/p/14474.html上一篇: 在C中组织虚拟内存
下一篇: 参数变量在内存中存储在哪里?