Archive

Posts Tagged ‘stack’

A major revision for my publication about Object-Oriented Memory Management

April 10th, 2009 bodom_lx No comments

It took about a year for a major revision of my document about memory management in object oriented programming languages.

This major revision adds C++ in addition to Java.

The paper is about a model for memory management during the execution of programs written in Java and C++.

The number of pages grew from 17 to 28.

You can see more information and download the pdf on this page.

I hope you will find it useful to model your programs and know how memory is handled.

Related posts

Heap vs. Stack in C++

March 20th, 2009 bodom_lx 4 comments

After the study of pointers versus references, the second natural question that comes in head of a ex Java developer turning to C++ is:

“What are the difference between static and dynamic memory allocation in C++?”

which can be translated as:

“When should I use the stack and when do I have to use the heap in C++?”

that can be further simplified to:

“When should I use the new operator in C++?”

I could simply summarize the answer to: “Use stack when possible”, but I think that this time there is the need of more explanations. Let’s have the following model for a process in the system:

A simplified model for a process

A simplified model for a process

I’m not really interested in an real representation of a process (see Modern Operating Systems by A. S. Tanenbaum for a very good explanation on processes), but focus on the stack and on the heap.
In reality the heap is a software abstraction but you can also imagine it like the stack.
In C++ programs there are also several other memory areas in which objects and non-object values may be stored (see this article on GotW for further details).

Why then choose between stack or heap? Quoting my publication on object-oriented memory management in Java:

Stack-based variables have their extent determined by their scope, so the former is constrained by the structure of the code at compile-time .

Sometimes there is a need for the variables with unconstrained extent in order to cope with
problems where lifetime of a variable can only be known at run-time.
In this case heap-based variables, whose extent is strictly under control of the programmer, are used. [..]

I promise that I will update my 17 pages about OO-memory management to cover also C++ by the end of June. By the way, following some forums, wikipedia, my publication and GotW, I also summarized pros and cons of stack and heap use in C++:

Stack Heap
Its size is determined at compile-time Size determined at run-time
Therefore, it is less expensive and quick Therefore, it is more expensive and slower than stack
The preferred way to store objects and variables if their size is limited. To be used only if needed: the amount of memory needed is variable and unknown, and may increase rapidly.
There is an AUTOMATIC CLEANUP of objects when they go out of their scope Objects STAY IN MEMORY even when you don’t use them anymore.
Programmers don’t have to bother to free resources Therefore, programmers HAVE TO CLEAN memory manually. However, all modern OS free the resources when the program exits.

Update 2009-07-03
After 4 months of heavy GUI and Database C++ programming, here are my thoughts: if you are planning to write a program with something more than a couple of objects interacting, using associations and therefore, objects as attributes, use the heap. Every serious program, even if not really big, uses heap for object allocation. Just take care to delete the objects when you don’t need them anymore. Objects on the heap are dynamically allocated and it is more comfortable to pass them through other objects using pointers. The use of the heap assures the live of objects even if the method that generated them runs out of scope ( =dies ). If you also plan to write GUIs, solid toolkits like QT recommend the use of heap to create graphical objects.

In some Operating Systems, stack is also very limited while heap is usually not.

Here are listed the sources I used for writing this article:

  • http://www.velocityreviews.com/forums/t278261-stack-vs-heap.html
  • http://www.computing.net/answers/programming/stackheap-c/2293.html
  • http://www.codeguru.com/forum/showthread.php?p=1186307#post1186307
  • http://www.codeguru.com/forum/showthread.php?t=350945
  • http://www.gotw.ca/gotw/009.htm
  • http://en.wikipedia.org/wiki/Process_(computing)#Representation
  • http://task3.cc/object-oriented-memory-management/

Hope that this article helped you to clearly understand the differences between stack and heap allocation in C++, write me if there are other issues or you need more explanations!

Related posts

OO Memory Management Model summary updated

April 6th, 2008 bodom_lx No comments

Today I wrote a new version of my summary posted here. The new version covers more topics, here is the updated list:

  • Memory portions assigned to a program (code area, heap / dynamic memory area), execution stack
  • How code is loaded in Java
  • The Activation Record (AR) and function calls
  • Abbrevations (AR, RV, RA, SP, N/E, @, ??, arb)
  • Examples on method calls and activation records usage
  • Declaraion vs. Definition of a variable, the scope of a variable, blocks
  • Scope Activation Record (SAR), Static Link (SL), the role of SL
  • The extent/lifetime of a variable
  • Dynamic memory allocation and handling
  • Dynamic vs. Static memory allocation
  • Dynamic memory scope and extent
  • Accessing dynamic memory
  • Classes and Objects in detail, object instantiation
  • Memory Management issues
  • Objects vs. Variables (definitions)
  • Methods of Objects
  • Class Attributes
  • The null value
  • Parameters (formal, actual), parameters passing (by reference, by value)
  • Constructors and Inline Initialization
  • Constructor’s call

Related posts

Object Oriented Memory Management

March 19th, 2008 bodom_lx No comments

