When you declare a new variable the compiler has to generate code that allocates sufficient memory to store the data it is to hold. The whole subject of memory allocation is a complicated and interesting one but every programmer should know about the two very general approaches - the stack and the heap.

Stack based memory is a natural match for the way that variables are allocated and created by a program constructed as a set of nested method calls - which most are.

What happens is that when a method is called all of its local variables are created on the top of the stack - creating its so called stack frame. While the method executes it has access to only its local variables i.e. its entire environment is limited to the data on the stack frame. Exceptions to this local only rule are global variables and objects but these are allocated on the heap.

If the method calls another method then the new method creates its stack frame on the top of the stack. In this way each new method can allocation its own local variables in its own portion of the memory allocated to the stack.

The beauty of stack allocation is that to implement garbage disposal all that has to happen is that when each method terminates its simply clears the stack of its stack frame i.e. it adjusts the stack pointer to point to the end of its stack frame so returning the memory to use. Of course when a method ends control returns to the method that called it and it finds the stack in just the state it needs to access its own local variables.

In this way each method gets access to its local variables while it is running without any need to keep track of where they are. The stack is also use to store parameters and return values passed between methods in a fairly obvious way. If you would like to know more about the theory of how a stack works then see "Stacks" in Babbage's Bag.

The stack in action

The stack works well for storing all manner of data that is created and destroyed following the pattern of method calls - but for global data and for very large or complex data we need something else.

The alternative is the "heap". This is a very descriptive name for an area of memory that is set aside simply for the purpose of storing global data. When a global object is created, by the use of new in c#, an area of the heap is allocated big enough to store it at its initial size and a reference to its location is returned - often to be stored in a local variable on the stack.

However unlike local variables which are created and destroyed along with the method they belong to there is no clear signal that a global object is finished with. It doesn't necessary become redundant when the local variable that it was initially created with goes out of scope because there could be many variables referencing it. So to deal with this problem a system wide garbage collector is provided which keeps track of what is stored on the heap and how many references there are to each. When an object on the heap has no references to it then it is deallocated and the memory recovered.

This the general idea of heap management but in practice there are many subtle problems. For example as the heap is used and released is slowly becomes fragmented into small blocks of used heap separating blocks of free space. The solution is to make the garbage collector consolidate memory every now and again. Notice also that it is generally better to adopt a throw away approach to heap management. For example, if an object, a string say, needs to increase in size then rather than try to open up some space to make the current allocation bigger it is generally better to mark the current allocation as garbage ready for collection and allocation a whole new block of memory even though this involves copying the existing object.

The first question that any VBA beginner needs answering is - how do I access the data stored in the spreadsheet. The answer is easy, but it introduces ideas of objects and their properties and methods. This is where the real programming starts.

]]>mike.james@infomaxgroup.co.uk (Janet Swift)Automating ExcelWed, 15 Dec 2010 00:00:00 +0000Scrollbars, Trackbars and fontshttp://www.i-programmer.info/ebooks/master-visual-basic/2475-scrollbars-trackbars-and-fonts.html
http://www.i-programmer.info/ebooks/master-visual-basic/2475-scrollbars-trackbars-and-fonts.htmlScrollbars and Trackbars are two of the most basic controls you can add to a form. In this installment of Mastering Visual Basic 2010 we take a look at how to use and how to avoid using them.
]]>mike.james@infomaxgroup.co.uk (Mike James)Master Visual Basic 2010Tue, 24 May 2011 16:00:09 +0000Kinect SDK1 - A 3D Point Cloudhttp://www.i-programmer.info/ebooks/practical-windows-kinect-in-c/4126-kinect-sdk1-a-3d-point-cloud.html
http://www.i-programmer.info/ebooks/practical-windows-kinect-in-c/4126-kinect-sdk1-a-3d-point-cloud.htmlA point cloud is a set of points in a 3D space. You can view the cloud from different angles and lighting conditions. You can even apply colors and textures to its surfaces. A point cloud is one step away from a full 3D model and sometimes it is more useful.
]]>mike.james@infomaxgroup.co.uk (Mike James)Practical Windows Kinect In C#Fri, 27 Apr 2012 00:00:00 +0000WinRT JavaScript - Web Workers & Promiseshttp://www.i-programmer.info/ebooks/creating-javascripthtml5-metro-applications/4713-winrt-javascript-web-workers-a-promises.html
http://www.i-programmer.info/ebooks/creating-javascripthtml5-metro-applications/4713-winrt-javascript-web-workers-a-promises.htmlIf you want to keep the user interface fast and responsive you have to follow the framework and create asynchronous functions - but how? This sounds complicated. The solution is to make use of Web Workers and wrap what you create so that it returns a promise. Let's see how it all fits together.
]]>mike.james@infomaxgroup.co.uk (Ian Elliot)Creating JavaScript/HTML5 Windows 8 ApplicationsWed, 29 Aug 2012 00:00:00 +0000Java Data Types - Numeric Datahttp://www.i-programmer.info/ebooks/modern-java/5423-java-data-types-numeric-data.html
http://www.i-programmer.info/ebooks/modern-java/5423-java-data-types-numeric-data.htmlAfter looking at some of the advanced ideas of classes and objects we need to return to some simpler topics to make our understanding complete. We need to look more closely at data and, to get things moving, numeric data.
]]>mike.james@infomaxgroup.co.uk (Alex Armstrong)Modern JavaWed, 06 Feb 2013 00:00:00 +0000Value and Referencehttp://www.i-programmer.info/ebooks/deep-c/362-deep-c-c1.html
http://www.i-programmer.info/ebooks/deep-c/362-deep-c-c1.html

Chapter One

Value and reference are a fundamental division in the way C# treats data. It is important that you understand the differences and most importantly when to use a struct and when to use a class. These aren't just differences in efficiency, they affect the semantics too.

When working with an Excel spreadsheet becomes repetitive you need a macro to automate the task - and for this you need VBA.

]]>mike.james@infomaxgroup.co.uk (Janet Swift)Automating ExcelFri, 10 Sep 2010 00:00:00 +0000Graphics and Animation in Visual Basichttp://www.i-programmer.info/ebooks/master-visual-basic/2246-graphics-and-animation-.html
http://www.i-programmer.info/ebooks/master-visual-basic/2246-graphics-and-animation-.htmlVisual Basic makes it very easy to load and display images that have been prepared earlier and stored on disk or on a web server. Not only does VB make this easy, with the addition of the Timer control making things move is very easy indeed.
]]>mike.james@infomaxgroup.co.uk (Mike James)Master Visual Basic 2010Thu, 07 Apr 2011 09:28:55 +0000