source: trunk/test_more/track_new_del.cpp @ 2439

Last change on this file since 2439 was 2439, checked in by bradbell, 8 years ago

Change BEGIN PROGRAM to BEGIN C++ using followind sed commands:

s| BEGIN PROGRAM| BEGIN C++|
s| END PROGRAM| END C++|

  • Property svn:keywords set to Id
File size: 1.7 KB
Line 
1/* $Id: track_new_del.cpp 2439 2012-06-18 02:28:36Z bradbell $ */
2/* --------------------------------------------------------------------------
3CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-12 Bradley M. Bell
4
5CppAD is distributed under multiple licenses. This distribution is under
6the terms of the
7                    Common Public License Version 1.0.
8
9A copy of this license is included in the COPYING file of this distribution.
10Please visit http://www.coin-or.org/CppAD/ for information on other licenses.
11-------------------------------------------------------------------------- */
12
13/*
14$begin TrackNewDel.cpp$$
15
16$section Tracking Use of New and Delete: Example and Test$$
17
18$index new, example$$
19$index example, new$$
20$index test, new$$
21
22$index delete, example$$
23$index example, delete$$
24$index test, delete$$
25
26$code
27$verbatim%test_more/track_new_del.cpp%0%// BEGIN C++%// END C++%1%$$
28$$
29
30$end
31*/
32// BEGIN C++
33
34# include <cppad/track_new_del.hpp>
35
36bool track_new_del(void)
37{       bool ok = true;
38
39        // initial count
40        size_t count = CPPAD_TRACK_COUNT();
41
42        // allocate an array of lenght 5
43        double *ptr = 0;
44        size_t  newlen = 5;
45        ptr = CPPAD_TRACK_NEW_VEC(newlen, ptr);
46
47        // copy data into the array
48        size_t ncopy = newlen;
49        size_t i;
50        for(i = 0; i < ncopy; i++)
51                ptr[i] = double(i);
52
53        // extend the buffer to be lenght 10
54        newlen = 10;
55        ptr    = CPPAD_TRACK_EXTEND(newlen, ncopy, ptr);
56               
57        // copy data into the new part of the array
58        for(i = ncopy; i < newlen; i++)
59                ptr[i] = double(i);
60
61        // check the values in the array
62        for(i = 0; i < newlen; i++)
63                ok &= (ptr[i] == double(i));
64
65        // free the memory allocated since previous call to TrackCount
66        CPPAD_TRACK_DEL_VEC(ptr);
67
68        // check for memory leak
69        ok &= (count == CPPAD_TRACK_COUNT());
70
71        return ok;
72}
73
74// END C++
Note: See TracBrowser for help on using the repository browser.