Online Book Reader

Home Category

Professional C__ - Marc Gregoire [383]

By Root 1527 0
such behavior will work or continue to work. The memory allocator is under no obligation to preserve any object once it has been deleted. Even if it does work, it is extremely poor programming style to use objects that have been deleted.

Many memory leak checking programs, such as Microsoft Visual C++ and Valgrind, will also detect double deletion and use of released objects.

If you disregard the recommendation for using smart pointers and instead still use dumb pointers, at least set your pointers to nullptr after deallocating their memory. This will prevent you from accidentally deleting the same pointer twice or to use an invalid pointer. It’s worth noting that you are allowed to call delete on a nullptr pointer; it simply will not do anything.

Accessing Out-of-Bounds Memory

Earlier in this chapter, you read that since a pointer is just a memory address, it is possible to have a pointer that points to a random location in memory. Such a condition is quite easy to fall into. For example, consider a C-style string that has somehow lost its '\0' termination character. The following function, which attempts to fill the string with all 'm' characters, would instead continue to fill the contents of memory following the string with 'm's:

void fillWithM(char* inStr)

{

int i = 0;

while (inStr[i] != '\0') {

inStr[i] = 'm';

i++;

}

}

If an improperly terminated string were handed to this function, it would only be a matter of time before an essential part of memory is overwritten and the program crashes. Consider what might happen if the memory associated with the objects in your program is suddenly overwritten with 'm's. It’s not pretty!

Bugs that result in writing to memory past the end of an array are often called buffer overflow errors. Such bugs have been exploited by several high-profile malware programs, for example viruses and worms. A devious hacker can take advantage of the ability to overwrite portions of memory to inject code into a running program.

Many memory checking tools detect buffer overflows as well. Also, using higher-level constructs like C++ strings and vectors will help prevent numerous bugs associated with writing to C-style strings and arrays.

Avoid using old C-style strings and arrays that offer no protection whatsoever. Instead, use modern and safe constructs like C++ strings and vectors that manage all their memory for you.

SUMMARY


In this chapter, you learned the ins and outs of dynamic memory. Aside from memory checking tools and careful coding, there are two keys to avoiding dynamic memory-related problems. First, you need to understand how pointers work under the hood. In reading about two different mental models for pointers, we hope you are confident that you know how the compiler doles out memory. Second, you can avoid all sorts of dynamic memory issues by obscuring pointers with stack-based objects, like the C++ string class, vector class, smart pointers, and so on.

If there is one thing that you should have learned from this chapter it is that you should try to avoid the use of old C-style constructs and functions as much as possible, and use the safe C++ alternatives.

Chapter 22

Multithreaded Programming with C++


WHAT’S IN THIS CHAPTER?

What multithreaded programming is and how to write multithreaded code

What deadlocks and race conditions are, and how to use mutual exclusion to prevent them

How to use atomic types and atomic operations

What thread pools are

Multithreaded programming is important on computer systems with multiple processors. It allows you to write a program to utilize all those processors in parallel. Systems with multiple processors already exist for a long time; however, they were rarely used in consumer systems. Today, all major CPU vendors are selling multicore processors. A multicore processor physically looks like a single processor, but inside it contains several parallel CPU processors, also called cores. Nowadays, multicore processors are being used for everything from servers to consumer computers. Even the latest smartphones

Return Main Page Previous Page Next Page

®Online Book Reader