Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Question for the C++ gurus
Message
From
13/05/2000 13:49:44
 
General information
Forum:
Visual FoxPro
Category:
Other
Miscellaneous
Thread ID:
00368531
Message ID:
00369357
Views:
23
>Does using the keyword "new" automatically put the object on the heap?

Yes.

>Before function ends, I have to delete pointer to deallocate memory used by object?

You are not deleting the pointer. You are telling the heap manager to free the memory block referenced by the pointer. The pointer itself is often allocated on the stack. Also make sure you do the appropriate delete for the type of new.

From C++ Primer Plus, Third Edition by Stephen Prata, Copyright 1998 by Sams publishing:

Don't use delete to free memory that new didn't allocate.
Don't use delete to free the same block of memory twice in succession.
Use delete [] if you used new [] to allocate an array.
Use delete (no brackets) if you used new to allocate a single entity.
It is safe to apply delete to the null pointer (nothing happens).

>If I didn't, would it automatically be deleted when object reference goes out of scope when function ends?

No, only objects created on the stack are automatically deleted when the object reference goes out of scope.
Previous
Reply
Map
View

Click here to load this message in the networking platform