C++-mem-mgnt4

C++-mem-mgnt4 - C++ Dynamic Memory Management Techniques...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: C++ Dynamic Memory Management Techniques Douglas C. Schmidt Professor Department of EECS d.schmidt@vanderbilt.edu Vanderbilt University www.dre.vanderbilt.edu/ schmidt/ (615) 343-8197 UCLA Extension Course OO Programming with C++ Dynamic Memory Management In C++, the new() and delete() operators provide built-in language support for dynamic memory allocation and deallocation. This feature has several benefits: Reduces common programmer errors : it is easy to forget to multiply the number of objects being allocated by sizeof when using malloc() , e.g. , // oops, only 2 1/2 ints! int *a = (int *) malloc (10); Enhances source code clarity : generally, there is no need to: (1) declare operator new() and delete() , (2) explicitly use casts , or (3) explicitly check the return value. Improves run-time efficiency : (1) users can redefine operator new() and delete() globally and also define them on a per- class basis and (2) calls can be inlined. Copyright c 1997-2003 Vanderbilt University 1 UCLA Extension Course OO Programming with C++ Dynamic Memory Management (contd) Operator new() can be either a globally defined function or a member of class T or a base class of T. Here is a minimal example of a global definition of operator new(): extern "C" void *malloc (size_t); void *operator new() (size_t sz) { return malloc (sz); } There must be only one global operator new() (with these particular argument types) in an executable Note, it is possible to overload operator new() ! if you do not supply your own, there is one in the C++ run-time library thats only a little more complicated than this one. Copyright c 1997-2003 Vanderbilt University 2 UCLA Extension Course OO Programming with C++ Dynamic Memory Management (contd) Operator new() , be it local or global, is only used for free store allocation Therefore, the following does not involve any direct invocation of operator new() : X a; X f (void) { X b; /* ... */ return b; } Note, an object allocated from the free store has a lifetime that extends beyond its original scope, int *f (int i) { int *ip = new() int[i]; // ... return ip; } Copyright c 1997-2003 Vanderbilt University 3 UCLA Extension Course OO Programming with C++ Error Handling By default, if operator new() cannot find memory it calls a pointer to function called new handler() , e.g. , void *operator new() (size_t size) { void *p; while ((p = malloc (size)) == 0) if (_new_handler) (*_new_handler)(); else return 0; return p; } if new handler() can somehow supply memory for malloc() then all is fine - otherwise, an exception is thrown Note, new handler() can be set by users via the set new handler() function, e.g....
View Full Document

Page1 / 9

C++-mem-mgnt4 - C++ Dynamic Memory Management Techniques...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online