Prev Next TrackNewDel.cpp Headings

@(@\newcommand{\W}[1]{ \; #1 \; } \newcommand{\R}[1]{ {\rm #1} } \newcommand{\B}[1]{ {\bf #1} } \newcommand{\D}[2]{ \frac{\partial #1}{\partial #2} } \newcommand{\DD}[3]{ \frac{\partial^2 #1}{\partial #2 \partial #3} } \newcommand{\Dpow}[2]{ \frac{\partial^{#1}}{\partial {#2}^{#1}} } \newcommand{\dpow}[2]{ \frac{ {\rm d}^{#1}}{{\rm d}\, {#2}^{#1}} }@)@
Tracking Use of New and Delete: Example and Test

# include <cppad/utility/track_new_del.hpp>

bool track_new_del(void)
{     bool ok = true;

     // initial count
     size_t count = CPPAD_TRACK_COUNT();

     // allocate an array of length 5
     double *ptr = CPPAD_NULL;
     size_t  newlen = 5;
     ptr = CPPAD_TRACK_NEW_VEC(newlen, ptr);

     // copy data into the array
     size_t ncopy = newlen;
     size_t i;
     for(i = 0; i < ncopy; i++)
          ptr[i] = double(i);

     // extend the buffer to be length 10
     newlen = 10;
     ptr    = CPPAD_TRACK_EXTEND(newlen, ncopy, ptr);

     // copy data into the new part of the array
     for(i = ncopy; i < newlen; i++)
          ptr[i] = double(i);

     // check the values in the array
     for(i = 0; i < newlen; i++)
          ok &= (ptr[i] == double(i));

     // free the memory allocated since previous call to TrackCount
     CPPAD_TRACK_DEL_VEC(ptr);

     // check for memory leak
     ok &= (count == CPPAD_TRACK_COUNT());

     return ok;
}

Input File: example/deprecated/track_new_del.cpp