Online Book Reader

Home Category

Professional C__ - Marc Gregoire [464]

By Root 1242 0
if the array was on the heap, you could cause memory corruption in the heap, which will cause errors later when you attempt to allocate or free more memory dynamically.

Chapter 21 introduces some of the common memory errors from the perspective of what to avoid when you’re coding. This section discusses memory errors from the perspective of identifying problems in code that exhibits bugs. You should be familiar with the discussion in Chapter 21 before continuing with this section.

Categories of Memory Errors

In order to debug memory problems you should be familiar with the types of errors that can occur. This section describes the major categories of memory errors. Each memory error includes a small code example demonstrating the error and a list of possible symptoms that you might observe. Note that a symptom is not the same thing as a bug itself: A symptom is an observable behavior caused by a bug.

Memory Freeing Errors

The following table summarizes five major errors involving freeing memory.

ERROR TYPE SYMPTOMS EXAMPLE

Memory leak Process memory usage grows over time.

Process runs slower over time.

Eventually, operations and system calls fail because of lack of memory. void memoryLeak()

{

int* p = new int[1000];

return; // BUG! Not freeing p.

}

Using mismatched allocation and free operations Does not usually cause a program crash immediately.

Can cause memory corruption on some platforms, which might show up as a program crash (segmentation violation) later in the program. void mismatchedFree()

{

int* p1 = (int*)malloc(sizeof(int));

int* p2 = new int;

int* p3 = new int[1000];

delete p1; // BUG! Should use free

delete[] p2; // BUG! Should use delete

free(p3); // BUG! Should use delete[]

}

Freeing memory more than once Can cause a program crash (segmentation violation) if the memory at that location has been handed out in another allocation between the two calls to delete. void doubleFree()

{

int* p1 = new int[1000];

delete[] p1;

int* p2 = new int[1000];

delete[] p1; // BUG! freeing p1 twice

}

Freeing unallocated memory Will usually cause a program crash (segmentation violation or bus error). void freeUnallocated()

{

int* p = reinterpret_cast(10000);

delete p; // BUG! p not a valid pointer.

}

Freeing stack memory Technically a special case of freeing unallocated memory. Will usually cause a program crash. void freeStack()

{

int x;

int* p = &x;

delete p; // BUG! Freeing stack memory

}

As you can see, some of the memory free errors do not cause immediate program termination. These bugs are more subtle, leading to problems later in the run of the program.

Most of the preceding problems can be avoided by using smart pointers instead of dumb pointers. Smart pointers are discussed in Chapter 21.

Memory Access Errors

The second category of memory errors involves the actual reading and writing of memory.

ERROR TYPE SYMPTOMS EXAMPLE

Accessing Invalid Memory Almost always causes program to crash immediately. void accessInvalid()

{

int* p = reinterpret_cast(10000);

*p = 5; // BUG! p is not a valid pointer.

}

Accessing Freed Memory Does not usually cause a program crash.

If the memory has been handed out in another allocation, can cause “strange” values to appear unexpectedly. void accessFreed()

{

int* p1 = new int;

delete p1;

int* p2 = new int;

*p1 = 5; // BUG! The memory pointed to

// by p1 has been freed.

}

Accessing Memory in a Different Allocation Does not cause a program crash.

Can cause “strange” values to appear unexpectedly. void accessElsewhere()

{

int x, y[10], z;

x = 0;

z = 0;

for (int i = 0; i <= 10; i++) {

y[i] = 5; // BUG for i==10! element 10

// is past end of array.

}

}

Reading Uninitialized Memory Does not cause a program crash unless you use the uninitialized value as a pointer and dereference it (as in the example). Even then, it will not always cause a program crash. void readUninitialized()

{

int* p;

cout << *p; // BUG! p is uninitialized

}

Memory access errors are more likely than memory free errors to cause program

Return Main Page Previous Page Next Page

®Online Book Reader