Major Update on 10th April 2009, inclusion of C++ programming language!
Updated on 18th April 2008, a complete example on stack and heap
Updated on 15th April 2008, new contents and new layout!
Updated on 6th April 2008, new contents!

The paper you can download from here is about a model for memory management during the execution of programs written in Java and C++.

It started on March, 2008 as a summary of the lecture notes of both the “Programming Project” and
“Software Engineering Project” courses held by professors of the CASE (Center for Applied Software Engineering) of the Free University of Bolzano – Bozen.

The first versions of this publication were only about Java memory management.
Subsequent revisions added information found on other sources. Unfortunately, the author forgot to
reference the sources on the document.

On March, 2009 the author began to add the information about C++ programming language. More
information from other sources were added, including their attribution.

The biggest source of this document is still the set of presentations of CASE.
The code snippets and their corresponding stack/heap diagrams are copied in full from those of the
slides.

The next major revision will contain original images (not belonging to CASE slides), as well as other code
snippets that I could find more clear than those of CASE.

If you find that this document contains information taken from one of your publications, please
contact the author, that is willing to either delete them from this document or to add an attribution to
your work.

Download the PDF of the summary

Table of Contents:

  • The model
  • Code load and execution
  • Activation Record (AR)
  • Contents of the Activation Record
  • Abbreviations for
  • Declaration vs. Definition
  • The scope of a variable
  • Extent of a Variable
  • Blocks
  • Scope Activation Record (SAR)
  • Example on SAR
  • Role of SLs
  • Dynamic Memory Allocation And Handling
  • Dynamic Vs. Static memory allocation
  • Dynamic Memory Scope and Extent
  • Accessing dynamic memory
  • Classes
  • Objects
  • Object instantiation
  • Objects in Memory (Java)
  • Objects in Memory (C++)
  • Memory Management issues (Java)
  • Memory Management issues (C++)
  • Methods
  • Methods (Java)
  • Methods (C++)
  • Attributes
  • The null value (Java)
  • The NULL value (C++)
  • Parameter
  • Parameter Passing (Java)
  • Example of parameters passing (Java)
  • Example of parameters passing (Java), continued
  • Parameter Passing (C++)
  • Example of parameters passing by value (C++)
  • Example of parameters passing by reference (C++)
  • Pointers vs. Parameters (C++)
  • Previous example using pointers (C++)
  • Constructor
  • Inline initialization
  • A constructor’s call (Java)
  • Class attributes
  • Example of class attributes (Jav)
  • Example of class attributes (C++)
  • Class Method
  • Example of Stack/Heap Diagrams in Java
  • Code
  • Stack Diagram
  • Heap Diagram
  • Memory portions assigned to a program (code area, heap / dynamic memory area), execution stack
  • How code is loaded in Java
  • The Activation Record (AR) and function calls
  • Abbrevations (AR, RV, RA, SP, N/E, @, ??, arb)
  • Examples on method calls and activation records usage
  • Declaraion vs. Definition of a variable, the scope of a variable, blocks
  • Scope Activation Record (SAR), Static Link (SL), the role of SL
  • The extent/lifetime of a variable
  • Dynamic memory allocation and handling
  • Dynamic vs. Static memory allocation
  • Dynamic memory scope and extent
  • Accessing dynamic memory
  • Classes and Objects in detail, object instantiation
  • Memory Management issues
  • Objects vs. Variables (definitions)
  • Methods of Objects
  • Class Attributes
  • The null value
  • Parameters (formal, actual), parameters passing (by reference, by value)
  • Constructors and Inline Initialization
  • Constructor’s call
  • Class attributes (static variables)
  • Class methods (static methods)
  • Complete Example of Stack/Heap Diagrams

Everything is integrated with simple examples.

Download the PDF of the summary

Related posts

Publications

November 5th, 2006 bodom_lx No comments

This page contains all my pubblications around in the Net. These pubblications are mainly how-tos, tutorials and even little thoughts that I find useful. If you find a description in Italian, you should speak Italian for understanding it.

In questa pagina elencheró tutte le mie piccole pubblicazioni in giro per la Rete. Queste pubblicazioni comprendono guide, how-to, anche piccoli, oppure pensieri ai quali dó importanza.

Introduction to Software Testing
http://task3.cc/introduction-to-software-testing/
Link to the Pubblication (PDF)
A brief introduction to Software Testing concepts and Techniques, with explanations about control-flow graphs and data-flow analysis.

Object Oriented Memory Management
http://task3.cc/object-oriented-memory-management/
Page with topics covered
Link to the Pubblication (PDF)
Modeling OO memory management (stack and heap) touching both Java and C++

Introduzione alla Logica Sfumata
Fuzzy Logic: tesina (PDF)
Fuzzy Logic: Slides Integrativi (PDF)

Installare OpenGEU su un Apple Macbook
http://opengeuwiki-it.intilinux.com/index.php?title=Macbook
Una guida su come installare Ubuntu, OpenGEU e qualsiasi altra distribuzione derivata da Ubuntu, sul Macbook

How to install OpenGEU on a Apple Macbook
http://opengeuwiki-en.intilinux.com/index.php?title=Macbook
A guide on how to succesfully install Ubuntu, OpenGEU and any other Ubuntu derivate distribution on a Macbook

