File: tut7.html

package info (click to toggle)
libcwd 1.0.4-1.1
  • links: PTS
  • area: non-free
  • in suites: jessie, jessie-kfreebsd
  • size: 8,136 kB
  • ctags: 10,313
  • sloc: cpp: 23,354; sh: 9,798; ansic: 1,172; makefile: 852; exp: 234; awk: 11
file content (603 lines) | stat: -rw-r--r-- 20,009 bytes parent folder | download | duplicates (4)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML LANG="en-us">
<HEAD>
<META name="Author" content="Carlo Wood">
<META http-equiv="content-type" content="text/html; charset=iso-8859-1">
<META http-equiv="content-script-type" content="text/javascript">
<TITLE>libcwd: The C++ Debugging Support Library - Tutorial</TITLE>
<SCRIPT SRC="../scripts/detect_browser.js"></SCRIPT>
<SCRIPT>need_style_tutorial=1</SCRIPT>
<SCRIPT SRC="../scripts/load_style_sheets.js"></SCRIPT>
</HEAD>
<BODY>

<TABLE class="header" height=64 width="100%" cellpadding=0 cellspacing=0 border=0>
  <TR>
    <TD width=237 valign=top>
      <IMG valign=top src="../images/libcwd_logo.png" alt="" align=left border=0>
    </TD>
    <TD width="100%" align=center>
      <DIV class="header-title">
      The C++ Debugging Support Library
      </DIV>
      <DIV class="header-copyright">
      By Carlo Wood, &copy;1999 - 2003.
      </DIV>
    </TD>
  </TR>
  <SCRIPT>if (is_mozilla4) document.write("<TR><TD colspan=2 height=19 valign=bottom><HR SIZE=2 NOSHADE></TD></TR>");</SCRIPT>
</TABLE>

<DIV class="body">

<H2>Tutorial 7: Advanced examples</H2>

<P>In this tutorial you will learn how to make memory allocations
&laquo;invisible&raquo; so that they will not show up in the
Allocated memory Overview, how to find information about an
allocated memory block given an arbitrary pointer pointing
inside it and how to write simple memory-leak detection code.</P>

<A NAME="Removing"></A>
<H3>7.1 Removing allocations from the Allocated memory Overview</H3>

<P>Sometimes a program can allocate a very large number of memory blocks.&nbsp;
Having all of those in the Allocated memory Overview could make it
impractically large.&nbsp; Therefore it is possible to remove items from this list.</P>

<P>In the following example we make one allocation invisible by
using the function <CODE>make_invisible()</CODE>:</P>

<P>Compile as: <CODE>g++ -g -DCWDEBUG test7.1.1.cc -lcwd -o invisible</CODE></P>
<P class="download">[<A HREF="examples7/test7.1.1.cc">download</A>]</P>
<PRE>
<!-- START CODE examples7/test7.1.1.cc -->
#include &quot;sys.h&quot;
#include &quot;debug.h&quot;

int main(void)
{
  Debug( libcw_do.on() );
  Debug( dc::malloc.on() );

  int* first = new int;
  AllocTag2(first, &quot;first&quot;);

  int* second = new int;
  AllocTag2(second, &quot;second&quot;);

  Debug( list_allocations_on(libcw_do) );

  Debug( <SPAN class="highlight">make_invisible(</SPAN>first<SPAN class="highlight">)</SPAN> );

  Debug( list_allocations_on(libcw_do) );

  delete second;
  delete first;

  return 0;
}
<!-- END CODE examples7/test7.1.1.cc -->
</PRE>

<P>The output of this program is</P>

<PRE class="output">
<!-- START OUTPUT examples7/test7.1.1-bin -->
MALLOC  : operator new (size = 4) = 0x6b7ec0 [test7.1.1.cc:9]
MALLOC  : operator new (size = 4) = 0x7f56e0 [test7.1.1.cc:12]
MALLOC  : Allocated memory: 8 bytes in 2 blocks.
          0x7f56e0         test7.1.1.cc:12   int; (sz = 4)  second
          0x6b7ec0         test7.1.1.cc:9    int; (sz = 4)  first
MALLOC  : Allocated memory: 4 bytes in 1 blocks.
          0x7f56e0         test7.1.1.cc:12   int; (sz = 4)  second
MALLOC  : delete 0x7f56e0         test7.1.1.cc:12   int; (sz = 4)  second 
</PRE>

