By Renan Campos. When to overload new/delete and why Requirements of the implementing “new” ...
-
Upload
albert-mccarthy -
Category
Documents
-
view
212 -
download
0
Transcript of By Renan Campos. When to overload new/delete and why Requirements of the implementing “new” ...
![Page 1: By Renan Campos. When to overload new/delete and why Requirements of the implementing “new” Implementing “array new” Implementing the “delete”](https://reader036.fdocuments.us/reader036/viewer/2022083007/56649e235503460f94b1026d/html5/thumbnails/1.jpg)
ITEM 51: ADHERE TO CONVENTION WHEN
WRITING NEW AND DELETE
By Renan Campos
![Page 2: By Renan Campos. When to overload new/delete and why Requirements of the implementing “new” Implementing “array new” Implementing the “delete”](https://reader036.fdocuments.us/reader036/viewer/2022083007/56649e235503460f94b1026d/html5/thumbnails/2.jpg)
OVERVIEW When to overload new/delete and why Requirements of the implementing
“new” Implementing “array new” Implementing the “delete” operator Summary Example Code Questions
![Page 3: By Renan Campos. When to overload new/delete and why Requirements of the implementing “new” Implementing “array new” Implementing the “delete”](https://reader036.fdocuments.us/reader036/viewer/2022083007/56649e235503460f94b1026d/html5/thumbnails/3.jpg)
WHEN AND WHY (ITEM 50) To detect usage errors To collect statistics about the use of
dynamically allocated memory To increase the speed of allocation and
deallocation To reduce the space overhead of default
memory management To componsate for suboptimal alignment in
the default allocation To cluster related objects near one another To obtain unconventional behavior
![Page 4: By Renan Campos. When to overload new/delete and why Requirements of the implementing “new” Implementing “array new” Implementing the “delete”](https://reader036.fdocuments.us/reader036/viewer/2022083007/56649e235503460f94b1026d/html5/thumbnails/4.jpg)
IMPLEMENTING NEWThe new operator requires:1. The right return value2. Calling the new-handling function
when insufficient memory is available3. Being prepared to cope with requests
for no memory4. Avoiding hiding the normal form of
new (See item 52)5. Handle inherited classes
![Page 5: By Renan Campos. When to overload new/delete and why Requirements of the implementing “new” Implementing “array new” Implementing the “delete”](https://reader036.fdocuments.us/reader036/viewer/2022083007/56649e235503460f94b1026d/html5/thumbnails/5.jpg)
IMPLEMENTING NEW[]
Just allocate a chunk of raw memory.
![Page 6: By Renan Campos. When to overload new/delete and why Requirements of the implementing “new” Implementing “array new” Implementing the “delete”](https://reader036.fdocuments.us/reader036/viewer/2022083007/56649e235503460f94b1026d/html5/thumbnails/6.jpg)
IMPLEMENTING DELETEvoid operator delete( void* rawMem ) throw() {
if ( rawMem == 0 ) return ;
// deallocate the memory pointed
}
![Page 7: By Renan Campos. When to overload new/delete and why Requirements of the implementing “new” Implementing “array new” Implementing the “delete”](https://reader036.fdocuments.us/reader036/viewer/2022083007/56649e235503460f94b1026d/html5/thumbnails/7.jpg)
SUMMARYFor new: Operator new should contain an infinite loop
trying to allocate memory. New_handler should be called if memory
request failed. Class-specific versions should handle
requests for larger blocks than expected.For delete: Operator delete should do nothing if given a
null pointer. Class-specific versions should handle
requests for larger blocks than expected.
![Page 8: By Renan Campos. When to overload new/delete and why Requirements of the implementing “new” Implementing “array new” Implementing the “delete”](https://reader036.fdocuments.us/reader036/viewer/2022083007/56649e235503460f94b1026d/html5/thumbnails/8.jpg)
SAMPLE CODE
![Page 9: By Renan Campos. When to overload new/delete and why Requirements of the implementing “new” Implementing “array new” Implementing the “delete”](https://reader036.fdocuments.us/reader036/viewer/2022083007/56649e235503460f94b1026d/html5/thumbnails/9.jpg)
QUESTIONS?