<?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[Wohl Ärger mit Zeigern.....]]></title><description><![CDATA[<p>Hi zusammen,<br />
ich hab nen Zeigerproblem mit der folgenden Klasse, es wird, wenn ich eine Zuweisung mache:</p>
<pre><code class="language-cpp">IntArray temp = getDomainAt(b,i);

     int* myArr;
     myArr = temp.getArray(); 
[cpp]

ab und zu mal eine 5 in beiden arrays, myArr und temp.arr zu 27. Hat jemand ne Idee warum?

[cpp]

  class IntArray {
       public:
        int* arr;
        int size;

        IntArray() {
        	arr = NULL;
        	size = 0;
        }

        ~IntArray(){
        	//std::cout &lt;&lt; &quot;IntArray Destruktor\n&quot;;
        	//if (arr!= NULL) delete[] arr;
        	//std::cout &lt;&lt; &quot;Intarray DEstruktor Ende\n&quot;;
        }

        IntArray(const IntArray&amp; p){
        	size=p.size;
        	arr = new int[size];
        	for (int i=0; i&lt;size; i++)
        		arr[i] =  p.arr[i];
        }

		  IntArray const&amp; operator=(IntArray const&amp; p) {
            size=p.size;
        	arr = new int[size];
        	for (int i=0; i&lt;size; i++)
        		arr[i] =  p.arr[i];
        	return *this;
        }

        int* getArray() {
        	if (arr == NULL) return NULL;
        	int* ret = new int[size];
        	for (int i=0; i&lt;size; i++){
        		 cout  &lt;&lt; arr[i];
        		ret[i]=arr[i];

        		 cout &lt;&lt; ret[i] &lt;&lt;&quot; ?=&quot; &lt;&lt; arr[i] &lt;&lt; endl;
        	}
        	return ret;
        }

  };
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/160683/wohl-ärger-mit-zeigern</link><generator>RSS for Node</generator><lastBuildDate>Fri, 11 Sep 2026 09:10:25 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/160683.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 28 Sep 2006 12:18:02 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Wohl Ärger mit Zeigern..... on Thu, 28 Sep 2006 12:18:02 GMT]]></title><description><![CDATA[<p>Hi zusammen,<br />
ich hab nen Zeigerproblem mit der folgenden Klasse, es wird, wenn ich eine Zuweisung mache:</p>
<pre><code class="language-cpp">IntArray temp = getDomainAt(b,i);

     int* myArr;
     myArr = temp.getArray(); 
[cpp]

ab und zu mal eine 5 in beiden arrays, myArr und temp.arr zu 27. Hat jemand ne Idee warum?

[cpp]

  class IntArray {
       public:
        int* arr;
        int size;

        IntArray() {
        	arr = NULL;
        	size = 0;
        }

        ~IntArray(){
        	//std::cout &lt;&lt; &quot;IntArray Destruktor\n&quot;;
        	//if (arr!= NULL) delete[] arr;
        	//std::cout &lt;&lt; &quot;Intarray DEstruktor Ende\n&quot;;
        }

        IntArray(const IntArray&amp; p){
        	size=p.size;
        	arr = new int[size];
        	for (int i=0; i&lt;size; i++)
        		arr[i] =  p.arr[i];
        }

		  IntArray const&amp; operator=(IntArray const&amp; p) {
            size=p.size;
        	arr = new int[size];
        	for (int i=0; i&lt;size; i++)
        		arr[i] =  p.arr[i];
        	return *this;
        }

        int* getArray() {
        	if (arr == NULL) return NULL;
        	int* ret = new int[size];
        	for (int i=0; i&lt;size; i++){
        		 cout  &lt;&lt; arr[i];
        		ret[i]=arr[i];

        		 cout &lt;&lt; ret[i] &lt;&lt;&quot; ?=&quot; &lt;&lt; arr[i] &lt;&lt; endl;
        	}
        	return ret;
        }

  };
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1145909</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1145909</guid><dc:creator><![CDATA[EvilEpsilon]]></dc:creator><pubDate>Thu, 28 Sep 2006 12:18:02 GMT</pubDate></item><item><title><![CDATA[Reply to Wohl Ärger mit Zeigern..... on Thu, 28 Sep 2006 12:51:01 GMT]]></title><description><![CDATA[<p>Hi,</p>
<p>würde Dir gerne helfen, aber ich versthe den Satz nicht:</p>
<p>EvilEpsilon schrieb:</p>
<blockquote>
<p>...<br />
ab und zu mal eine 5 in beiden arrays, myArr und temp.arr zu 27....</p>
</blockquote>
<p>Gruß,</p>
<p>Simon2.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1145921</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1145921</guid><dc:creator><![CDATA[Simon2]]></dc:creator><pubDate>Thu, 28 Sep 2006 12:51:01 GMT</pubDate></item><item><title><![CDATA[Reply to Wohl Ärger mit Zeigern..... on Thu, 28 Sep 2006 13:11:43 GMT]]></title><description><![CDATA[<p>Simon2 schrieb:</p>
<blockquote>
<p>Hi,</p>
<p>würde Dir gerne helfen, aber ich versthe den Satz nicht:</p>
<p>EvilEpsilon schrieb:</p>
<blockquote>
<p>...<br />
ab und zu mal eine 5 in beiden arrays, myArr und temp.arr zu 27....</p>
</blockquote>
</blockquote>
<p>Ich glaube er meint, dass er in beiden Arrays (myArr und tmp.arr) jeweils ne 5 stehen hat, die dann in beiden Arrays zu ner 27 werden. Warum auch immer.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1145935</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1145935</guid><dc:creator><![CDATA[DarthZiu]]></dc:creator><pubDate>Thu, 28 Sep 2006 13:11:43 GMT</pubDate></item><item><title><![CDATA[Reply to Wohl Ärger mit Zeigern..... on Thu, 28 Sep 2006 14:30:23 GMT]]></title><description><![CDATA[<p>Aha. Kann sein.</p>
<p>Um das analysieren zu können, müssten wir wohl &quot;ein wenig mehr main sehen&quot;.<br />
Ich finde allerdings die &quot;IntArray&quot;-Klasse ein wenig seltsam: Woher bekommt das Objekt denn seinen Speicher ?</p>
<p>Im StandardCtor wird er auf &quot;nix&quot; gesetzt, im CpyCtor erzeugt er sich selbst welchen. D.h. aber, dass ein Objekt, dass &quot;per Copy erzeugt&quot; wurde, ein delete[] durchführen muß, während ein &quot;per Standard konstruiertes&quot; das (vermutlich) nicht darf.<br />
Wahrscheinlich kommt daher auch Deine &quot;DTor-Verwirrung&quot; ... <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 />
Außerdem wird beim operator=() dann noch Speicher geholt, ohne dass der vorher benutzte freigegeben wird (hat vermutlich dieselbe Ursache: Man weiß ja nicht, ob das Objekt so oder so erzeugt wurde).<br />
Letztlich sollte die Klasse den Speicher komplett kapseln (non-public-Attribute und keine &quot;Speicherersetzung von Außen&quot; möglich).</p>
<p>Ich denke, EvilEpsilon erzeugt mehrere IntArrays, die auf denselben Speicher verweisen und manipuliert den dann (evtl. ungewollt) von Außen auch noch.</p>
<p>NACHTRAG: Falls Du das nicht nur zum Lernen verwenden möchtest, sondern tatsächlich irgendwo &quot;brauchst&quot;, wäre std::vector&lt;int&gt; bestimmt die bessere Wahl...</p>
<p>Gruß,</p>
<p>Simon2.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1145939</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1145939</guid><dc:creator><![CDATA[Simon2]]></dc:creator><pubDate>Thu, 28 Sep 2006 14:30:23 GMT</pubDate></item></channel></rss>