It is a concept that is very conveniently skipped while learning C++ at the beginner's level. Having used the 'new' and 'delete' operators, it is imperative to understand how the C++ memory is managed.
1. What does the 'new' operator actually do??
Ans. It basically does two things - allocate memory for the new object and call the constructor of that object and thereby in effect 'create' the object.
The C++ Memory Heap a.k.a the 'free-store' is used to assign chunks of memory size as requested by each constructor that is called by the 'new' operator on that object.
In comparison to the malloc() function, the new operator returns a type-safe pointer while th malloc() return a pointer of type 'void' which needs to be explicity type-casted by the programmer.
For the special case when the memory store available with the free store is exhausted, we can test if the new operator returns a NULL.. But there is a more elegant way to handle this case.. look for my next post on how to handle the situation of free store exhuastion.
Friday, September 5, 2008
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment