mirror of
https://gitlab.com/fbb-git/cppannotations
synced 2024-11-16 07:48:44 +01:00
f61c34b9c7
git-svn-id: https://cppannotations.svn.sourceforge.net/svnroot/cppannotations/trunk@432 f6dd340e-d3f9-0310-b409-bdd246841980
57 lines
3.1 KiB
Text
57 lines
3.1 KiB
Text
In contrast to the set of functions that handle i(memory allocation) in bf(C)
|
|
(i.e., ti(malloc) etc.), memory allocation in bf(C++) is handled by
|
|
the operators ti(new) and ti(delete).
|
|
Important differences between tt(malloc) and tt(new) are:
|
|
itemization(
|
|
it() The function tt(malloc) doesn't `know' what the allocated memory
|
|
will be used for. E.g., when memory for tt(int)s is allocated, the programmer
|
|
must supply the correct expression using a multiplication by
|
|
tt(sizeof(int)). In contrast, tt(new) requires a type to be specified; the
|
|
ti(sizeof) expression is implicitly handled by the compiler. Using tt(new) is
|
|
therefore emi(type safe).
|
|
it() Memory allocated by tt(malloc) is initialized by ti(calloc),
|
|
initializing the allocated characters to a configurable initial value. This
|
|
is not very useful when objects are available. As operator tt(new) knows about
|
|
the type of the allocated entity it may (and will) call the constructor of an
|
|
allocated class type object. This constructor may be also supplied with
|
|
arguments.
|
|
it() All bf(C)-allocation functions must be inspected for
|
|
ti(NULL)-returns. This is not required anymore when tt(new) is used. In fact,
|
|
tt(new)'s behavior when confronted with failing memory allocation is
|
|
configurable through the use of a em(new_handler) (cf. section
|
|
ref(NEWHANDLER)).
|
|
)
|
|
A comparable relationship exists between ti(free) and tt(delete):
|
|
tt(delete) makes sure that when an object is deallocated, its
|
|
destructor is automatically called.
|
|
|
|
The automatic calling of constructors and destructors when objects are created
|
|
and destroyed has consequences which we shall discuss in this
|
|
chapter. Many problems encountered during bf(C) program development are caused
|
|
by incorrect memory allocation or i(memory leaks): memory is not allocated,
|
|
not freed, not initialized, boundaries are overwritten, etc.. bf(C++) does
|
|
not `magically' solve these problems, but it em(does) provide us with tools
|
|
to prevent these kinds of problems.
|
|
|
|
As a consequence of tt(malloc) and friends becoming deprecated
|
|
the very frequently used ti(str...) functions, like
|
|
ti(strdup), that are all tt(malloc) based, should be avoided in
|
|
bf(C++) programs. Instead, the facilities of the tt(string) class and
|
|
operators tt(new) and tt(delete) should be used instead.
|
|
|
|
Memory allocation procedures influence the way classes dynamically allocating
|
|
their own memory should be designed. Therefore, in this chapter these topics
|
|
are discussed in addition to discussions about operators tt(new) and
|
|
tt(delete). We'll first cover the peculiarities of operators tt(new) and
|
|
tt(delete), followed by a discussion about:
|
|
itemization(
|
|
it() the destructor: the member function that's called when an object
|
|
ceases to exist;
|
|
it() the assignment operator, allowing us to assign an object to another
|
|
object of its own class;
|
|
it() the tt(this) pointer, allowing explicit references to the object for
|
|
which a member function was called;
|
|
it() the copy constructor: the constructor creating a copy of an object;
|
|
it() the move constructor: a constructor creating an object from
|
|
an anonymous temporary object.
|
|
)
|