Automatic/Manual memory allocation

B

Bartosz Wiklak

Hi, I hope this question is not too general and I understand that is
was asked partially in many places.
I'm always surprised reading posts at this group concerning manual
memory allocation, pointers, especially void* pointers.
People write sometimes that "there is nothing bad in void*", manual
pointer manipulations is ok and so on.
I have many old habits, I'm always tempted to use malloc rather than
new[] etc but since I'm not involved in scientific programming where
programs were rather short and topic was far more complicated than the
software, and I work on bigger projects where maintenance is a big
issue I hate manual memory allocation.
Please don't get me wrong, I worked in Java for some time and it's
garbage collector driven me crazy but when the overload of automatic
memory management is only several percent of time comparing to manual
management I would like to use it.

What are the general patters used to get rid of manual memory
allocation and management? I'm talking about using vectors instead of
tables, or generally using RAII. What are the pitfalls?
For example, for all time I used a pattern of creating a single object
and then, in containers and methods, just using a pointer to that
object. This gave me many problems when destroying the object,
ensuring that all references are up to date.
On the other hand, I still worry that if I just use an object instance
in all places ( like containers ) I'll end with memory filled up with
many instances of the same object.

I thought that shared_ptr would be a good idea but I would't like to
get used to boost to much as it is forbidden to use it in many
companies.

Do you have the same problems? What are your solutions?

Regs,
Bartek
 
R

Robert Fendt

For example, for all time I used a pattern of creating a single object
and then, in containers and methods, just using a pointer to that
object. This gave me many problems when destroying the object,
ensuring that all references are up to date.

In that case there's something wrong with your design from the
start. Memory is a resource, and it has to have an owning
object. It sounds to me like your semantics were wrong, which
can lead to all sorts of problems which are _not_ solved by the
mere presence of garbage collection. And not by smart pointers
either. That said, shared_ptr/weak_ptr can be a great asset (if
used wisely) and can make things a lot easier for the programmer.
On the other hand, I still worry that if I just use an object instance
in all places ( like containers ) I'll end with memory filled up with
many instances of the same object.

The very core of RAII is that you know precisely when a resource
will be freed. So if an object copy is dropped from a container
or from the scope, its resources will be deleted *then and
there*. If you really have "the same" resource myriads of times
inside a container, then I dare say again your design is flawed.
Apart from that, many objects have a surprisingly small memory
footprint, since the size is essentially the sum of its member
variables (not functions), plus the vtable pointer in case of
polymorphic classes.
I thought that shared_ptr would be a good idea but I would't like to
get used to boost to much as it is forbidden to use it in many
companies.

Pardon my french, but that's just bull. A company that does not
evaluate available assets is doomed to fail in any case, and
rightly so. If the company shuns a piece of high-quality
technology that's even available to them free of charge, they
are just plain stupid, sorry. By the way, Boost is not a
monolithic library but rather a sum of many elements, most of
which can be used separately. shared_ptr is a template, it does
not introduce linker dependencies and AFAIK it can be used
'standalone' quite nicely.

Also, consider that shared_ptr is even a special case: it is
part of TR1, it will be part of C++ 0x and it is already
supported by VC++ (8.0+ I think) and by GCC 4 out of the box.
Although I would still strongly suggest to use the Boost version
for now since it is the more portable solution.

Regards,
Robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top