You are here:

C++/Object Instantiation vs. Dynamic Memory Allocation


QUESTION: 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?


ANSWER: Hello Smita, thank you for the question.

When you create a variable such as A a, this allocates memory from the stack. What this means is that the compiler is in charge of allocating and deleting the memory for that variable and its data members. When the variable falls out of scope, its destructor is called, and all of its data members are deleted by the compiler.

When you create a variable using new, this tells the compiler that the programmer will be responsible for deleting the variable's memory. New returns a memory address, which is then assigned to a pointer, as pointers are the type that hold a memory address. When you create with new, you must delete also, or else the memory address is leaked. This memory is allocated from the heap.

As for the crash, it's hard to pinpoint the cause of that without knowing exactly what's going on. It could be caused by an infinite number of things. Perhaps you are allocating memory in your class, and the variable is newed, then you delete the variable but not that variables using new inside the class, for instance.

I hope this information was helpful.

- Eddie

---------- FOLLOW-UP ----------

QUESTION: Thanks Eddie for your explanation..
Well i will tell u where i get segmentation fault. When i am creating an object of a invuilt class and i am trying to use its methods without creating the object with new. When i create the object with new everythinh is fine. Below code explains:
//works fine
 QLabel* ob = new QLabel();

//segmentation falut comes
 QLabel* ob;

Note: Qlabel creates a label in which we can write a text

Can you please explain.
Thanks in advance


Hello again Smita, thank you for the question.

The reason you get a crash is because in the second code snippet, you are trying to access a pointer (with the -> operator) that has no value. A pointer is a variable that stores a memory address. If you want to use that pointer, you have to use new like the first example.

If you changed it this:

QLabel ob;

That would work just fine.

A pointer has to have valid memory to be able to work. In the first example, you provided it memory via the new operator. In the second example, it's an undefined pointer. A pointer has to either be newed, or set to some other object's memory address.

QLabel object; // memory is allocated by the compiler.

QLabel *p = &object // sets p equal to the address of the object variable. Now p and object both reference the same memory address, and using either variable will work.

Are you using Qt? Great tool!

I hope this information was helpful.

- Eddie


All Answers

Answers by Expert:

Ask Experts




I can answer questions about the C++ language, object oriented design and architecture. I am knowledgable in a lot of the math that goes into programming, and am certified by I also know a good deal about graphics via OpenGL, and GUIs.


I have completed numerous games and demos created with the C++ programming language. Currently employed as a software engineer in the modeling and simulation field. I have about 7 years experience.

©2016 All rights reserved.