memory management for local variables

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
compro
Posts: 9
Joined: Sun Mar 18, 2012 10:29 am

memory management for local variables

Post by compro »

can anyone tell me how memory is allocated to variables declared in function(local variables)
lets say i have a kernel. now i know that dynamic allocation would start at the end of the kernel.
but where in memory are local variables stored??
i tried to figure out experimantally and what i found that local variables were give address<end of kernel
also with each new variable the address given was smaller than the last one.
is memory given to local variables from one of the following sections declared in link file(the text, bss, code section).
and also since memory address given to local variables is downwards(or in decreasing order) . is there any way to increase the space for local variables.

also once the function is returned will the memory address given to new local variables replace the ones that were already allocated the previous local variables or it will just keep going downwards.
User avatar
Solar
Member
Member
Posts: 7635
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: memory management for local variables

Post by Solar »

Local variables are placed on the stack. Stack space is not included in an executable, but set up by the kernel at runtime (setting the stack segment register).

Take note that your question hints at a certain lack of knowledge about details of how computer programs work that is excusable in userspace application programming, but usually a showstopper for most kernel-space work. This is not a field where "training on the job" tactics are encouraged.
Every good solution is obvious once you've found it.
invalid
Member
Member
Posts: 60
Joined: Thu Feb 23, 2012 8:39 am

Re: memory management for local variables

Post by invalid »

It depends on the language/compiler used. C typically reserves space for local variables on the stack. In assembler, AFAIK, you need to manage symbols manually, and if you want to reuse the space, you need to do so explicitly. What are you trying to achieve?
compro
Posts: 9
Joined: Sun Mar 18, 2012 10:29 am

Re: memory management for local variables

Post by compro »

first of all i am no pro but just a student
also what i am trying to achieve is this

i have a kernel(a very basic one indeed)
i have set up the gdt
it is a flat memory model.
the stack segment has the same address as the data segment selector.
now i am not using any application on top of this kernel
i was just trying to see how local variables(declared in the main file of the kernel) are allocated space.
Snake
Posts: 19
Joined: Wed Mar 21, 2012 7:38 pm

Re: memory management for local variables

Post by Snake »

How the local variables are allocated is determined by the compiler. They would be allocated to either the stack or the registers, depending on which optimizations your compiler uses. You might want to look at the C calling convention to see why and how the stack is used to implement functions. http://en.wikibooks.org/wiki/X86_Disass ... onventions
Post Reply