<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Heap Corruption]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich habe eine Klasse gechrieben in der ich den &amp;-Operator überladen möchte:</p>
<pre><code class="language-cpp">BinaryInt BinaryInt::operator&amp;(const BinaryInt bi)
{
	int size	= MAX(strlen(cIntBin), strlen(bi.cIntBin)) + 1;		// + 1: Null Terminator
	int minsize = MIN(strlen(cIntBin), strlen(bi.cIntBin));

	char* res = new char[size];

	for (int i=0; i &lt; minsize; ++i)
		res[i] = (cIntBin[i] == '1' &amp;&amp; bi.cIntBin[i] == '1') ? '1' : '0';

	res[minsize] = '\0';

	delete [] cIntBin;            //  HEAP CORRUPTION #135!!
	cIntBin = new char[strlen(res)];
	strcpy(cIntBin, res);
	delete [] res;

	return *this;
}
</code></pre>
<p><code>cIntBin</code> wurde über den Heap alloziert und soll nun gelöcht werden. Schon hier scheitert das Ganze. Ich habe nun einige Zeit versucht das Problem selber zu lösen, aber irgendwie komme ich einfach nicht darauf... Ich bin sicher das ist kein Problem für euch Profis!</p>
<p>Vielen Dank im Voraus!</p>
<p><code>MIN</code> und <code>MAX</code> :</p>
<pre><code class="language-cpp">#define MAX(a,b)((a) &gt;= (b) ? (a) : (b))
#define MIN(a,b)((a) &lt;= (b) ? (a) : (b))
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/299359/heap-corruption</link><generator>RSS for Node</generator><lastBuildDate>Thu, 13 Aug 2026 07:57:31 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/299359.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 09 Feb 2012 11:10:01 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Heap Corruption on Thu, 09 Feb 2012 11:11:22 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich habe eine Klasse gechrieben in der ich den &amp;-Operator überladen möchte:</p>
<pre><code class="language-cpp">BinaryInt BinaryInt::operator&amp;(const BinaryInt bi)
{
	int size	= MAX(strlen(cIntBin), strlen(bi.cIntBin)) + 1;		// + 1: Null Terminator
	int minsize = MIN(strlen(cIntBin), strlen(bi.cIntBin));

	char* res = new char[size];

	for (int i=0; i &lt; minsize; ++i)
		res[i] = (cIntBin[i] == '1' &amp;&amp; bi.cIntBin[i] == '1') ? '1' : '0';

	res[minsize] = '\0';

	delete [] cIntBin;            //  HEAP CORRUPTION #135!!
	cIntBin = new char[strlen(res)];
	strcpy(cIntBin, res);
	delete [] res;

	return *this;
}
</code></pre>
<p><code>cIntBin</code> wurde über den Heap alloziert und soll nun gelöcht werden. Schon hier scheitert das Ganze. Ich habe nun einige Zeit versucht das Problem selber zu lösen, aber irgendwie komme ich einfach nicht darauf... Ich bin sicher das ist kein Problem für euch Profis!</p>
<p>Vielen Dank im Voraus!</p>
<p><code>MIN</code> und <code>MAX</code> :</p>
<pre><code class="language-cpp">#define MAX(a,b)((a) &gt;= (b) ? (a) : (b))
#define MIN(a,b)((a) &lt;= (b) ? (a) : (b))
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2178826</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2178826</guid><dc:creator><![CDATA[C++H]]></dc:creator><pubDate>Thu, 09 Feb 2012 11:11:22 GMT</pubDate></item><item><title><![CDATA[Reply to Heap Corruption on Thu, 09 Feb 2012 11:23:07 GMT]]></title><description><![CDATA[<p>Wieso nutzt du new[]? Wieso nutzt du delete[]? Was soll der Code überhaupt machen? Hast du auch brav die Regel der großen Drei befolgt? Verstehst du überhaupt, worauf ich mit meinen Fragen hinaus will?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2178829</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2178829</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Thu, 09 Feb 2012 11:23:07 GMT</pubDate></item><item><title><![CDATA[Reply to Heap Corruption on Thu, 09 Feb 2012 11:32:11 GMT]]></title><description><![CDATA[<p>Ich benutze new[], weil ich einen char Pointer habe und die Anzahl der Zeichen nicht ganau festgelegt (bzw. variabel) sind. Der Code soll eigentlich nur eine logische AND Operation mit zwei Zeichenketten durchführen und natürlich habe ich die Regel der großen Drei befolgt!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2178832</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2178832</guid><dc:creator><![CDATA[C++H]]></dc:creator><pubDate>Thu, 09 Feb 2012 11:32:11 GMT</pubDate></item><item><title><![CDATA[Reply to Heap Corruption on Thu, 09 Feb 2012 11:34:38 GMT]]></title><description><![CDATA[<p>wieso nicht einfach:</p>
<pre><code class="language-cpp">for (int i=0; i &lt; minsize; ++i)
{
 cIntBin[i] &amp;= bi.cIntBin[i]
}
</code></pre>
<p>?<br />
und die ganzen new/delete usw. sind weg...<br />
Vorausgesetzt man hat hat in der Konstruktoren und Zuweisungen alles richtig gemacht.</p>
<p>P.S. Allerdings hast Du so nach wie vor einen Bug wenn Du bei der unterschiedlichen Längen... Die char-arrays müssen dann von links gefüllt werden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2178834</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2178834</guid><dc:creator><![CDATA[Travor]]></dc:creator><pubDate>Thu, 09 Feb 2012 11:34:38 GMT</pubDate></item><item><title><![CDATA[Reply to Heap Corruption on Thu, 09 Feb 2012 11:36:04 GMT]]></title><description><![CDATA[<p>und wieso hast du ein char-Array? Was spricht gegen std::string/std::vector/std:bitset?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2178835</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2178835</guid><dc:creator><![CDATA[daddy_felix]]></dc:creator><pubDate>Thu, 09 Feb 2012 11:36:04 GMT</pubDate></item><item><title><![CDATA[Reply to Heap Corruption on Thu, 09 Feb 2012 11:45:19 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">cIntBin = new char[strlen(res)];
    strcpy(cIntBin, res);
</code></pre>
<p>Fällt die da was auf? Mit std::string wäre es nicht passiert ...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2178837</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2178837</guid><dc:creator><![CDATA[manni66]]></dc:creator><pubDate>Thu, 09 Feb 2012 11:45:19 GMT</pubDate></item><item><title><![CDATA[Reply to Heap Corruption on Thu, 09 Feb 2012 11:47:36 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/27972">@Travor</a>:<br />
Habe ich gemacht:</p>
<pre><code class="language-cpp">BinaryInt BinaryInt::operator&amp;(const BinaryInt bi)
{
	int size	= MAX(strlen(cIntBin), strlen(bi.cIntBin)) + 1;		// + 1: Null Terminator
	int minsize = MIN(strlen(cIntBin), strlen(bi.cIntBin));

	//char* res = new char[size];

	for (int i=0; i &lt; minsize; ++i)
		cIntBin[i] &amp;= bi.cIntBin[i];
		//res[i] = (cIntBin[i] == '1' &amp;&amp; bi.cIntBin[i] == '1') ? '1' : '0';

	//res[minsize] = '\0';

	//BinaryInt Copy(*this);
	//delete [] Copy.cIntBin;
	//Copy.cIntBin = new char[strlen(res)];
	//strcpy(Copy.cIntBin, res);
	//delete [] res;

	return *this;        // HEAP CORRUPTION #138
}
</code></pre>
<p>Jetzt tritt eine Heap Curruption #138 auf. Falls es an den Konstruktoren liegt, hier nochmal der Code:</p>
<pre><code class="language-cpp">BinaryInt::BinaryInt()
	: cIntBin(0), cIntDec(0)
{}

BinaryInt::BinaryInt(char* DecInt)
	: cIntBin(new char[strlen(DecInt)]), cIntDec(new char[5])
{
	strcpy(cIntBin, DecInt);
	strcpy(cIntDec, &quot;55555&quot;);
}

BinaryInt::BinaryInt(const BinaryInt&amp; bi)
	: cIntBin(new char[strlen(bi.cIntBin)]), cIntDec(new char[strlen(cIntDec)])
{
	strcpy(cIntBin, bi.cIntBin);
	strcpy(cIntDec, bi.cIntDec);
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2178838</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2178838</guid><dc:creator><![CDATA[C++H]]></dc:creator><pubDate>Thu, 09 Feb 2012 11:47:36 GMT</pubDate></item><item><title><![CDATA[Reply to Heap Corruption on Thu, 09 Feb 2012 11:50:31 GMT]]></title><description><![CDATA[<p>Für einen String mit n Zeichen Länge brauchst Du n+1 Zeichen Platz. Aber eigentlich weißt Du das schon (oder hast Du den u.a. Kommentar kopiert?). Ergo: Du hast</p>
<pre><code class="language-cpp">// + 1: Null Terminator
</code></pre>
<p>mindestens fünfmal vergessen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2178841</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2178841</guid><dc:creator><![CDATA[LordJaxom]]></dc:creator><pubDate>Thu, 09 Feb 2012 11:50:31 GMT</pubDate></item><item><title><![CDATA[Reply to Heap Corruption on Thu, 09 Feb 2012 11:50:23 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">cIntBin(new char[strlen(DecInt)]
</code></pre>
<p>siehe oben</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2178842</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2178842</guid><dc:creator><![CDATA[manni66]]></dc:creator><pubDate>Thu, 09 Feb 2012 11:50:23 GMT</pubDate></item><item><title><![CDATA[Reply to Heap Corruption on Thu, 09 Feb 2012 11:56:27 GMT]]></title><description><![CDATA[<p>Deine Arrays sind zu klein. Warum verwendest du nicht einfach std::string?<br />
Und nimm doch std::min() bzw. std::max() oder verwend zumindest inline Funktionen und keine Makros dafür...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2178843</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2178843</guid><dc:creator><![CDATA[dot]]></dc:creator><pubDate>Thu, 09 Feb 2012 11:56:27 GMT</pubDate></item><item><title><![CDATA[Reply to Heap Corruption on Thu, 09 Feb 2012 11:58:44 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/869">@C</a>++H</p>
<p>Man hat Dir schon mehrmals beantwortet <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /> <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /> .<br />
Und wie gesagt. So eine UND-operation funktioniert nur bei BigEndians richtig..<br />
Da muss Du dir ganz bewusst vor Augen führen, was das für die Konsequenzen hat.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2178846</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2178846</guid><dc:creator><![CDATA[Travor]]></dc:creator><pubDate>Thu, 09 Feb 2012 11:58:44 GMT</pubDate></item><item><title><![CDATA[Reply to Heap Corruption on Thu, 09 Feb 2012 11:59:13 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/10648">@LordJaxom</a>:<br />
Danke - irgendwie habe ich das übersehen. Nun bekomme ich eine &quot;Debug Assertion Failed!&quot;: _BLOCK_TYPE_IS_VALID.<br />
(Der Kommentar ist überings nicht kopiert)</p>
<pre><code class="language-cpp">BinaryInt BinaryInt::operator&amp;(const BinaryInt bi)
{
	int size	= MAX(strlen(cIntBin), strlen(bi.cIntBin)) + 1;		// + 1: Null Terminator
	int minsize = MIN(strlen(cIntBin), strlen(bi.cIntBin));

	char* res = new char[size];

	for (int i=0; i &lt; minsize; ++i)
		res[i] = cIntBin[i] &amp; bi.cIntBin[i];

	res[minsize] = '\0';

	delete [] cIntBin;
	cIntBin = new char[size];
	strcpy(cIntBin, res);
	delete [] res;

	return *this;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2178847</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2178847</guid><dc:creator><![CDATA[C++H]]></dc:creator><pubDate>Thu, 09 Feb 2012 11:59:13 GMT</pubDate></item><item><title><![CDATA[Reply to Heap Corruption on Thu, 09 Feb 2012 12:06:46 GMT]]></title><description><![CDATA[<p>In diesem Abschnitt sehe ich keinen Fehler, der muss woanders verursacht werden.</p>
<p>Wieso duplizierst Du zum Schluß eigentlich res nochmal und löscht dann das Original statt res einfach cIntBin zuzuweisen?</p>
<p>Warum nutzt Du nicht std::string oder std::vector?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2178850</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2178850</guid><dc:creator><![CDATA[LordJaxom]]></dc:creator><pubDate>Thu, 09 Feb 2012 12:06:46 GMT</pubDate></item><item><title><![CDATA[Reply to Heap Corruption on Thu, 09 Feb 2012 12:19:54 GMT]]></title><description><![CDATA[<p>Okay - warum ich std::string und std::vector etc. nicht benutze ist einfach zu erklären: Ich will gerade auf diesem Gebiet ohne zusätzliche Container arbeiten um u.A. mit solchen Problemen wie diesem Erfahrung zu gewinnen.</p>
<p>Eventuell ist mein = Operator das Problem (dort erhalte ich auch beim ersten delete[] den o.g. Error), den ich auch in meinem Kopierkonstruktor verwende:</p>
<pre><code class="language-cpp">BinaryInt&amp; BinaryInt::operator=(const BinaryInt&amp; bi)
{
	if (*this != bi)
	{
		delete [] cIntDec;
		delete [] cIntBin;

		cIntDec = new char[strlen(bi.cIntDec) + 1];
		cIntBin = new char[strlen(bi.cIntBin) + 1];

		strcpy(cIntDec, bi.cIntDec);
		strcpy(cIntBin, bi.cIntBin);
	}

	return *this;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2178859</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2178859</guid><dc:creator><![CDATA[C++H]]></dc:creator><pubDate>Thu, 09 Feb 2012 12:19:54 GMT</pubDate></item><item><title><![CDATA[Reply to Heap Corruption on Thu, 09 Feb 2012 12:39:28 GMT]]></title><description><![CDATA[<ol>
<li>Du vergleichst auf Gleichheit der Werte, nicht auf Gleichheit der Objekte.</li>
<li>wenn der zweite <code>new[]</code> Aufruf fehlschlägt erzeugt dein Code ein Speicherleck, da für den ersten <code>new[]</code> niemals <code>delete[]</code> aufgerufen wird. Eine Möglichkeit ist die Verwendung von <code>new (nothrow)[]</code> . Ausserdem sollte deine Klasse <code>exception safe</code> sein, d.h. ein Objekt sollte sich <s>beim Auftreten einer Exception nicht verändern</s> nach dem Auftreten einer Exception in einem definierten Zustand befinden. Wenn in Zeile 8 oder 9 eine <code>bad_alloc</code> Exception geworfen wird sind die member <code>cIntDec</code> und <code>cIntBin</code> schon zerstört und zeigen in´s Nirvana.</li>
</ol>
]]></description><link>https://www.c-plusplus.net/forum/post/2178869</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2178869</guid><dc:creator><![CDATA[DocShoe]]></dc:creator><pubDate>Thu, 09 Feb 2012 12:39:28 GMT</pubDate></item><item><title><![CDATA[Reply to Heap Corruption on Thu, 09 Feb 2012 13:02:59 GMT]]></title><description><![CDATA[<p>Ist absolut ungeprüft.. Aber in etwa so wie es laufen sollte:</p>
<pre><code class="language-cpp">template&lt; typename T &gt;
bool __inline SafeNewArray( T*&amp; instance, size_t anzahl )
{
	try
	{
		instance = new T[ anzahl ];
	}
	catch( std::bad_alloc&amp; /*not_needed*/)
	{
		instance = 0;
		return false;
	}
	return true;
}

template&lt; typename T &gt;
void SafeDeleteArray( T*&amp; p )
{
	delete[] p;
	p = 0;
}

BinaryInt&amp; BinaryInt::operator=( const BinaryInt&amp; bi )
{
    //if (*this != bi)
	if ( this != &amp;bi )	// Kein versteckter Operator==(... )
    {
        //delete [] cIntDec;
        //delete [] cIntBin;

		size_t newSizeDec = strlen( bi.cIntDec ) + 1;
		size_t newSizeBin = strlen( bi.cIntBin ) + 1;
		if ( newSizeDec != 0  &amp;&amp;  newSizeBin != 0 )
		{
			char* decTmp = 0;
			char* binTmp = 0;
			if ( !SafeNewArray( decTmp, newSizeDec )  ||  !SafeNewArray( binTmp, newSizeBin ) )
			{
				// Zu wenig Speicher...
				// Was ist zu tun ?

				// z.B. alles lassen wie es ist.
				SafeDeleteArray( decTmp );
				SafeDeleteArray( binTmp );
				return ( *this );
			}

			//strcpy(cIntDec, bi.cIntDec);
			//strcpy(cIntBin, bi.cIntBin);

			// Die strings sicher kopieren...
			// Hie stehen Dir je nach OS/Compiler unterschiedliche Möglichkeiten zu Verfügung.
			errno_t result = strcpy_s( decTmp, newSizeDec, bi.cIntDec );
			if ( result != 0 )
			{
				// Fehler- blabla...
				// Was ist zu tun ?
			}

			result = strcpy_s( binTmp, newSizeBin, bi.cIntBin );
			if ( result != 0 )
			{
				// Fehler- blabla...
				// Was ist zu tun ?
			}

			// Alles richtig.
			SafeDeleteArray( cIntDec );
			SafeDeleteArray( cIntBin );

			cIntDec = decTmp;
			cIntBin = binTmp;
		}
		else
		{
			// Overflow -&gt; was ist zu tun ?
			// Ist aber unrealistisch, da die Konstruktoren vorher schon aussteigen würden.
		}

    }

    return *this;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2178886</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2178886</guid><dc:creator><![CDATA[Travor]]></dc:creator><pubDate>Thu, 09 Feb 2012 13:02:59 GMT</pubDate></item><item><title><![CDATA[Reply to Heap Corruption on Thu, 09 Feb 2012 13:12:34 GMT]]></title><description><![CDATA[<p>Warum verändert dein SafeDeleteArray den Zeigerwert? Willst du mögliche Fehler vertuschen, anstatt es schon beim Debuglauf krachen zu lassen?</p>
<p>Was soll das überhaupt? Deine SafeXXXArrays sind vielleicht gegen Exceptions sicher die sowieso nie fliegen, aber nicht gegen ganz normale Exceptions im Code oder nachlässige Programmierer. Ein RAII-Design wäre sicher gegen alles</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2178892</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2178892</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Thu, 09 Feb 2012 13:12:34 GMT</pubDate></item><item><title><![CDATA[Reply to Heap Corruption on Thu, 09 Feb 2012 13:14:39 GMT]]></title><description><![CDATA[<p>Mal ganz ehrlich... wenn man soweit ist, solchen Code zu verwenden, dann kann man sich gratulieren und zugestehen, dass man die Problematik verstanden hat. Anschließend wirft man das raus und benutzt <code>std::string/std::vector</code> .</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2178893</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2178893</guid><dc:creator><![CDATA[DocShoe]]></dc:creator><pubDate>Thu, 09 Feb 2012 13:14:39 GMT</pubDate></item><item><title><![CDATA[Reply to Heap Corruption on Thu, 09 Feb 2012 13:20:45 GMT]]></title><description><![CDATA[<p>DocShoe schrieb:</p>
<blockquote>
<p>Mal ganz ehrlich... wenn man soweit ist, solchen Code zu verwenden, dann kann man sich gratulieren und zugestehen, dass man die Problematik verstanden hat. Anschließend wirft man das raus und benutzt <code>std::string/std::vector</code> .</p>
</blockquote>
<p>richtig <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /><br />
aber es wurde ja ausdrücklich so gewünscht:)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2178898</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2178898</guid><dc:creator><![CDATA[Travor]]></dc:creator><pubDate>Thu, 09 Feb 2012 13:20:45 GMT</pubDate></item><item><title><![CDATA[Reply to Heap Corruption on Thu, 09 Feb 2012 13:31:53 GMT]]></title><description><![CDATA[<p>Würde es nicht einfach reichen, einen (unsigned) Integer zu warappen (kann ja auch 64 Bit breit sein). Dann würden sich nämlich viele Dinge vereinfachen. Vor allem das Hinzufügen von neuen Operatoren (|, ^ etc.) würde sich vereinfachen:</p>
<pre><code class="language-cpp">class BinInt
{
	typedef unsigned bi_type;
public:
	BinInt() : val_(0) {}
	BinInt(const BinInt&amp; b) : val_(b.val_) {}
	BinInt(bi_type v) : val_(v) {}
	BinInt operator &amp;(const BinInt&amp; b)
	{
		return BinInt(val_ &amp; b.val_);
	}
	BinInt&amp; operator &amp;=(const BinInt&amp; b)
	{
		val_ &amp;= b.val_;
		return *this;
	}
	BinInt&amp; operator =(const BinInt&amp; b)
	{
		val_ = b.val_;
		return *this;
	}
	BinInt operator &amp;(bi_type b)
	{
		return BinInt(val_ &amp; b);
	}
	BinInt&amp; operator &amp;=(bi_type b)
	{
		val_ &amp;= b;
		return *this;
	}

	friend ostream&amp; operator&lt;&lt;(ostream&amp; o, const BinInt&amp; b);

private:
	bi_type val_;
};

ostream&amp; operator &lt;&lt;(ostream&amp; o, const BinInt&amp; b)
{
	for(int i=sizeof(BinInt::bi_type)*CHAR_BIT-1;i&gt;=0;--i)
	{
		o&lt;&lt;((b.val_&gt;&gt;i) &amp; 1?'1':'0');
	}
	return o;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2178907</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2178907</guid><dc:creator><![CDATA[Gelangweilter]]></dc:creator><pubDate>Thu, 09 Feb 2012 13:31:53 GMT</pubDate></item><item><title><![CDATA[Reply to Heap Corruption on Thu, 09 Feb 2012 14:24:28 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/27972">@Travor</a>:<br />
Ich habe mal deinen Code umgesetzt. Jetzt crasht es, aber schon beim strcyp_s (&quot;Zugriffsverletzung&quot;).</p>
<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/18927">@DocShoe</a>:<br />
Ich weiß, dass man sowas in der Praxis nicht machen wird - zumal std::string und std::vector perfekt funktionieren. Mir geht es, aber (wie schon erwähnt) um den Lerneffekt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2178935</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2178935</guid><dc:creator><![CDATA[C++H]]></dc:creator><pubDate>Thu, 09 Feb 2012 14:24:28 GMT</pubDate></item><item><title><![CDATA[Reply to Heap Corruption on Thu, 09 Feb 2012 14:54:23 GMT]]></title><description><![CDATA[<p>Der objektorientierte Designer sagt: Wieso soll sich deine BinaryInt Klasse um Implementierungsdetails ihres Datenspeichers kümmern müssen? Schreib eine Klasse BinaryString, die kümmert sich um <strong>ihren eigenen</strong> Speicherplatz, weiß wie sie <strong>selber</strong> kopiert wird, weiß <strong>selber</strong> wie man <strong>ihre</strong> Länge misst, usw.</p>
<p>Diese Klasse wird dann von deinem BinaryInt benutzt, der BinaryInt kümmert sich dann nur noch um seine eigenen Sachen und nutzt deren zur Verügung gestellte Funktionalität, um sein Ding zu machen.</p>
<p>Das nennt man dann Kapselung.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2178953</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2178953</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Thu, 09 Feb 2012 14:54:23 GMT</pubDate></item><item><title><![CDATA[Reply to Heap Corruption on Thu, 09 Feb 2012 19:16:34 GMT]]></title><description><![CDATA[<p>C++H schrieb:</p>
<blockquote>
<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/27972">@Travor</a>:<br />
Ich habe mal deinen Code umgesetzt. Jetzt crasht es, aber schon beim strcyp_s (&quot;Zugriffsverletzung&quot;).</p>
</blockquote>
<p>Ich befürchte Du muss dein Code hier komplett stellen damit wir den Fehler entdecken können.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2179085</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2179085</guid><dc:creator><![CDATA[Travor]]></dc:creator><pubDate>Thu, 09 Feb 2012 19:16:34 GMT</pubDate></item></channel></rss>