You are here:

C++/Object Instantiation vs. Dynamic Memory Allocation


Say you have class A, you can instantiate an object of class A like:

A a;

And then you can do stuff with that class (eg, a.method(), etc.)

But you can also do:

A a = new A();

And do stuff with that, and delete it with:

delete a;

My question: What's the difference between the two?

Also sometimes i get a segmentation fault when i am not using new operator..and sometimes i dont ..what is the significance behind this?


Hi, Smita.

The difference between these two types of allocation are where the memory comes and how longs it sticks around.

First, let's discuss the two places that memory can come from.  You can allocate memory from the heap or from the stack.  The stack is where all your regular variables come from.  When you simply declare a variable -- be it an integer, a floating point, a boolean, a class, a struct, whatever -- it is allocated on the stack:

   int stackInt;
   bool stackBool;
   float stackFloat;

You can, however, dynamically allocate memory, which pulls the memory from the heap:

   int* heapInt = new int;
   bool* heapBool = new bool;
   float* heapFloat = new float;

The stack is a small, fixed-size block of memory dedicated to your program.  All of the local variables, function parameters, etc. exist in the stack memory.  The heap is the larger pool of available memory that is shared between all running applications.  Basically, the heap is the entirety of free memory during program execution.

Another important distinction between dynamic and non-dynamic variables is that dynamic memory must be deleted.  The memory will be allocated to your running program until you call delete on the address.  Local variables, on the other hand, are deallocated as soon as they fall out of scope:

   if (someBoolean)
       int localInt = 3;
       int* dynamicInt = new int;
       *dynamicInt = localInt;

At the end of the above code block, the localInt variable is automatically deallocated because it is a stack-based variable.  The dynamicInt variable, though, is not deallocated, meaning memory is leaked (because we are not maintaining a copy of the pointer, so we cannot delete the allocated memory).

So, that covers the basic differences between heap-based and stack-based variable allocation.  The most puzzling part of your question is about the seg fault.  However, you may have already started to figure out what causes the seg fault you're seeing.

When you create an instance of a class on the stack, you are requesting a block of memory from the stack that has to hold the entire class.  If the class is a large class, then you end up taking a large chunk from your stack.  Because the stack is generally small, and its size cannot change at runtime, you can end up running out of stack memory.  When this happens, many operating systems will throw a seg fault error.  The solution is to, instead, allocate any large chunks of memory on the heap.  If you need a large string, it's a good idea to allocate it on the heap.  If you need a large class, it's a good idea to allocate it on the heap.

OK, hopefully that clears up all of your questions.  If you have any further questions, please do not hesitate to ask.  I'm here to help. :)


All Answers

Answers by Expert:

Ask Experts


Joseph Moore


I've been programming in one form or another since my brother taught me BASIC when I was 6. I've been programing professionally since I was 20, first web development with HTML, JS, DHTML, CSS, etc., then I became a video game developer, writing code in C, C++, C#, SQL, assembly, and various scripting languages. I've even written my own scripting languages, custom designed for the games I was making. I also dabble in Java, PHP, and Perl. I've worked on pretty much every aspect of game development, including graphics, audio, gameplay, tool, UI, input, animation, and physics.


I've been writing C++ code for 12 years, both on my own in my spare time and professionally.


Bachelor of Science in Game Design and Development, Full Sail University, Winter Park, FL

Awards and Honors
Salutatorian and Advanced Achiever Awards at Full Sail; Independent Games Festival Student Showcase winner, 2004; Featured article on Gamasutra about an experimental game developed in 2004

©2017 All rights reserved.