| | |
C++ Basics: Object Instantiation vs. Dynamic Memory Allocation
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2006
Posts: 1
Reputation:
Solved Threads: 0
Quick question while learning C++:
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?
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?
•
•
Join Date: Feb 2005
Posts: 17
Reputation:
Solved Threads: 2
Your code wont compile as is. What you need to have is
C++ Syntax (Toggle Plain Text)
void aFunction { A a; // create local auto object on the stack A* ap = new A() // create object on the heap & have ap refer to it // do something delete ap; // manually delete object that ap refers to } // here a & ap are destroyed automatically
![]() |
Similar Threads
- Static and Dynamic Memory Allocations...Advan's & Disadvan's??? (Computer Science)
- regarding dynamic memory allocation (C)
- memory allocation ptr to array? how? (C)
- Dynamic memory allocation homework (C++)
Other Threads in the C++ Forum
- Previous Thread: path
- Next Thread: Not home work help but very ver simple
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bmp c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll download dynamic encryption error file forms fstream function functions game givemetehcodez google graph gui iamthwee ifstream input int integer java lib library linkedlist linker linux loop looping loops map math matrix memory microsoft newbie news number output pointer problem program programming project python random read recursion recursive reference return string strings struct studio system temperature template templates test text text-file tree unix url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






