Memory Management - A primer

A lot of people that start programming and do not come from a C/C++ background, i.e. learning the fundamentals of programming (generally taught at Uni level studies) for the reason that they are not programmers or have not as yet been to Uni or do not intend on doing so. So, are they disadvantaged? No, here's a quick start to get you upto speed, or even refresh if you have learned this before.

NOTE : This is specific to C++, the same can be then applied to other languages/compilers.

There are 4 categories of memory, when dealing with C++
1. Code - The area of memory that has been allocated and contain all the instructions for all the functions and members functions, As the program is executed, these are read and executed.
NOTE: This might not be true in the case of interpreted languages and script languages, this is only true for Compiled languages/code.

2. Static Data - This is the area that contains all the global variables as well as any local class data members that are declared as static.

3. Run-time stack - This is the area generally used by most running apps, in most languages. All local, nonstatic variables reside on the runtime stack.

4. Free Store, or Heap - This is the area of memory that is explicitly requested for the new operator, not applicable for non C++ type languages.

Now, in context of Lua, the most important areas to look at are 2 and 3. In the same context, if all variables are assigned as non-local then they get placed on the static heap, which means they are available for the lifetime of the app ,from the start to the end. Since on mobile devices memory is limited, it is advised to declare most variables are local.

The local variables are stored on the Run-time stack which are tied to the function entry and exit. Functions are invoked in a hierarchical fashion, which means if f invokes g, and g invokes h, then h must terminate before g continues execution and g before f will resume. The advantage of this is that all the variables assigned are for the scope of that function/code block. A variable defined in f will be available to g and h, any variables defined in g will be available to h. When the functions terminate, the variables space is cleared up releasing the memory used by those variables.

In C++ this functionality has it's drawbacks and comes at a price.

Hope this has given you a little idea on what memory allocation is like in C++. In c++ each memory location allocated has a pointer that points to this memory allocated. A variable like int a, has a value and a memory location, so printf(a); will print he value of a, where as printf(&a); will print the memory address where the variable has it's storage. Luckily Lua does not have any such complications.

What is a Leak?
Several things, but in the context of programming, any memory that is not deallocated at the end of the function/application is called a memory leak.

if you have any questions, please feel free to ask in the comments below, till next time,

cheers,

?:)

Comments

  1. Nice article. Glad to learn something more this morning.

    Naomi

    ReplyDelete

Post a Comment

Popular Posts