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.

[SOLVED] Global Vs Local Variable(Keil C51 Compiler)

Status
Not open for further replies.

praveenrpk92

Newbie level 6
Joined
Dec 28, 2015
Messages
11
Helped
1
Reputation
2
Reaction score
1
Trophy points
3
Activity points
98
I have all global variables in my project. Since RAM data size has almost become full, i wanted to replace some of the global variables with local variables in functions. But each local variable declaration seems to take up RAM space as if it is a global variable;

Assume I have few global variables and the data size is 10 bytes

now i add a new function
Code:
void function1()
{
char var1,var2,var3,var4,var4,var5,var6,var7,var8,var9,var10;
}

After compiling, lthis increases the data size to 20.

adding another function

Code:
void function2()
{
char var1,var2,var3,var4,var4,var5,var6,var7,var8,var9,var10;
}
After compiling, this again increases the data size to 30. What is the point of using local variables in this scenario? It works as if the the variables are declared globally. Shouldn't the data size be global variables 10 bytes + maximum local variable size i.e 10 bytes = 20 bytes total?
Is it better to declare the 10 variables as global once and save the 10 bytes?
 

Hi,
If you define local variables in side function, that memory released at the time of exit.

It is the basic, is it any change in case of keil compiler.
 

The compiler don't know at first sight whether you will instantiate these functions at the same layer of the program or not. If you call all them sequentially, really could reuse the same memory space for all that functions, but if you call, for instance function1() inside an ISR vector ( which in fact is not an appropriate deed ) and if you call function2() inside the main(), both memories resourses will not be released, being used at same time.
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top