Re: Best way to allocate memory in the constructor
salvatore benedetto wrote:
Hi,
What's the best way to allocate memory in the constructor and avoid
memory leaks if the constructor fails?
The general advice is to use separate, dedicated, classes for the memory
management and the application logic.
A memory management class should not allocate more than one block of
memory in its constructor. Note that the standard provides a set of
classes that fit most needs for memory management.
Let's say that in my constructor, I have to allocate memory
with malloc (i.e. an array of char *)several times, and one
of them fails.
I will assume that you are somehow prohibited from replacing the raw
pointers with std::string instances.
What's the best way to deallocate the previously allocated memory?
Is the destructor being called if I "return" in the middle of
the constructor body? If not, is it a good idea to call explicity
the constructor in order to clean the memory?
At the moment, I simply check every allocation and if the check fails
I deallocate all the memory previously allocated, but this produces
a lot of code duplication.
That is generally the accepted way of dealing with malloc() failures.
Note that is does not work when you use new/new[] to allocate the
memory.
Example:
if ((first = (char *)malloc(sizeof(....)) == NULL)
return;
if ((second = (char *)malloc.... )) == NULL) {
free(first); return;
}
if ((third = (int *)malloc... )) == NULL) {
free(second);
free(first);
return;
}
// and so on!
You can reduce duplication, by first initialising all pointers to NULL
and deallocating only after you have tried to allocate them all.
For example:
first = NULL;
second = NULL;
third = NULL;
first = (char*)malloc(/*...*/);
second = (char*)malloc(/*...*/);
third = (char*)malloc(/*...*/);
if ((first == NULL) ||
(second == NULL) ||
(third == NULL)
)
{
free(first);
free(second);
free(third);
//throw an exception to report falure
}
This works, because free(NULL) is required to work and do nothing.
Thanks in advance for your help.
Salvo
Bart v Ingen Schenau
--
a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq
c.l.c FAQ: http://c-faq.com/
c.l.c++ FAQ: http://www.parashift.com/c++-faq-lite/
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]