377x Filetype PDF File size 0.10 MB Source: classes.engineering.wustl.edu
CSE 502 C++ Crash Course
Part III: More mem management
Steve Cole
January 27, 2015
1 Intro
Review: mem alloc/delete, static vs. dynamic, arrays as pointers
–Go over class Jan15 exercise code.
2 Double pointers
• Recall that a pointer can hold the address of any type, including an-
other pointer
– e.g., double** points to a double*
• Recall also that a pointer can be pointing to a single element, or the
first element of an array
– e.g., int* x = new int; // single int
int* x = new int[20]; // array of 20 ints
• Mem layout:
• Implication: a double pointer could be concealing anywhere from a
single object to a 2D array of objects
1. Single item (not common)
int** x = new int*;
*x = new int;
1
Memlayout:
2. 2D array (NROWS x NCOLS)
int** x = new int*[NROWS];
for(int i=0; i < NROWS; i++) x[i] = new int[NCOLS];
Memlayout:
3. 1D array of pointers (NROWS x 1)
int** x = new int*[NROWS];
for(int i=0; i < NROWS; i++) x[i] = new int;
Memlayout:
• 1Darray of pointers is more common for object types than POD types
–allows address to be stored multiple places
(see class Jan20 code for an example with the Point class)
2.1 Double pointers and deletion
• When using 2D pointers, must take extra care to delete all memory,
but not double-delete anything
• Deleting all memory: must pay attention to actual allocations! (single
elt vs. 1D array vs. 2D array)
1. Single item (not common)
int** x = new int*;
*x = new int;
...
delete *x;
2
delete x;
Note: must go in this order!
Memlayout:
2. 2D array (NROWS x NCOLS)
int** x = new int*[NROWS];
for(int i=0; i < NROWS; i++) x[i] = new int[NCOLS];
...
for(int i=0; i < NROWS; i++) delete[] x[i];
delete[] x;
Note: must go in this order!
Memlayout:
3. 1D array of pointers (NROWS x 1)
int** x = new int*[NROWS];
for(int i=0; i < NROWS; i++) x[i] = new int;
...
for(int i=0; i < NROWS; i++) delete x[i];
delete[] x;
Note: must go in this order!
Memlayout:
3
• Avoiding double deletions: must pay attention to actual allocations
(from new) vs. pure references.
1. 1D array of pointers (NROWS x 1)
int** x = new int*[NROWS];
for(int i=0; i < NROWS; i++) x[i] = new int;
...
int** y = new int*[NROWS];
for(int i=0; i < NROWS; i++) y[i] = x[i];
...
for(int i=0; i < NROWS; i++) delete x[i];
delete[] x;
delete[] y;
Note: Do not delete any of the y[i]s! They’ve already been
deleted.
– 3 news, 3 deletes
Memlayout:
3 Debugging
A good debugger is an invaluable tool for a programmer in any compiled
language. Just as g++ is a popular C++ compiler, though not the only one
available, gdb (GNU DeBugger) is a popular C++ debugger. Eclipse can inte-
grate with gdb just as it does with g++ to provide a full-featured debugging
experience, with features such as setting breakpoints and visually stepping
through code.
Windows users should have already installed gdb as a Cygwin package;
if Eclipse has been building your projects in a Debug directory, you have a
debugger installed.
Macusers will need to install gdb separately and then tell Eclipse where
to find it; see tutorial linked to from course website for details.
3.1 Segfaults
Segmentation faults (aka “segfaults”) happen when a C++ program tries to
access memory outside its given address space. This can happen in a vari-
ety of ways: dereferencing an uninitialized pointer, dereferencing nullptr ,
4
no reviews yet
Please Login to review.