How to manage a card reader and the FSFE crypto card
http://task3.cc/how-to-manage-a-card-reader-and-the-fsfe-crypto-card/
A nice tutorial on how to set up a USB card reader for using it with a GPG card (for example, the FSFE fellow card)

Some definitions for Introduction to Programming
PDF
A very little PDF document with some useful definitions for a past university course, Introduction to Programming.

Hp nx6325 (and friends) thermal problems solved
http://task3.cc/hp-nx6325-and-friends-thermal-problems-solved/
An article on how to solve the (in)famous thermal problem of all Hp notebooks with an Amd Turion 64 X2 / Sempron cpu. It contains a bash script that really works and turns on the CPU fan when the temperature rises.


Installare Gnome Slacky su Slackware Current
http://www.slacky.eu/wikislack/index.php?title=Gnome_Slacky_su_Slackware_Current
Un How-To su come installare e configurare Gnome Slacky su Slackware Current

Abilitare l’accelerazione 3D con schede ATI e distribuzione Ubuntu Feisty Fawn
http://wiki.ubuntu-it.org/Abilitare3DAti
Un how-to sul wiki di ubuntu-it che spiega come installare i driver proprietari Ati per linux, utilizzando la distribuzione Ubuntu Feisty Fawn

Abilitare l’accelerazione 3D con schede ATI e distribuzione Ubuntu Edgy Eft
http://wiki.ubuntu-it.org/Abilitare3DAti/Edgy
Un how-to sul wiki di ubuntu-it che spiega come installare i driver proprietari Ati per linux, utilizzando la distribuzione Ubuntu Edgy Eft

Abilitare l’accelerazione 3D con schede ATI e distribuzione Ubuntu Dapper Drake
http://wiki.ubuntu-it.org/Abilitare3DAti/Dapper

un how-to sul wiki di ubuntu-it che spiega come installare i driver proprietari Ati per linux, utilizzando la distribuzione Ubuntu Dapper Drake

Abilitare l’accelerazione 3D con schede ATI e distribuzione Ubuntu Breezy Badger
http://wiki.ubuntu-it.org/Abilitare3DAti/Breezy
un how-to sul wiki di ubuntu-it che spiega come installare i driver proprietari Ati per linux, utilizzando la distribuzione Ubuntu Breezy Badger

Abilitare l’accelerazione 3D con schede ATI e distribuzione Ubuntu Hoary Hedgehog
http://wiki.ubuntu-it.org/Abilitare3DAti/Hoary
un how-to sul wiki di ubuntu-it che spiega come installare i driver proprietari Ati per linux, utilizzando la distribuzione Ubuntu Hoary Hedgehog

Abilitare l’accelerazione 3D con schede ATI e distribuzione Ubuntu Warty Warthog
http://wiki.ubuntu-it.org/Abilitare3DAti/Warty
un how-to sul wiki di ubuntu-it che spiega come installare i driver proprietari Ati per linux, utilizzando la distribuzione Ubuntu Warty Warthog

Abilitare l’accelerazione 3D con schede ATI e distribuzione Ubuntu
http://wiki.ubuntu-it.org/Enable3DAtiRadeon(fglrx)
-ora deprecata – é la pubblicazione della quale vado piú fiero: un how-to sul wiki di ubuntu-it che spiega come installare i driver proprietari Ati per linux, utilizzando la distribuzione Ubuntu. Questo testo é una delle piccole prove che lo spirito del software libero e della condivisione delle informazioni funzioni davvero, perché creato dal sottoscritto (rimango sempre il mantainer ufficiale, ed é mia la maggior parte del testo), ma modificato e migliorato dagli utenti. Soprattutto, funziona :-)
Ora questa guida ha avuto uno split-up, andate su http://wiki.ubuntu-it.org/AbilitareIl3D per dettagli

Installare e configurare Xgl con schede video Ati (anche serie Xxxx) e Ubuntu Dapper Drake
http://task3.cc/2006/08/04/installare-xgl-con-schede-ati – é il risultato della raccolta di una mole immensa di informazioni nella Rete, della sperimentazione di Xgl sul mio portatile, e le istruzioni per come configurarlo correttamente. Questa guida non funziona per Ubuntu Edgy Eft, perché il sistema é cambiato, non appena configureró Xgl sul portartile nuovo e su Edgy Eft, prometto di scrivere come fare

Risolvere i problemi con il wireless Intel dopo l’update a Dapper Drake
http://task3.cc/2006/06/03/ubuntu-dapper-drake-e-wireless-intel/
– Piccola guida su come far funzionare nuovamente la propria scheda di rete wireless Intel dopo l’aggiornamento a Dapper Drake. A scanso di equivoci, questa piccola guida serve solo se la propria scheda di rete risultava giá operativa prima dell’update


If you find my publications interesting, if something I wrote on the blog did help you to solve a problem, if you realize you could have paid for solving it..
Why don’t you consider a donation? Donations are useful to maintain my domains and the infrastructures that host my Projects. I’m just a student, I’m not interested to earn profits from my projects, that will always remain free. But I would be delighted to don’t pay for them :-)

Related posts