<P>As you can see, the first allocation at line 9 disappeared from the
overview after it was made invisible.</P>

<P>Pointer validation at de-allocation is still performed however.&nbsp;
For instance, when we make a mistake when freeing the first <CODE>int</CODE>:</P>

<P>Compile as: <CODE>g++ -g -DCWDEBUG test7.1.2.cc -lcwd -o coredump</CODE></P>
<P class="download">[<A HREF="examples7/test7.1.2.cc">download</A>]</P>
<PRE>
<!-- START CODE examples7/test7.1.2.cc -->
#include &quot;sys.h&quot;
#include &quot;debug.h&quot;

int main(void)
{
  Debug( libcw_do.on() );
  Debug( dc::malloc.on() );

  int* first = new int;
  AllocTag2(first, &quot;first&quot;);

  int* second = new int;
  AllocTag2(second, &quot;second&quot;);

  Debug( list_allocations_on(libcw_do) );

  Debug( make_invisible(first) );

  Debug( list_allocations_on(libcw_do) );

  delete second;
  delete <SPAN class="highlight">[]</SPAN> first;	// Make a deliberate error

  return 0;
}
<!-- END CODE examples7/test7.1.2.cc -->
</PRE>

<P>then the output becomes</P>

<PRE class="output">
<!-- START OUTPUT examples7/test7.1.2-bin -->
MALLOC  : operator new (size = 4) = 0x6e6410 [test7.1.2.cc:9]
MALLOC  : operator new (size = 4) = 0x7f5780 [test7.1.2.cc:12]
MALLOC  : Allocated memory: 8 bytes in 2 blocks.
          0x7f5780         test7.1.2.cc:12   int; (sz = 4)  second
          0x6e6410         test7.1.2.cc:9    int; (sz = 4)  first
MALLOC  : Allocated memory: 4 bytes in 1 blocks.
          0x7f5780         test7.1.2.cc:12   int; (sz = 4)  second
MALLOC  : delete 0x7f5780         test7.1.2.cc:12   int; (sz = 4)  second 
COREDUMP: You are `delete[]'-ing a block that was allocated with `new'!  Use `delete' instead.
</PRE>

<P>Also the function <CODE>test_delete()</CODE> still works:</P>

<P>Compile as: <CODE>g++ -g -DCWDEBUG test7.1.3.cc -lcwd -o test_delete</CODE></P>
<P class="download">[<A HREF="examples7/test7.1.3.cc">download</A>]</P>
<PRE>
<!-- START CODE examples7/test7.1.3.cc -->
#include &quot;sys.h&quot;
#include &quot;debug.h&quot;

int main(void)
{
  Debug( libcw_do.on() );
  Debug( dc::malloc.on() );
  Debug( dc::notice.on() );

  void* p = malloc(3000);
  
  Debug( make_invisible(p) );
  Debug( list_allocations_on(libcw_do) );

  Dout(dc::notice, &quot;test_delete(&quot; &lt;&lt; p &lt;&lt; &quot;) = &quot; &lt;&lt; <SPAN class="highlight">test_delete(</SPAN>p<SPAN class="highlight">)</SPAN>);
  free(p);
  Dout(dc::notice, &quot;test_delete(&quot; &lt;&lt; p &lt;&lt; &quot;) = &quot; &lt;&lt; <SPAN class="highlight">test_delete(</SPAN>p<SPAN class="highlight">)</SPAN>);

  return 0;
}
<!-- END CODE examples7/test7.1.3.cc -->
</PRE>

<P>results in</P>

<PRE class="output">
<!-- START OUTPUT examples7/test7.1.3-bin -->
MALLOC  : malloc(3000) = 0x7f79a0 [test7.1.3.cc:10]
MALLOC  : Allocated memory: 0 bytes in 0 blocks.
NOTICE  : test_delete(0x7f79a0) = 0
NOTICE  : test_delete(0x7f79a0) = 1
</PRE>

<P>However, <CODE>find_alloc()</CODE>, the function that is explained in the next paragraph,
will <EM>fail</EM> to find an &laquo;invisible&raquo; block (it will return <CODE>NULL</CODE>).</P>

<A NAME="Retrieving"></A>
<H3>7.2 Retrieving information about memory allocations</H3>

