File: track_new_del.cpp

package info (click to toggle)
cppad 2026.00.00.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,584 kB
  • sloc: cpp: 112,960; sh: 6,146; ansic: 179; python: 71; sed: 12; makefile: 10
file content (59 lines) | stat: -rw-r--r-- 1,331 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
// SPDX-FileCopyrightText: Bradley M. Bell <bradbell@seanet.com>
// SPDX-FileContributor: 2003-22 Bradley M. Bell
// ----------------------------------------------------------------------------

/*
@begin TrackNewDel.cpp@@

$section Tracking Use of New and Delete: Example and Test$$



$srcthisfile%0%// BEGIN C++%// END C++%1%$$

$end
*/
// BEGIN C++

# 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 = nullptr;
   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;
}

// END C++