jagomart
digital resources
picture1_C   Mem2


 207x       Filetype PDF       File size 0.10 MB       Source: classes.engineering.wustl.edu


File: C Mem2
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 ...

icon picture PDF Filetype PDF | Posted on 01 Feb 2023 | 2 years ago
Partial capture of text on file.
                         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
The words contained in this file might help you see if this file matches what you are looking for:

...Cse c crash course part iii more mem management steve cole january intro review alloc delete static vs dynamic arrays as pointers go over class jan exercise code double recall that a pointer can hold the address of any type including an other e g points to also be pointing single element or rst array int x new ints layout implication could concealing anywhere from object d objects item not common memlayout nrows ncols for i darray is types than pod allows stored multiple places see example with point and deletion when using must take extra care all memory but anything deleting pay attention actual allocations elt note in this order avoiding deletions pure references y do ys they ve already been deleted news deletes debugging good debugger invaluable tool programmer compiled language just popular compiler though only one available gdb gnu eclipse inte grate it does provide full featured experience features such setting breakpoints visually stepping through windows users should have inst...

no reviews yet
Please Login to review.