<P>Libcwd allows the developer to generate powerful debugging output; aside from being able to test
if a given pointer points to the start of an allocated memory block, using <CODE>test_delete()</CODE>,
it is even possible to find all information about an allocated memory block that is also shown in the Allocated memory Overview
when given a pointer pointing anywhere <EM>inside</EM> an allocated memory block.&nbsp; For example:</P>

<P>Compile as: <CODE>g++ -g -DCWDEBUG test7.2.1.cc -lcwd -o find_alloc</CODE></P>
<P class="download">[<A HREF="examples7/test7.2.1.cc">download</A>]</P>
<PRE>
<!-- START CODE examples7/test7.2.1.cc -->
#include &quot;sys.h&quot;
#include &quot;debug.h&quot;

using namespace libcwd;

template&lt;typename T1, typename T2&gt;
  struct Foo {
    T1 for_me;
    T2 for_you;
    double d;
  };

int main(void)
{
  Debug( libcw_do.on() );
  Debug( dc::malloc.on() );
  Debug( dc::notice.on() );

  Foo&lt;char, int&gt;* f = new Foo&lt;char, int&gt;;
  AllocTag(f, &quot;Our test object&quot;);

  int* p = &amp;f-&gt;for_you;	// Pointer that points inside `f'

  Dout(dc::notice, &quot;f == &quot; &lt;&lt; static_cast&lt;void*&gt;(f));
  Dout(dc::notice, &quot;p == &quot; &lt;&lt; static_cast&lt;void*&gt;(p));

#ifdef CWDEBUG
  alloc_ct const* alloc = <SPAN class="highlight">find_alloc(</SPAN>p<SPAN class="highlight">)</SPAN>;
  Dout(dc::notice,
	 &quot;p points inside \&quot;&quot;
      &lt;&lt; <SPAN class="highlight">alloc-&gt;description()</SPAN>
      &lt;&lt; &quot;\&quot; starting at &quot;
      &lt;&lt; <SPAN class="highlight">alloc-&gt;start()</SPAN>
      &lt;&lt; &quot; with size &quot;
      &lt;&lt; <SPAN class="highlight">alloc-&gt;size()</SPAN>
      &lt;&lt; '.');
  Dout(dc::notice,
	 &quot;This memory block contains a \&quot;&quot;
      &lt;&lt; <SPAN class="highlight">alloc-&gt;type_info().demangled_name()</SPAN> &lt;&lt; &quot;\&quot;.&quot;);
  Dout(dc::notice,
	 &quot;The allocation type is `&quot;
      &lt;&lt; <SPAN class="highlight">alloc-&gt;memblk_type()</SPAN>
      &lt;&lt; &quot;' and was allocated at &quot;
      &lt;&lt; <SPAN class="highlight">alloc-&gt;location()</SPAN>
      &lt;&lt; '.');
#endif

  return 0;
}
<!-- END CODE examples7/test7.2.1.cc -->
</PRE>

<P>The output of this program is</P>

<PRE class="output">
<!-- START OUTPUT examples7/test7.2.1-bin -->
MALLOC  : operator new (size = 16) = 0x634540 [test7.2.1.cc:19]
NOTICE  : f == 0x634540
NOTICE  : p == 0x634544
NOTICE  : p points inside &quot;Our test object&quot; starting at 0x634540 with size 16.
NOTICE  : This memory block contains a &quot;Foo&lt;char, int&gt;*&quot;.
NOTICE  : The allocation type is `memblk_type_new' and was allocated at test7.2.1.cc:19.
</PRE>

<P>Note that the <EM>type</EM> returned by <CODE>alloc-&gt;type_info().demangled_name()</CODE> is
the type of the pointer passed to <CODE>AllocTag()</CODE>:
This string will always end on a <CODE>'*'</CODE>.&nbsp;</P>

<P>The original reason for supporting pointers that point <EM>inside</EM> a memory block
and not just to the start of it, was so that it could be used in base classes of objects
derived with multiple inheritance: <CODE>find_alloc(this)</CODE> will
always return the correct memory block, even if there is an offset between <CODE>this</CODE>
and the real start of the allocated block.&nbsp;
The same holds for arrays of objects allocated with <CODE>new[]</CODE>.</P>

<P>It is also possible to get the values for the number of bytes and blocks allocated
in total, as is printed at the top of each Allocated memory Overview.&nbsp;
See the next paragraph.</P>

<A NAME="Memory"></A>
<H3>7.3 Memory leak detection</H3>

<P><CODE>mem_blocks()</CODE> and <CODE>mem_size()</CODE>
(like everything else defined in namespace <CODE>libcwd</CODE>)
can be used to write a very simply memory leak detection system:</P>

<P>Compile as: <CODE>g++ -g -DCWDEBUG test7.3.1.cc -lcwd -o total_alloc</CODE></P>
<P class="download">[<A HREF="examples7/test7.3.1.cc">download</A>]</P>
<PRE>
<!-- START CODE examples7/test7.3.1.cc -->
#include &quot;sys.h&quot;
#include &quot;debug.h&quot;

using namespace libcwd;

int main(void)
{
  Debug( libcw_do.on() );
  Debug( dc::malloc.on() );

  char* memory_leak = new char [300];
  AllocTag(memory_leak, "memory_leak");

  // Debug( make_invisible(memory_leak) );

#ifdef CWDEBUG
  if (<SPAN class="highlight">mem_blocks()</SPAN> &gt; 0)
    Dout(dc::warning, &quot;There are still &quot; &lt;&lt; <SPAN class="highlight">mem_size()</SPAN> &lt;&lt; &quot; bytes allocated!&quot;);
  else
    Dout(dc::malloc, &quot;No memory leaks.&quot;);
#endif

  return 0;
}
<!-- END CODE examples7/test7.3.1.cc -->
</PRE>

<P>The output of this program is:</P>

<PRE class="output">
<!-- START OUTPUT examples7/test7.3.1-bin -->
MALLOC  : operator new[] (size = 300) = 0x62bdc0 [test7.3.1.cc:11]
WARNING : There are still 300 bytes allocated!
</PRE>

<P>Invisible blocks are <B>not</B> detected!&nbsp;
When you comment out the <CODE>Debug( make_invisible(memory_leak) );</CODE> then
the output will read</P>

<PRE class="output">
MALLOC  : operator new[] (size = 300) = 0x804fc88
MALLOC  : No memory leaks.
</PRE>

<P>This allows you to improve the memory leak detection a bit in the case
of global objects that allocate memory.&nbsp; Nevertheless, that is bad
coding: you shouldn't define global objects that allocate memory.</P>

<P>Here is an example program anyway:</P>

<P>Compile as: <CODE>g++ -g -DCWDEBUG test7.3.2.cc -lcwd -o memleak</CODE></P>
<P class="download">[<A HREF="examples7/test7.3.2.cc">download</A>]</P>
<PRE>
<!-- START CODE examples7/test7.3.2.cc -->
#include &quot;sys.h&quot;
#include &quot;debug.h&quot;

class A {
private:
  char* dynamic_memory;
public:
  A(void)
  {
    dynamic_memory = new char [300];
    AllocTag(dynamic_memory, "A::dynamic_memory");
  }
  ~A()
  {
    if (dynamic_memory)
      delete [] dynamic_memory;
  }
};

// Global object that allocates memory (bad programming!)
A a;

int main(void)
{
  Debug( libcw_do.on() );
  Debug( dc::malloc.on() );

#ifdef CWDEBUG
  if (libcwd::mem_blocks() &gt; 0)
  {
    Dout(dc::malloc|dc::warning, &quot;Memory leak&quot;);
    Debug( list_allocations_on(libcw_do) );
  }
  else
    Dout(dc::malloc, &quot;No memory leaks.&quot;);
#endif

  return 0;
}
<!-- END CODE examples7/test7.3.2.cc -->
</PRE>

<P>results in</P>

<PRE class="output">
<!-- START OUTPUT examples7/test7.3.2-bin -->
MALLOC  : Memory leak
MALLOC  : Allocated memory: 300 bytes in 1 blocks.
new[]     0x7bc420         test7.3.2.cc:10   char[300]; (sz = 300)  A::dynamic_memory
MALLOC  : delete[] 0x7bc420         test7.3.2.cc:10   char[300]; (sz = 300)  A::dynamic_memory 
</PRE>

<P>simply because <CODE>A::dynamic_memory</CODE> is not deleted until <EM>after</EM> <CODE>main</CODE>.</P>

<P>A simple kludge is to make all memory allocated <EM>before</EM> <CODE>main</CODE>, invisible:</P>

<P>Compile as: <CODE>g++ -g -DCWDEBUG test7.3.3.cc -lcwd -o memleak2</CODE></P>
<P class="download">[<A HREF="examples7/test7.3.3.cc">download</A>]</P>
<PRE>
<!-- START CODE examples7/test7.3.3.cc -->
#include &quot;sys.h&quot;
#include &quot;debug.h&quot;

class A {
private:
  char* dynamic_memory;
public:
  A(void)
  {
    dynamic_memory = new char [300];
    AllocTag(dynamic_memory, "A::dynamic_memory");
  }
  ~A()
  {
    if (dynamic_memory)
      delete [] dynamic_memory;
  }
};

// Global object that allocates memory (bad programming!)
A a;

int main(void)
{
  <SPAN class="highlight">Debug( make_all_allocations_invisible_except(NULL) );</SPAN>

  Debug( libcw_do.on() );
  Debug( dc::malloc.on() );

#ifdef CWDEBUG
  if (libcwd::mem_blocks() &gt; 0)
  {
    Dout(dc::malloc|dc::warning, &quot;Memory leak&quot;);
    Debug( list_allocations_on(libcw_do) );
  }
  else
    Dout(dc::malloc, &quot;No memory leaks.&quot;);
#endif

  return 0;
}
<!-- END CODE examples7/test7.3.3.cc -->
</PRE>

<P>which will simply output</P>

<PRE class="output">
<!-- START OUTPUT examples7/test7.3.3-bin -->
MALLOC  : No memory leaks.
</PRE>

<P>Libcwd provides an alternative way to check for memory leaks using so called &laquo;markers&raquo;.&nbsp;
A marker is like a directory, any allocation made after a marker is created is put into that directory.&nbsp;
When a marker is removed and there are still allocation inside it, you will get a warning!&nbsp;
In the following example we allocate a memory block, then set a marker and next allocate two more memory
blocks.&nbsp The Allocated memory Overview is then printed.</P>

<P>Compile as: <CODE>g++ -g -DCWDEBUG test7.3.4.cc -lcwd -o marker</CODE></P>
<P class="download">[<A HREF="examples7/test7.3.4.cc">download</A>]</P>
<PRE>
<!-- START CODE examples7/test7.3.4.cc -->
#include &quot;sys.h&quot;
#include &quot;debug.h&quot;

int main(void)
{
  Debug( make_all_allocations_invisible_except(NULL) );
  Debug( libcw_do.on() );
  Debug( dc::malloc.on() );

  int* p1 = new int [10];
  AllocTag(p1, &quot;p1&quot;);

<SPAN class="highlight">#if CWDEBUG_MARKER</SPAN>
  <SPAN class="highlight">libcwd::marker_ct* marker = new libcwd::marker_ct(&quot;A test marker&quot;);</SPAN>
<SPAN class="highlight">#endif</SPAN>

  int* p2 = new int [20];
  AllocTag(p2, &quot;p2&quot;);

  int* p3 = new int [30];
  AllocTag(p3, &quot;p3&quot;);

  Debug( list_allocations_on(libcw_do) );

<SPAN class="highlight">#if CWDEBUG_MARKER</SPAN>
  // Delete the marker while there are still allocations inside it
  <SPAN class="highlight">delete marker;</SPAN>
<SPAN class="highlight">#endif</SPAN>

  return 0;
}
<!-- END CODE examples7/test7.3.4.cc -->
</PRE>

<P>The output of this program is:</P>

<PRE class="output">
<!-- START OUTPUT examples7/test7.3.4-bin -->
MALLOC  : operator new[] (size = 40) = 0x615300 [test7.3.4.cc:10]
MALLOC  : operator new (size = 16) = 0x62bd20 [test7.3.4.cc:14]
MALLOC  : New libcwd::marker_ct at 0x62bd20
MALLOC  : operator new[] (size = 80) = 0x80e3f0 [test7.3.4.cc:17]
MALLOC  : operator new[] (size = 120) = 0x80e580 [test7.3.4.cc:20]
MALLOC  : Allocated memory: 256 bytes in 4 blocks.
(MARKER)  0x62bd20         test7.3.4.cc:14   &lt;marker&gt;; (sz = 16)  A test marker
    new[]     0x80e580         test7.3.4.cc:20   int[30]; (sz = 120)  p3
    new[]     0x80e3f0         test7.3.4.cc:17   int[20]; (sz = 80)  p2
new[]     0x615300         test7.3.4.cc:10   int[10]; (sz = 40)  p1
MALLOC  : Removing libcwd::marker_ct at 0x62bd20 (A test marker)
  * WARNING : Memory leak detected!
  * new[]     0x80e580         test7.3.4.cc:20   int[30]; (sz = 120)  p3
  * new[]     0x80e3f0         test7.3.4.cc:17   int[20]; (sz = 80)  p2
MALLOC  : delete 0x62bd20         test7.3.4.cc:14   &lt;marker&gt;; (sz = 16)  A test marker 
</PRE>

<P>Individual allocations (or other markers, inclusive everything they contain)
can be moved outside a marker with the function <CODE>move_outside()</CODE>.&nbsp;
For example, we could move the allocation of <CODE>p2</CODE> outside our marker:</P>

<P>Compile as: <CODE>g++ -g -DCWDEBUG test7.3.5.cc -lcwd -o marker2</CODE></P>
<P class="download">[<A HREF="examples7/test7.3.5.cc">download</A>]</P>
<PRE>
<!-- START CODE examples7/test7.3.5.cc -->
#include &quot;sys.h&quot;
#include &quot;debug.h&quot;

int main(void)
{
  Debug( make_all_allocations_invisible_except(NULL) );
  Debug( libcw_do.on() );
  Debug( dc::malloc.on() );

  int* p1 = new int [10];
  AllocTag(p1, &quot;p1&quot;);

#if CWDEBUG_MARKER
  libcwd::marker_ct* marker = new libcwd::marker_ct(&quot;A test marker&quot;);
#endif

  int* p2 = new int [20];
  AllocTag(p2, &quot;p2&quot;);

  int* p3 = new int [30];
  AllocTag(p3, &quot;p3&quot;);

<SPAN class="highlight">#if CWDEBUG_MARKER</SPAN>
  <SPAN class="highlight">Debug( move_outside(marker, p2) );</SPAN>
<SPAN class="highlight">#endif</SPAN>

  Debug( list_allocations_on(libcw_do) );

#if CWDEBUG_MARKER
  // Delete the marker while there are still allocations inside it
  delete marker;
#endif

  return 0;
}
<!-- END CODE examples7/test7.3.5.cc -->
</PRE>

<P>which results in the output</P>

<PRE class="output">
<!-- START OUTPUT examples7/test7.3.5-bin -->
MALLOC  : operator new[] (size = 40) = 0x615300 [test7.3.5.cc:10]
MALLOC  : operator new (size = 16) = 0x62bdb0 [test7.3.5.cc:14]
MALLOC  : New libcwd::marker_ct at 0x62bdb0
MALLOC  : operator new[] (size = 80) = 0x80e490 [test7.3.5.cc:17]
MALLOC  : operator new[] (size = 120) = 0x80e620 [test7.3.5.cc:20]
MALLOC  : Allocated memory: 256 bytes in 4 blocks.
new[]     0x80e490         test7.3.5.cc:17   int[20]; (sz = 80)  p2
(MARKER)  0x62bdb0         test7.3.5.cc:14   &lt;marker&gt;; (sz = 16)  A test marker
    new[]     0x80e620         test7.3.5.cc:20   int[30]; (sz = 120)  p3
new[]     0x615300         test7.3.5.cc:10   int[10]; (sz = 40)  p1
MALLOC  : Removing libcwd::marker_ct at 0x62bdb0 (A test marker)
  * WARNING : Memory leak detected!
  * new[]     0x80e620         test7.3.5.cc:20   int[30]; (sz = 120)  p3
MALLOC  : delete 0x62bdb0         test7.3.5.cc:14   &lt;marker&gt;; (sz = 16)  A test marker 
</PRE>


</DIV>
<P class="line"><IMG width=870 height=19 src="../images/lines/hippo.png"></P>
<DIV class="buttons">
<A HREF="tut6.html"><IMG width=64 height=32 src="../images/buttons/lr_prev.png" border="0"></A>
<A HREF="index.html"><IMG width=64 height=32 src="../images/buttons/lr_index.png" border="0"></A>
<A HREF="tut8.html"><IMG width=64 height=32 src="../images/buttons/lr_next.png" border="0"></A>
</DIV>

<ADDRESS>Copyright &copy; 2001, 2002 Carlo Wood.&nbsp; All rights reserved.</ADDRESS>

</BODY>
</HTML>