<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>NeoEGM.com &#187; Memory Leaks</title>
	<atom:link href="https://www.neoegm.com/tag/memory-leaks/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.neoegm.com</link>
	<description>Knowledge is inside</description>
	<lastBuildDate>Mon, 08 Jul 2024 05:38:01 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.2.39</generator>
	<item>
		<title>Simple C++ List Class</title>
		<link>https://www.neoegm.com/tech/programming/c-cpp/simple-list-class/</link>
		<comments>https://www.neoegm.com/tech/programming/c-cpp/simple-list-class/#comments</comments>
		<pubDate>Tue, 18 Aug 2009 20:55:36 +0000</pubDate>
		<dc:creator><![CDATA[NeoEGM]]></dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[Class]]></category>
		<category><![CDATA[Console]]></category>
		<category><![CDATA[Download]]></category>
		<category><![CDATA[Easy]]></category>
		<category><![CDATA[Exercise]]></category>
		<category><![CDATA[Freeware]]></category>
		<category><![CDATA[GNU GPL]]></category>
		<category><![CDATA[Include]]></category>
		<category><![CDATA[Library]]></category>
		<category><![CDATA[List]]></category>
		<category><![CDATA[Memory Leaks]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[Portable]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Source Code]]></category>
		<category><![CDATA[Teaching]]></category>
		<category><![CDATA[wxDev]]></category>

		<guid isPermaLink="false">http://www.neoegm.com/?p=976</guid>
		<description><![CDATA[Just as I published some days ago the Simple C++ String Class as a C++ learning exercise, now I am freeing a Simple C++ List Class. The standard library has a list class. But, while learning, it&#8217;s a good idea to know how to develop your own list class. That&#8217;s why I made the List [&#8230;]<div class='yarpp-related-rss'>
<strong>
Related posts:<ol>
<li><a href="https://www.neoegm.com/tech/programming/c-cpp/simple-string-class/" rel="bookmark" title="Simple C++ String Class">Simple C++ String Class </a></li>
<li><a href="https://www.neoegm.com/tech/programming/c-cpp/cppmemdbg-easy-to-use-cpp-memory-leak-detection-library/" rel="bookmark" title="cppMemDbg &#8211; Easy to use C++ memory leak detection library">cppMemDbg &#8211; Easy to use C++ memory leak detection library </a></li>
<li><a href="https://www.neoegm.com/tech/programming/c-cpp/control-de-asistencia/" rel="bookmark" title="Attendance Control">Attendance Control </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<p>Just as I published some days ago the <a href="http://www.neoegm.com/tech/programming/c-cpp/simple-string-class/">Simple C++ String Class</a> as a C++ learning exercise, now I am freeing a <strong>Simple C++ List Class</strong>.</p>
<p>The standard library has a <em>list</em> class. But, while learning, it&#8217;s a good idea to know how to develop your own <em>list</em> class.</p>
<p>That&#8217;s why I made the <em>List</em> class. It&#8217;s not intended for professional projects (for them, you should use the standard library&#8217;s <em>list</em>), but as help to learn C++.</p>
<pre class="brush: cpp; title: ; notranslate">
template &lt;class TYPE&gt;
class List
{
	/* ... */

public:
	//Construction and destruction
	List() { /* ... */ }
	~List() { /* ... */ }
	
	List(const List&amp; rlList) { /* ... */ }
	
	//Assignment operator
	List&amp; operator=(const List&amp; rlList);

	//Information
	int Length() { /* ... */ }
	bool Empty() { /* ... */ }

	//Element managing
	int Add(TYPE&amp; rtData);
	TYPE* Elem(int nPos);
	bool Delete(int nPos);
	void DeleteAll();
	
	//Search
	int Find(TYPE&amp; rItem, int nStartAt = 0);

	//Operadores
	TYPE&amp; operator[](int nPos) { /* ... */ }	//Elem
	int operator&lt;&lt;(TYPE&amp; rdData) { /* ... */ }	//Add

protected:
	void FreeList();
	void Init() { /* ... */ }
};

//Output
template &lt;class TYPE&gt;
std::ostream&amp; operator&lt;&lt;(std::ostream&amp; oStream, List&lt;TYPE&gt;&amp; rlList);
</pre>
<p><span id="more-976"></span></p>
<p>This is a sample project made to explain the <em>List</em> class usage.</p>
<pre class="brush: cpp; title: ; notranslate">
#include &lt;iostream&gt;
	using std::cout;
	using std::cin;
	using std::endl;

#include &quot;List.h&quot;
#include &quot;String.h&quot;

int main(int argc, char *argv[])
{
	cout &lt;&lt; &quot;List sample project&quot; &lt;&lt; endl;
	cout &lt;&lt; &quot;-------------------&quot; &lt;&lt; endl &lt;&lt; endl;


	//-----------------------------
	cout &lt;&lt; &quot;&gt; Creating a list of strings... An empty string finishes the list...&quot; &lt;&lt; endl &lt;&lt; endl;

	List&lt;String&gt; lStrings;
	String sTmp;
	
	do
	{
		cout &lt;&lt; &quot;&gt; String &quot; &lt;&lt; lStrings.Length()+1 &lt;&lt; &quot;: &quot;;
		cin &gt;&gt; sTmp;
		
		if (sTmp.Length())
			lStrings.Add(sTmp);
	} while (sTmp.Length());
	
	cout &lt;&lt; endl &lt;&lt; &quot;&gt; Entry finished. &quot; &lt;&lt; lStrings.Length() &lt;&lt; &quot; string(s) loaded.&quot; &lt;&lt; endl &lt;&lt; endl;
	
	cout &lt;&lt; &quot;&gt; Printing list...&quot; &lt;&lt; endl &lt;&lt; endl;
	
	cout &lt;&lt; lStrings;

	cout &lt;&lt; endl;
	//-----------------------------
	
	
	//-----------------------------
	do
	{
		cout &lt;&lt; &quot;&gt; Type a string to find in the list (exact match) [empty = end]: &quot;;
		cin &gt;&gt; sTmp;

		if (sTmp.Length())
		{
			int nFound = lStrings.Find(sTmp);
			
			if (nFound != -1)
			{
				lStrings.Delete(nFound);

				cout &lt;&lt; &quot;&gt; String \&quot;&quot; &lt;&lt; sTmp &lt;&lt; &quot;\&quot; found at position &quot; &lt;&lt; nFound+1 &lt;&lt; &quot; and removed.&quot; &lt;&lt; endl &lt;&lt; endl;
				
				cout &lt;&lt; &quot;&gt; Printing list...&quot; &lt;&lt; endl &lt;&lt; endl;

				if (!lStrings.Empty())
					cout &lt;&lt; lStrings;
				else
				{
					cout &lt;&lt; &quot;[Empty list]&quot; &lt;&lt; endl &lt;&lt; endl;
					break;
				}
			}
			else
				cout &lt;&lt; &quot;&gt; String \&quot;&quot; &lt;&lt; sTmp &lt;&lt; &quot;\&quot; not found.&quot; &lt;&lt; endl;
		}
		
		cout &lt;&lt; endl;
	} while (sTmp.Length());
	
	cout &lt;&lt; endl;
	//-----------------------------

	//-----------------------------
	cout &lt;&lt; &quot;&gt; Now getting a bit more complex... Let's create a list of lists of strings... An empty string finishes the list and an empty first string finishes the list of lists...&quot; &lt;&lt; endl &lt;&lt; endl;

	List&lt; List&lt;String&gt; &gt; lLists;
	List&lt;String&gt; lTmpList;
	int nStrings = 0;

	do
	{
		lTmpList.DeleteAll();
		
		cout &lt;&lt; &quot;&gt; Loading list &quot; &lt;&lt; lLists.Length()+1 &lt;&lt; &quot;...&quot; &lt;&lt; endl;
		
		do
		{
			cout &lt;&lt; &quot;\t&gt; String &quot; &lt;&lt; lTmpList.Length()+1 &lt;&lt; &quot;: &quot;;
			cin &gt;&gt; sTmp;
			
			if (sTmp.Length())
			{
				lTmpList.Add(sTmp);
				nStrings++;
			}
		} while (sTmp.Length());
		
		if (lTmpList.Length())
			lLists.Add(lTmpList);
	} while (lTmpList.Length());
	
	cout &lt;&lt; endl &lt;&lt; &quot;&gt; Entry finished. &quot; &lt;&lt; lLists.Length() &lt;&lt; &quot; list(s) loaded, &quot; &lt;&lt; nStrings &lt;&lt; &quot; string(s) loaded.&quot; &lt;&lt; endl &lt;&lt; endl &lt;&lt; endl;

	cout &lt;&lt; &quot;&gt; Printing lists (standard method)...&quot; &lt;&lt; endl &lt;&lt; endl;

	cout &lt;&lt; lLists;

	cout &lt;&lt; endl;
	
	cout &lt;&lt; &quot;&gt; Printing lists (custom method)...&quot; &lt;&lt; endl &lt;&lt; endl;
	
	for (int i = 0; i &lt; lLists.Length(); i++)
	{
		List&lt;String&gt; *plList = lLists.Elem(i);

		if (plList)
		{
			cout &lt;&lt; &quot;- List &quot; &lt;&lt; i &lt;&lt; endl &lt;&lt; endl;
			cout &lt;&lt; *plList;
			cout &lt;&lt; endl;
		}
	}
	//-----------------------------

    return 0;
}
</pre>
<p>And this is its output:</p>

<pre class="console">
List sample project
-------------------

> Creating a list of strings... An empty string finishes the list...

> String 1: Test 1
> String 2: Test 2
> String 3: Test 3, a little bit longer
> String 4:

> Entry finished. 3 string(s) loaded.

> Printing list...

Test 1
Test 2
Test 3, a little bit longer

> Type a string to find in the list (exact match) [empty = end]: Hello
> String "Hello" not found.

> Type a string to find in the list (exact match) [empty = end]: Test 1
> String "Test 1" found at position 1 and removed.

> Printing list...

Test 2
Test 3, a little bit longer

> Type a string to find in the list (exact match) [empty = end]: Test 1
> String "Test 1" not found.

> Type a string to find in the list (exact match) [empty = end]: test 2
> String "test 2" not found.

> Type a string to find in the list (exact match) [empty = end]: Test 2
> String "Test 2" found at position 1 and removed.

> Printing list...

Test 3, a little bit longer

> Type a string to find in the list (exact match) [empty = end]: Test 3, a little bit longer
> String "Test 3, a little bit longer" found at position 1 and removed.

> Printing list...

[Empty list]


> Now getting a bit more complex... Let's create a list of lists of strings... An empty string finishes the list and an empty first string finishes the list of lists...

> Loading list 1...
        > String 1: Test 1a
        > String 2: Test 1b
        > String 3: Test 1c
        > String 4:
> Loading list 2...
        > String 1: Test 2a
        > String 2: Test 2b
        > String 3: Test 2c
        > String 4: Test 2d
        > String 5:
> Loading list 3...
        > String 1: Test 3a
        > String 2: Test 3b
        > String 3: This is a looooooooooooooooooooooong string
        > String 4:
> Loading list 4...
        > String 1:

> Entry finished. 3 list(s) loaded, 10 string(s) loaded.


> Printing lists (standard method)...

Test 1a
Test 1b
Test 1c

Test 2a
Test 2b
Test 2c
Test 2d

Test 3a
Test 3b
This is a looooooooooooooooooooooong string


> Printing lists (custom method)...

- List 0

Test 1a
Test 1b
Test 1c

- List 1

Test 2a
Test 2b
Test 2c
Test 2d

- List 2

Test 3a
Test 3b
This is a looooooooooooooooooooooong string

</pre>
<p>This project also uses the <a href="http://www.neoegm.com/tech/programming/c-cpp/simple-string-class/">Simple C++ String Class</a> to show the List class working with custom classes. Exactly the same code could be without problem by simply replacing &#8220;String&#8221; with &#8220;string&#8221; and including the standard library&#8217;s <em>string</em> header.</p>
<p>The code completely is portable.</p>
<p>It&#8217;s been developed, compiled and tested using <a href="http://wxdsgn.sourceforge.net/">wxDev-C++</a> for Windows with the <a href="http://www.mingw.org/">MinGW compiler</a> (included in the bundle).</p>
<p><a href="http://www.gnu.org/licenses/gpl-3.0.txt"><img src="http://www.neoegm.com/wp-content/uploads/2009/07/gplv3-127x511.png" alt="GNU GPL v3" title="GNU GPL v3" width="127" height="51" class="aligncenter size-full wp-image-251" /></a> <span class="aligncenter">List is licensed under the <a href="http://www.gnu.org/licenses/gpl-3.0.txt">GNU GPL v3</a> (attached)&#8230;</span></p>
<p><br/><br />
I&#8217;ve also tested the project for memory leaks using the <a href="http://www.neoegm.com/tech/programming/c-cpp/cppmemdbg-easy-to-use-cpp-memory-leak-detection-library/">cppMemDbg – Easy to use C++ memory leak detection library</a> and it found no problems at all&#8230;</p>
<p>You can download the library output and the cppMemDbg adapted project here:</p>
<div align="center">
<p class="download"><a href="http://download.neoegm.com/software/list-class/List_Sample_Project_cppMemDbg_Output.txt">Download cppMemDbg Output</a></p>
</div>
<div align="center">
<p class="download"><a href="http://download.neoegm.com/software/list-class/List_Sample_Project_1.0_cppMemDbg.zip">Download Adapted Project 1.0</a></p>
</div>
<p><br/><br />
Now, finally, the download links:</p>
<div align="center">
<p class="download"><a href="http://download.neoegm.com/software/list-class/List_1.0.zip">Download List Class 1.0</a></p>
</div>
<div align="center">
<p class="download"><a href="http://download.neoegm.com/software/list-class/List_Sample_Project_1.0.zip">Download List Sample Project 1.0</a></p>
</div>
<h4>Incoming search terms for the article:</h4>
<ul>
<li><a href="https://www.neoegm.com/tech/programming/c-cpp/simple-list-class/" title="c list class">c list class</a></li>
<li><a href="https://www.neoegm.com/tech/programming/c-cpp/simple-list-class/" title="list class c">list class c</a></li>
<li><a href="https://www.neoegm.com/tech/programming/c-cpp/simple-list-class/" title="class list c">class list c</a></li>
<li><a href="https://www.neoegm.com/tech/programming/c-cpp/simple-list-class/" title="lista simple c">lista simple c</a></li>
<li><a href="https://www.neoegm.com/tech/programming/c-cpp/simple-list-class/" title="clase lista c">clase lista c</a></li>
<li><a href="https://www.neoegm.com/tech/programming/c-cpp/simple-list-class/" title="LISTA SIMPLE EN C">LISTA SIMPLE EN C</a></li>
<li><a href="https://www.neoegm.com/tech/programming/c-cpp/simple-list-class/" title="listas simples en c">listas simples en c</a></li>
<li><a href="https://www.neoegm.com/tech/programming/c-cpp/simple-list-class/" title="clase lista en c">clase lista en c</a></li>
<li><a href="https://www.neoegm.com/tech/programming/c-cpp/simple-list-class/" title="c class list">c class list</a></li>
<li><a href="https://www.neoegm.com/tech/programming/c-cpp/simple-list-class/" title="list class in c">list class in c</a></li>
</ul>
<div class='yarpp-related-rss'>
<strong><p>Related posts:<ol>
<li><a href="https://www.neoegm.com/tech/programming/c-cpp/simple-string-class/" rel="bookmark" title="Simple C++ String Class">Simple C++ String Class </a></li>
<li><a href="https://www.neoegm.com/tech/programming/c-cpp/cppmemdbg-easy-to-use-cpp-memory-leak-detection-library/" rel="bookmark" title="cppMemDbg &#8211; Easy to use C++ memory leak detection library">cppMemDbg &#8211; Easy to use C++ memory leak detection library </a></li>
<li><a href="https://www.neoegm.com/tech/programming/c-cpp/control-de-asistencia/" rel="bookmark" title="Attendance Control">Attendance Control </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>https://www.neoegm.com/tech/programming/c-cpp/simple-list-class/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>cppMemDbg &#8211; Easy to use C++ memory leak detection library</title>
		<link>https://www.neoegm.com/tech/programming/c-cpp/cppmemdbg-easy-to-use-cpp-memory-leak-detection-library/</link>
		<comments>https://www.neoegm.com/tech/programming/c-cpp/cppmemdbg-easy-to-use-cpp-memory-leak-detection-library/#comments</comments>
		<pubDate>Sat, 08 Aug 2009 11:51:13 +0000</pubDate>
		<dc:creator><![CDATA[NeoEGM]]></dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[Download]]></category>
		<category><![CDATA[Easy]]></category>
		<category><![CDATA[Freeware]]></category>
		<category><![CDATA[GNU GPL]]></category>
		<category><![CDATA[Include]]></category>
		<category><![CDATA[Library]]></category>
		<category><![CDATA[Memory Leaks]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Source Code]]></category>

		<guid isPermaLink="false">http://www.neoegm.com/?p=727</guid>
		<description><![CDATA[This library is the C++ sequel to the cMemDbg. Just as the cMemDbg, it is a very easy to use library which can help you to detect and track memory leaks. Its usage is very similar to the cMemDbg, but with support for the C++ operators (new, new[], delete and delete[]). There are lots of [&#8230;]<div class='yarpp-related-rss'>
<strong>
Related posts:<ol>
<li><a href="https://www.neoegm.com/tech/programming/c-cpp/cmemdbg-easy-to-use-c-memory-leak-detection-library/" rel="bookmark" title="cMemDbg &#8211; Easy to use C memory leak detection library">cMemDbg &#8211; Easy to use C memory leak detection library </a></li>
<li><a href="https://www.neoegm.com/tech/programming/c-cpp/simple-list-class/" rel="bookmark" title="Simple C++ List Class">Simple C++ List Class </a></li>
<li><a href="https://www.neoegm.com/tech/programming/c-cpp/simple-string-class/" rel="bookmark" title="Simple C++ String Class">Simple C++ String Class </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<p>This library is the C++ sequel to the <a href="http://www.neoegm.com/tech/programming/c-cpp/cmemdbg-easy-to-use-c-memory-leak-detection-library/">cMemDbg</a>.</p>
<p>Just as the cMemDbg, it is a very easy to use library which can help you to detect and track memory leaks.</p>
<p>Its usage is very similar to the cMemDbg, but with support for the C++ operators (new, new[], delete and delete[]).</p>
<p>There are lots of solutions for this on the net, but this one has the particularity of being really simple to implement.</p>

<pre class="console">>new  003D26D8  36  [Main.cpp:127]
>new  003D2708  36  [Main.cpp:128]
>ERROR  Bad free type  free => delete  003D2708  36
(Main.cpp:128)
>free  003D2708  36  (Main.cpp:128)  [Main.cpp:129]
>free  003D2708  0    [Main.cpp:130]
>ERROR  Trying to free unallocated memory: 003D2708
[Main.cpp:130]
>delete[]  003D3EB0  7  (String.cpp:59)  [String.h:41]
[...]
>delete[]  003D24F0  4  (String.cpp:59)  [String.h:41]
>delete  003D2490  40  (Lista.h:120)  [Lista.h:112]
>INFO  PROBLEM: Memory leak found (36 bytes)
>INFO  Unfreed block  003D26D8  36    [Main.cpp:127]
</pre>
<p><span id="more-727"></span>You just have to add the following include to your main include file (a file that gets included by each file of your project) or, in case you don&#8217;t have one, to each file that calls any memory allocation function (malloc, realloc, calloc, free, new, new[], delete or delete[]).</p>
<p>This is the line:</p>
<pre class="brush: cpp; light: true; title: ; notranslate">#include &quot;cMemDbg.h&quot;</pre>
<p>There are two extra cautions you have to take into account:<br />
1- Include it <strong>after</strong> the standard headers (stdio.h, stdlib.h, malloc.h, etc.).<br />
2- Never call the delete (or delete[]) operator without knowing if its argument is NULL. So: &#8220;delete a;&#8221; should become &#8220;if (a) delete a;&#8221;. Otherwise, the library may show incorrect messages.</p>
<p>That&#8217;s all&#8230; Expecting more? Well, there is a final step&#8230; You have to make this call just before you exit your program so you can get the detailed conclusions:</p>
<pre class="brush: cpp; light: true; title: ; notranslate">PrintMemoryLeakInfo();</pre>
<p>By default, the library output will go to stdout (normally the console screen). If you want to redirect it to a file, you can call the InitCPPMemDbg() function. This is its prototype:</p>
<pre class="brush: cpp; light: true; title: ; notranslate">void InitCPPMemDbg(const char *pszOutputPath = NULL);</pre>
<p>Other option, to simplify, is to create a global object of the &#8220;cppMemDbg&#8221; type at the beginning of the global declarations. You can pass a file path to the constructor to redirect the output. It will call InitCPPMemDbg (to setup the redirect) at construction and PrintMemoryLeakInfo() at destruction.</p>
<p>Simply like this:</p>
<pre class="brush: cpp; light: true; title: ; notranslate">cppMemDbg cDbg;</pre>
<p>Or, with redirection:</p>
<pre class="brush: cpp; light: true; title: ; notranslate">cppMemDbg cDbg(&quot;mylocalfile.txt&quot;);</pre>
<p>Easy, not?</p>
<p>That was all&#8230; Really.</p>
<p>The code completely is portable.</p>
<p><a href="http://www.gnu.org/licenses/gpl-3.0.txt"><img src="http://www.neoegm.com/wp-content/uploads/2009/07/gplv3-127x511.png" alt="GNU GPL v3" title="GNU GPL v3" width="127" height="51" class="aligncenter size-full wp-image-251" /></a> <span class="aligncenter">cppMemDbg is licensed under the <a href="http://www.gnu.org/licenses/gpl-3.0.txt">GNU GPL v3</a> (attached)&#8230;</span></p>
<p>Now, before going to more &#8220;expert&#8221; features, I&#8217;ll leave here the link to download the library for the ones who don&#8217;t wish to keep reading:</p>
<div align="center">
<p class="download"><a href="http://download.neoegm.com/software/cppmemdbg/cppMemDbg_1.0.zip">Download cppMemDbg 1.0</a></p>
</div>
<p>Now let&#8217;s continue&#8230;</p>
<p>The library comes with three configurable settings (available in cppMemDbg.cpp):</p>
<ul>
<li><strong>PRINT_OPERATIONS:</strong> If set to 1, it will print to the configured output (stdout by default) each memory alloc operation done in the program (allocation or free). Otherwise, the library will just print problems and notifications and the final dump. <em>[Default value = 1]</em></li>
<li><strong>MAX_ALLOC:</strong> Internal library memory stack length (in elements). It is the max amount of allocations that can be tracked without being freed. You can increase this value at will if needed. In fact, if it is needed, the library will print a message saying: &#8220;INTERNAL_ERROR: Allocation stack overflow, please increase MAX_ALLOC&#8221;. <em>[Default value = 256]</em></li>
<li><strong>g_fFile:</strong> FILE* to print the library generated notifications to. <em>[Default value = stdout]</em></li>
<li><strong>MAX_DELETE_STACK:</strong> Internal library <em>delete</em> nesting stack array length (in elements). It is the max amount of <em>delete</em>s that can be nested. You can increase this value at will if needed. In fact, if it is needed, the library will print a message saying: &#8220;INTERNAL_ERROR: Delete stack overflow, please increase MAX_DELETE_STACK&#8221;. <em>[Default value = 16]</em></li>
</ul>
<p>Finally, there are two more functions you could use wherever needed:</p>
<ul>
<li><strong>PrintTotalAllocatedMemory():</strong> Prints the accumulative amount of memory allocated at the moment of the call.</li>
<li><strong>PrintMemoryReservedByCMemDbgLibrary():</strong> Prints the amount of memory reserved by the library (defined at compilation time by the MAX_ALLOC constant).</li>
</ul>
<p>That&#8217;s all&#8230; I said it was easy to use.</p>
<p>Anyway, if after reading the cppMemDbg.h file&#8217;s comments, you have any questions, comments or suggestions, please feel free to contact me.</p>
<p>Here is the <a href="http://www.neoegm.com/tech/programming/c-cpp/control-de-asistencia/">Attendance Control</a> project modified to test it for memory leaks and allocation/deallocation problems with the cppMemDbg library:</p>
<div align="center">
<p class="download"><a href="http://download.neoegm.com/software/cppmemdbg/ControlAsistencia_cppMemDbg.zip">Download sample project</a></p>
</div>
<p>The code is portable between Linux and Windows (I&#8217;ve tested it myself on both platforms and it worked seamlessly).</p>
<p>It&#8217;s been developed, compiled and tested using <a href="http://wxdsgn.sourceforge.net/">wxDev-C++</a> for Windows with the <a href="http://www.mingw.org/">MinGW compiler</a> (included in the bundle). In Linux, it was compiled using the GNU GCC compiler.</p>
<p>It is configured to send the output to the &#8220;TestMemDbg.txt&#8221; file in the working directory. This is the relevant piece of the Main.cpp file:</p>
<pre class="brush: cpp; first-line: 101; highlight: [107,109]; title: ; notranslate">
//...

#include &quot;ControlDeAsistencia.h&quot;
#include &quot;BasicFunctions.h&quot;
#include &lt;fstream&gt;

#include &quot;cppMemDbg.h&quot;

cppMemDbg cDbg(&quot;TestMemDbg.txt&quot;);

//...
</pre>
<p>And this is its successful output (the real output es tabbed to make it easier the analysis en a spreadsheet software):</p>

<pre class="console">>new[]  003D3F58  14  [String.cpp:59]
>new[]  003D2438  14  [String.cpp:59]
>delete[]  003D3F58  14  (String.cpp:59)  [String.h:41]
>new[]  003D3EB0  7  [String.cpp:59]
>new[]  003D2450  7  [String.cpp:59]
[...]
>delete[]  003D2558  4  (String.cpp:59)  [String.h:41]
>delete[]  003D24F0  4  (String.cpp:59)  [String.h:41]
>delete  003D2490  40  (Lista.h:120)  [Lista.h:112]
>INFO  No memory leaks detected
</pre>
<h4>Incoming search terms for the article:</h4>
<ul>
<li><a href="https://www.neoegm.com/tech/programming/c-cpp/cppmemdbg-easy-to-use-cpp-memory-leak-detection-library/" title="cppMemDbg">cppMemDbg</a></li>
<li><a href="https://www.neoegm.com/tech/programming/c-cpp/cppmemdbg-easy-to-use-cpp-memory-leak-detection-library/" title="mingw memory leak detection">mingw memory leak detection</a></li>
<li><a href="https://www.neoegm.com/tech/programming/c-cpp/cppmemdbg-easy-to-use-cpp-memory-leak-detection-library/" title="memory leak detection mingw">memory leak detection mingw</a></li>
<li><a href="https://www.neoegm.com/tech/programming/c-cpp/cppmemdbg-easy-to-use-cpp-memory-leak-detection-library/" title="mingw memory leak">mingw memory leak</a></li>
<li><a href="https://www.neoegm.com/tech/programming/c-cpp/cppmemdbg-easy-to-use-cpp-memory-leak-detection-library/" title="c memory leak library">c memory leak library</a></li>
<li><a href="https://www.neoegm.com/tech/programming/c-cpp/cppmemdbg-easy-to-use-cpp-memory-leak-detection-library/" title="c memory leak detector">c memory leak detector</a></li>
<li><a href="https://www.neoegm.com/tech/programming/c-cpp/cppmemdbg-easy-to-use-cpp-memory-leak-detection-library/" title="memory leak mingw">memory leak mingw</a></li>
<li><a href="https://www.neoegm.com/tech/programming/c-cpp/cppmemdbg-easy-to-use-cpp-memory-leak-detection-library/" title="c memory leak detection library">c memory leak detection library</a></li>
<li><a href="https://www.neoegm.com/tech/programming/c-cpp/cppmemdbg-easy-to-use-cpp-memory-leak-detection-library/" title="memory leak detection c">memory leak detection c</a></li>
<li><a href="https://www.neoegm.com/tech/programming/c-cpp/cppmemdbg-easy-to-use-cpp-memory-leak-detection-library/" title="libreria para delete en c">libreria para delete en c</a></li>
</ul>
<div class='yarpp-related-rss'>
<strong><p>Related posts:<ol>
<li><a href="https://www.neoegm.com/tech/programming/c-cpp/cmemdbg-easy-to-use-c-memory-leak-detection-library/" rel="bookmark" title="cMemDbg &#8211; Easy to use C memory leak detection library">cMemDbg &#8211; Easy to use C memory leak detection library </a></li>
<li><a href="https://www.neoegm.com/tech/programming/c-cpp/simple-list-class/" rel="bookmark" title="Simple C++ List Class">Simple C++ List Class </a></li>
<li><a href="https://www.neoegm.com/tech/programming/c-cpp/simple-string-class/" rel="bookmark" title="Simple C++ String Class">Simple C++ String Class </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>https://www.neoegm.com/tech/programming/c-cpp/cppmemdbg-easy-to-use-cpp-memory-leak-detection-library/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>cMemDbg &#8211; Easy to use C memory leak detection library</title>
		<link>https://www.neoegm.com/tech/programming/c-cpp/cmemdbg-easy-to-use-c-memory-leak-detection-library/</link>
		<comments>https://www.neoegm.com/tech/programming/c-cpp/cmemdbg-easy-to-use-c-memory-leak-detection-library/#comments</comments>
		<pubDate>Mon, 06 Apr 2009 18:42:07 +0000</pubDate>
		<dc:creator><![CDATA[NeoEGM]]></dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[Debug]]></category>
		<category><![CDATA[Easy]]></category>
		<category><![CDATA[Freeware]]></category>
		<category><![CDATA[GNU GPL]]></category>
		<category><![CDATA[Include]]></category>
		<category><![CDATA[Library]]></category>
		<category><![CDATA[Memory Leaks]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Source Code]]></category>

		<guid isPermaLink="false">http://www.neoegm.com/?p=58</guid>
		<description><![CDATA[After some time working with C, I decided to develop a very easy to use library which can help you to detect and track memory leaks. There are lots of solutions for this on the net, but this one has the particularity of being really simple to implement. You just have to add the following [&#8230;]<div class='yarpp-related-rss'>
<strong>
Related posts:<ol>
<li><a href="https://www.neoegm.com/tech/programming/c-cpp/cppmemdbg-easy-to-use-cpp-memory-leak-detection-library/" rel="bookmark" title="cppMemDbg &#8211; Easy to use C++ memory leak detection library">cppMemDbg &#8211; Easy to use C++ memory leak detection library </a></li>
<li><a href="https://www.neoegm.com/tech/programming/c-cpp/simple-list-class/" rel="bookmark" title="Simple C++ List Class">Simple C++ List Class </a></li>
<li><a href="https://www.neoegm.com/tech/programming/c-cpp/simple-string-class/" rel="bookmark" title="Simple C++ String Class">Simple C++ String Class </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<p>After some time working with C, I decided to develop a very easy to use library which can help you to detect and track memory leaks.</p>
<p>There are lots of solutions for this on the net, but this one has the particularity of being really simple to implement.</p>
<p><span id="more-58"></span><br />
You just have to add the following include to your main include file (a file that gets included by each file of your project) or, in case you don&#8217;t have one, to each file that calls any memory allocation function (malloc, realloc, calloc or free). The only extra caution you have to take into account is to include it <strong>after</strong> the standard headers (stdio.h, stdlib.h, malloc.h, etc.).</p>
<p>This is the line:</p>
<pre class="brush: plain; light: true; title: ; notranslate">#include &quot;cMemDbg.h&quot;</pre>
<p>That&#8217;s all&#8230; Expecting more? Well, there is a final step&#8230; You have to make this call just before you exit your program so you can get the detailed conclusions:</p>
<pre class="brush: plain; light: true; title: ; notranslate">PrintMemoryLeakInfo();</pre>
<p>Easy, not?</p>
<p>That was all&#8230; Really.</p>
<p>Now, before going to more &#8220;expert&#8221; features, I&#8217;ll leave here the link to download the library for the ones who don&#8217;t wish to keep reading:</p>
<div align="center">
<p class="download"><a href="http://download.neoegm.com/software/cmemdbg/cMemDbg_1.0.zip">Download cMemDbg 1.0</a></p>
</div>
<p>Now let&#8217;s continue&#8230;</p>
<p>The library comes with three configurable settings (available in cMemDbg.c):</p>
<ul>
<li><strong>PRINT_OPERATIONS:</strong> If set to 1, it will print to the configured output (stdout by default) each memory alloc operation done in the program (allocation or free). Otherwise, the library will just print problems and notifications and the final dump. <em>[Default value = 1]</em></li>
<li><strong>MAX_ALLOC:</strong> Internal library memory stack length (in elements). It is the max amount of allocations that can be tracked without being freed. You can increase this value at will if needed. In fact, if it is needed, the library will print a message saying: &#8220;INTERNAL_ERROR: Allocation stack overflow, please increase MAX_ALLOC&#8221;. <em>[Default value = 256]</em></li>
<li><strong>PRINT_OUTPUT:</strong> Location to print the library generated notifications (can be any stream/file in which you could print using fprintf). <em>[Default value = stdout]</em></li>
</ul>
<p>Finally, there are two more functions you could use wherever needed:</p>
<ul>
<li><strong>PrintTotalAllocatedMemory():</strong> Prints the accumulative amount of memory allocated at the moment of the call.</li>
<li><strong>PrintMemoryReservedByCMemDbgLibrary():</strong> Prints the amount of memory reserved by the library (defined at compilation time by the MAX_ALLOC constant).</li>
</ul>
<p>That&#8217;s all&#8230; I said it was easy to use.</p>
<p>Anyway, if after reading the cMemDbg.h files&#8217; comments, you have any questions, comments or suggestions, please feel free to contact me.<br />
<h4>Incoming search terms for the article:</h4>
<ul>
<li><a href="https://www.neoegm.com/tech/programming/c-cpp/cmemdbg-easy-to-use-c-memory-leak-detection-library/" title="detectar fugas de memoria en c">detectar fugas de memoria en c</a></li>
<li><a href="https://www.neoegm.com/tech/programming/c-cpp/cmemdbg-easy-to-use-c-memory-leak-detection-library/" title="cmemdbg">cmemdbg</a></li>
<li><a href="https://www.neoegm.com/tech/programming/c-cpp/cmemdbg-easy-to-use-c-memory-leak-detection-library/" title="c memory leak">c memory leak</a></li>
<li><a href="https://www.neoegm.com/tech/programming/c-cpp/cmemdbg-easy-to-use-c-memory-leak-detection-library/" title="detectar fuga memoria c">detectar fuga memoria c</a></li>
<li><a href="https://www.neoegm.com/tech/programming/c-cpp/cmemdbg-easy-to-use-c-memory-leak-detection-library/" title="detectar fugas de memoria">detectar fugas de memoria</a></li>
<li><a href="https://www.neoegm.com/tech/programming/c-cpp/cmemdbg-easy-to-use-c-memory-leak-detection-library/" title="internal_error allocation stack overflow please increase max_alloc">internal_error allocation stack overflow please increase max_alloc</a></li>
<li><a href="https://www.neoegm.com/tech/programming/c-cpp/cmemdbg-easy-to-use-c-memory-leak-detection-library/" title="detectar memory leaks c">detectar memory leaks c</a></li>
<li><a href="https://www.neoegm.com/tech/programming/c-cpp/cmemdbg-easy-to-use-c-memory-leak-detection-library/" title="detectar fugas de memoria c">detectar fugas de memoria c</a></li>
<li><a href="https://www.neoegm.com/tech/programming/c-cpp/cmemdbg-easy-to-use-c-memory-leak-detection-library/" title="freebsd memory leak detection">freebsd memory leak detection</a></li>
<li><a href="https://www.neoegm.com/tech/programming/c-cpp/cmemdbg-easy-to-use-c-memory-leak-detection-library/" title="Detector de fugas de memoria">Detector de fugas de memoria</a></li>
</ul>
<div class='yarpp-related-rss'>
<strong><p>Related posts:<ol>
<li><a href="https://www.neoegm.com/tech/programming/c-cpp/cppmemdbg-easy-to-use-cpp-memory-leak-detection-library/" rel="bookmark" title="cppMemDbg &#8211; Easy to use C++ memory leak detection library">cppMemDbg &#8211; Easy to use C++ memory leak detection library </a></li>
<li><a href="https://www.neoegm.com/tech/programming/c-cpp/simple-list-class/" rel="bookmark" title="Simple C++ List Class">Simple C++ List Class </a></li>
<li><a href="https://www.neoegm.com/tech/programming/c-cpp/simple-string-class/" rel="bookmark" title="Simple C++ String Class">Simple C++ String Class </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>https://www.neoegm.com/tech/programming/c-cpp/cmemdbg-easy-to-use-c-memory-leak-detection-library/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
