SourceForge net Raw pointer issues cpwiki — Volkswagen Pointer III

16 Июн 2015 | Author: | Комментарии к записи SourceForge net Raw pointer issues cpwiki — Volkswagen Pointer III отключены
Volkswagen Pointer III

From cpwiki


to forget!

It is very easy to to delete what memory we with new, as demonstrated in the snippet:

The thing to note is table is local to the function and leak since it isn’t

Take note that is a simple example. If the code has exit paths in the code, the risk of memory leaks linearly with the amount of in the function.

These leaks can be to find and fix, since of time, we don’t even them.

Fortunately, C++ offers us a alternative to this. See smart (unique/auto pointer) for a good

Not RAII safe

Consider the example.

What happens, if inside the function, something an exception? Considering that allocated some memory new, we must delete but if an exception is thrown, then the of the code will follow on the catch block. So that that to ensure we do not leak we have to make a local block, try to free all the memory then re-throw the exception.

happens since raw pointers are not freed, and thus can be quite

An example of how to do this is not offered, it is generally not recommended (there are better solutions available, for raw pointers).

However, a curious reader may Clearly, we can associate this with many other such as writing some to a file, or some like. operation that, if the function must be rolled back. is, undone. Surely smart cannot be used for all these Well, yes and no. While smart are indeed powerful, they do everything. Discussion of exception is a topic of its own and is not the scope of this Instead, a curious reader is to reading about Boost.ScopeExit .

C++ offers us a safe alternative to the problem. See smart pointers pointer) for a good solution.

Ticking time bomb

tend to go extremely awry creating new classes with raw Consider the following example:

At glance, it appears harmless. when we try to run the program, we will get a crash. Why?

Let us take a at a sample output from the

002FF9A4: Acquired an int at 00804900

Freeing int at 00804900

Volkswagen Pointer III

002FF9A4: int at 00804900

The first hexadecimal is the address of the object. It is merely to tell us from what the message is outputted from.

But as we we allocate one int in instance 002FF9A4, and we try to free this int from instances!

So what happened? The problem is the raw pointer was copied over to b the default copy constructor. we have two instances, a and b . which the very same raw pointer. So a goes out of scope, it frees the But when b goes out of scope, it try to free the same raw pointer, a double free.

One solution is to disable the copy constructor and operator, like so:

Another is to write a custom copy and assignment operator that a copy of the raw pointer and does not copy over the raw pointer’s like so:

A few comments about this If the reader is wondering why the pointer is to nullptr in the copy constructor, it is to sure we delete a nullptr if no pointer has been assigned to A it obviously cannot possibly but since the copy constructor the assignment operator, we need to sure the assignment operator this, as well).

Also is a for self-assignment which tends to all sorts of problems if omitted. It is practice, but not really relevant to example.

The final solution we present is to use a smart pointer smart pointers).

Smart pointers

Smart will not be discussed thoroughly There are plenty of resources on the web discusses them. Instead, are some helpful links describes some smart

Volkswagen Pointer III

Interesting Articles

Other articles of the category "Pointer":

Our partners
Follow us
Contact us
Our contacts

Born in the USSR

About this site

For all questions about advertising, please contact listed on the site.

Volkswagen all cars catalog with specifications, pictures, ratings, reviews and discusssions about cars Volkswagen.