SparkFun Forums 

Where electronics enthusiasts find answers.

General MicroView Support area - You have questions, find your answers here.

Moderators: marcus@geekammo, Help@GeekAmmo

By tdoan
#174675
Hi folks,

I just need to confirm my thought about whether compiler provides some memory management.

In assembly language, we have to manage data on our own (load/read to specific register, memory location). However in higher level language such as C we don't have to up to.

For example:
int x, y, z;

We would have no control where variables x, y and z are stored right? That mean the compiler does some memory management?
Would the compiler be able to know not to overwrite data of the main program when subroutine and interrupt service routine is called?
By Help@GeekAmmo
#174711
Hi tdoan,

From your example:
int x, y, z;

These are allocated during compile time.

avr-libc does provide some runtime dynamic memory management, however for a 2K RAM device, my preference is to manage the memory during compile time. I prefer to create a compile time buffer and reuse it over and over knowing what is in it and when to use it.

Hope this helps.

Cheers
JP
By stevech
#174723
tdoan wrote:Hi folks,

I just need to confirm my thought about whether compiler provides some memory management.

In assembly language, we have to manage data on our own (load/read to specific register, memory location). However in higher level language such as C we don't have to up to.

For example:
int x, y, z;

We would have no control where variables x, y and z are stored right? That mean the compiler does some memory management?
Would the compiler be able to know not to overwrite data of the main program when subroutine and interrupt service routine is called?
Your questions are fundamental programming questions. Need some study. The big picture, for C/C++ programs:

variables declared outside of any function are "static". They are gloabally available to any function >in the same source code file<. They can be made available to functions in other files using certain C/C++ declarations.

Variables declared inside a function exist until that function returns. So this:
Code: Select all
void foo(void)  {
  int x,y,z;
  }
are "local" variables. Local to the function here named foo.
But in this one:
Code: Select all
void foo(void)  {
  static int x,y,z;
  }
The variables x,y,z are accessed only by foo() in which they are declared. But being static, these don't lose their value when foo() exits. This static in a function is rarely used. It's dangerous if foo() is to be reentrant.

the code
Code: Select all
// file alpha.c
int x,y,z;
Has x,y,z declared outside of any function, so they are implicitly static. That is, if you put "static" in front of "int", above, it makes no difference. These variables are usable by any function in alpha.c.

How to share static file level variables among 2+ files is simple but too much to start into here.

Theses are simple C examples. C++ gets more complex. So get a good C (not C++) book, use a C compiler on a PC to write PC programs to master C. Then switch to microprocessors. After that, dig into C++ which has many different concepts.

Dynamic memory allocation such as the "new" operator in C++, or calling malloc(), should NOT be done on small RAM micros. You'll soon realize why.
By tdoan
#174814
Thanks folks.

I understand variable scopes but when I mention where the variables stored, I meant as in x is stored in register A, y is stored in register B etc. I am guessing we have no control over which registers the variable is stored in when using high level programming language.