03.12.2012 Views

C++ for Scientists - Technische Universität Dresden

C++ for Scientists - Technische Universität Dresden

C++ for Scientists - Technische Universität Dresden

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

2.10. POINTERS AND REFERENCES 51<br />

Furthermore, the programmer is responsible <strong>for</strong> releasing the memory when not needed anymore.<br />

This is done by<br />

delete[] v;<br />

As we came from arrays, we made the second step be<strong>for</strong>e the first one regarding pointer usage.<br />

The simple use of pointers is allocating one single data item.<br />

int∗ ip = new int;<br />

Releasing such memory is per<strong>for</strong>med by<br />

delete ip;<br />

Note the duality of allocation and release: the single-object allocation requires a single-object<br />

release and the array allocation demands an array release. 20<br />

Pointers can also refer to other variables<br />

int i= 3;<br />

int∗ ip2= &i;<br />

The operator & takes an object and returns its address. The reverse operator is ∗ that takes<br />

an address and returns object.<br />

int j= ∗ip2;<br />

This is called dereferencing. It is clear from the context whether the symbol ∗ represents a<br />

dereference or a multiplication.<br />

A danger of pointers are memory leaks. For instance, our array y became too small and we<br />

want assign a new array<br />

int∗ y = new int[15];<br />

We can now use more space in y. Nice. But what happened with the memory that we allocated<br />

be<strong>for</strong>e? It is still there but we have no access to it anymore. We cannot release it anymore.<br />

This memory is lost <strong>for</strong> the rest of our program execution. Only when the program is finished<br />

the operation system will be able to free it. In the example it is only 40 byte out of how many<br />

Gigabyte you might have. But if this happens with larger data in an iterative process the dead<br />

memory grows and at some point the program crashes when all memory is used.<br />

The warnings above are not intended as fun killers. And we do not discourage the use of<br />

pointers. Many things can be only achieved with pointers: lists, queues, trees, graphs, . . . But<br />

pointers must be used with utter care to avoid all the really serious problems mentioned above.<br />

There are two strategies to minimize pointer-related errors:<br />

Use standard implementations from the standard library or other validated libraries. std::vector<br />

from the standard library provides you all the functionality of dynamic arrays, including<br />

resizing and range check, and the memory is released automatically, see § 4.9. Smart pointers<br />

from Boost provide automatic resource management: dynamically allocated memory<br />

that is not referred by a smart pointer is released automatically, see § 11.2.<br />

20 TODO: Otherwise?

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!