Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Local variable problems:

Status
Not open for further replies.

bhattaroshan

Member level 1
Joined
Sep 28, 2010
Messages
33
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,288
Activity points
1,531
Hi everyone,

Does the variables declared in some function totally destroyed when we come out of the function. Or, is it that the memory address allocated by the variable can be replaced by other programs/functions at any time?
 

Does the variables declared in some function totally destroyed when we come out of the function.?
Yes. Unless the variable is declared as static, its value is lost. Its memory address can further be used by another function, and you don't have any guarantee that the next time the function is called the same memory location is assignet to that variable.
If you want that the variable preserve its value between calls, declare it as static inside the function.
Regards

Z
 

I program in BASIC. The docs say that a variable allocated within a function exists only within the function. When the function ends the variable is dismissed. The memory is supposedly released, and can be used to store other variables.

I don't know whether a block of memory ends up resembling swiss cheese after this is done a number of times. Functions could conceivably be called in a manner that forces the program to allocate ever more blocks of memory to store variables.
 

OK.. i came to know that the data written in RAM is not necessary to be erased after coming out from the function. But, it can be erased whenever necessary. But, again i have one doubt how can it be found out that particular memory in RAM is not in use, and so can be replaced by some other variables whenever necessary?
 

The most popuar method to assign memory space for local variables is to place them on the stack. This will assure their consistency during "lifetime" and automatical memory space release afterwards. The method is also dealing well with reentrant functions.

Compilers for microcontrollers with restricted stack space (e.g. PIC16/18, 8051) need to manage local variable space explicitely and keeping track of function call levels. Reentrancy is generally causing problems for these compilers.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top