Of Stacks and Heaps

OK, so I may not be a hard-core programmer like some in th room (you know who you are), but there are times when I really enjoy understanding just that little bit more about the (computer) world we live in today. So it was a very pleasant time spent on Kirupa this evening, learning and understanding (I think) about how memory is allocated based on my hacked together code. All very interesting, and perhaps someone can tell me if it’s all correct?

Stacks and Heaps

  • http://rant.blackapache.net/ OJ

    I have no idea who you’re referring to, but whoever he/she is I hope they post! As I’d be interested in what they’ve got to say :)

    The article isn’t bad. I think some of the bits are a little misleading, and some other bits arent explained well enough.

    Since this article is very .NET specific isn’t not really safe to assume that the rules stated in the article are correct for other languages (in fact, they’re not :) ).

    So, a few points:

    Code Snippets
    The code snippets that he’s posted are technically uncompilable, because the bodies of the functions need to be included in the class scope:

    // this is wrong
    class Simpsons
    {
      public string name;
      public int age;
    }

    public void People()
    {
      Simpsons bart = new Simpsons();
      bart.name = "Bart";
      bart.age = 10;
    }

    // this is right
    class Simpsons
    {
      public string name;
      public int age;

      public void People()
      {
        Simpsons bart = new Simpsons();
        bart.name = "Bart";
        bart.age = 10;
      }
    }

    Value vs Reference
    The author stated that reference (class) types are always on the heap – which in .NET they are. But what they fail to state is why they’re on the heap. The reason for this is simple. In C# (and VB.NET), all class types have to be instantiated using the new keyword. This keyword is what forces allocation of the object on the heap, not the stack. If you call ‘new’, you get heap, if you don’t, you get stack. So if you look at the above code snippet, the instance of ‘bart’ is stored on the heap, but the reference (ie. the variable) ‘bart’ is stored on the stack. So ‘bart’, which is on the stack, references an instance of ‘Simpsons’ which is on the heap.

    In other languages, such as C++, it can be different. Observe:

    class Simpsons
    {
    public:
      string name;
      int age;
    };

    int main()
    {
      Simpsons bart; // totally allocated on the stack
      Simpsons* bort = new Simpsons(); // reference on stack, instance on heap
      return 0;
    }

    So it’s possible, and very common, that class instances are stored on the stack as well – they’re not only tied to the heap. The key, again, is the new keyword.

    So in a nutshell, just be mindful that it will vary from language to language.

    Garbage Collection
    This is available in a stack of languages, but not available in others. C and C++ are the common ones which don’t have their own garbage collector (managed C++ does, but only if you use managed objects and gcnew instead of new). If you use new or malloc() you have to make sure you clean up your objects manually via delete and free.

    Other than that, it’s a pretty good primer for those new to memory allocation and stacks/heaps.

    Lots of love!
    OJ :)

  • http://blog.kirupa.com Kirupa

    Hi OJ – I definitely agree with your assessment of the article, but the code provided does work: http://www.kirupa.com/temp/Program.txt

    Since running the code wasn’t a focus of the article, I skipped the parent class/namespace/import data that would normally be in it. So, yes, copying and pasting the code into Visual Studio wouldn’t work, but the reason is because the parent class was simply omitted.

    Concerning the breadth of details, I wrote this article with the goal of providing a basic overview of how some of the things behind the scenes work.

    As such, I deliberately had to leave out a lot of the details such as many “why is it done that way” snippets because I felt that would be too much for readers who are learning about this for the first time to absorb.

    If you want to write a follow-up article that contains more details, I’d be very happy to post your article on the site. More info here: http://www.kirupa.com/developer/tutorials.htm

    Cheers!
    Kirupa :)

  • http://rant.blackapache.net/ OJ

    Hi Kirupa!

    Great comment, it backs up your article nicely. I’d love to write something to build on the foundation that your provided in your article, and I appreciate the offer to have it posted on your site.

    I’ll aim to get an structure together, along with some ideas for content that will work well with what you’ve already presented, in the next week or so. I’ll be sure to keep you posted.

    Love ya work! I’ll be in touch.

    Cheers :)
    OJ

  • dan

    Hey guys,

    OJ – look forward to seeing what you produce

    Kirupa – Thanks for stopping by, I don’t know how you find the time to do everything you do, but it’s great to see you getting around and reading comments and replying. And thanks for a great resource in Kirupa.com. In the middle of pouring over Senoculars AS3.0 tips. Genious!