c - Where is local data defined inside a function stored at startup -


i understand global data stored in .bss or .data segment.

if have data such structure defined inside function however, structure placed on stack when function called.

where compiler store data @ startup tho before function called? know compilers generate different assembly code depending on target , calling conventions etc. , generally, wouldn't local structure have stored somewhere first compiler knows how put on stack when function called? in advance.

in case of global variables (also static local):
compiler accesses global variables memory parts of address , size. address placeholder (much name of variable). size implicit, depending on kind of access compiler has chosen in assembly/machine-code. linker/loader aware of size, makes sure next variable not overlapping.
address placeholder gets replaced during linking (e.g embedded, static, execute-in-place) or loading (e.g. programs on pc, apps).

in case of local variables:
"address" value, gets written placeholder, relative, offset on "top" of stack position @ time of starting function.
otherwise determined values global placeholders. position on stack influenced variable accessed, optionally sub-offset struct members. actual address determined stack position (i.e. value of stack pointer) plus relative offset found in placeholder.

the placeholders lists of offsets, stored in binary (e.g. program.exe) along actual binary code. "when have decided variable found in memory, write address in places within code listed here: ...". writing these places example put meaningful value in byte during execution interpreted literal offset relative "mov" assembler instruction; relative e.g. section pointer start of bss section.

so in short, address information in lists of placeholders. these lists migth not found in memory @ all, or not @ execution time. once program loaded (including filling in placeholders) these lists not needed anymore.
size information part of code (e.g. using 8bit access or 32bit access) , part of layout of variable locations. each gets accessed far enough behind previous variable.

so, shorter, information looking half hidden , half not in memory @ during execution.

i assume question not value inside variables. cover it: value gets initialised either 0, constant initialisation value or, special cases, not @ all, keeping whatever in memory @ startup. init values found in sections lists of constants. constant given variable found similar placeholders.
in case of local variables, compiler not initialise, can configured write 0 (which kind of "expensive"). why practice init variables before using them. local variables explicit initialisation of course initialised during stack frame setup.


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -