Board index » cppbuilder » Simple Memory allocation for a beginner

Simple Memory allocation for a beginner


2007-12-18 02:09:03 AM
cppbuilder90
I haven't used C++ in quite a while, and I can't remember some simple memory
allocation questions.
When you make an object like
thehandler = new someobjecttype;
Do you have to use memalloc or the like to create the new object in the
someobjecttype constructor, or what?
 
 

Re:Simple Memory allocation for a beginner

Hi Dan
Dan says:
Quote
I haven't used C++ in quite a while, and I can't remember some simple memory
allocation questions.

When you make an object like
thehandler = new someobjecttype;

Do you have to use memalloc or the like to create the new object in the
someobjecttype constructor, or what?
TEdit* Edit = new TEdit(this);
//the edit have an owner so You don't need to delete
Graphics::TBitmap* Bitmap = new Graphics::TBitmap();
//here is no owner so You must delete
delete Bitmap;
Kind regards
Asger
 

Re:Simple Memory allocation for a beginner

"Dan" < XXXX@XXXXX.COM >writes:
Quote
I haven't used C++ in quite a while, and I can't remember some simple
memory allocation questions.

When you make an object like
thehandler = new someobjecttype;
The above "new expression" will allocate memory from the heap and
construct the object in that memory. Nothing further needs to be done
before you can use it.
Quote
Do you have to use memalloc or the like to create the new object in
the someobjecttype constructor, or what?
Nope.
But be sure to remember to delete the object when you're done with it.
--
Chris (TeamB);
 

{smallsort}

Re:Simple Memory allocation for a beginner

"Dan" < XXXX@XXXXX.COM >wrote in message
Quote
When you make an object like
thehandler = new someobjecttype;

Do you have to use memalloc or the like to create the
new object in the someobjecttype constructor, or what?
No. 'new' allocates the memory for you, and then calls the constructor.
The class's constructor is responsible for invoking the base class
constructor (if there is one) and initializing any class members.
Use 'delete' to destroy the object and free its memory:
delete thehandler;
Gambit