<?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[Dynamische Array Container Implementierung]]></title><description><![CDATA[<p>Hi,<br />
ich soll einen Dynamischen Array Container Implementieren und komme einfach net weiter <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /> . Bitte um Hilfe Tipp und Tricks sehr erwünscht.</p>
<p>DynArray:</p>
<pre><code class="language-cpp">#pragma once
#pragma warning(disable:4996)

#include &lt;algorithm&gt;

template &lt; typename T &gt;
class DynArray
{
	T* pfirst, *plast, *pend;
public:
	friend class TestDynArray;

	/*	DynArrays default constructor
		Constructs an empty DynArray, with no content and a size of zero.
	*/
	DynArray(void)
	{
		pfirst = plast = pend = NULL;    
	}

	/*	Repetitive sequence constructor
		Initializes the DynArray with its content set to a repetition, n times, of copies 
		of value. The constructed container has a size and capacity of n.
	*/
	DynArray( size_t n, const T&amp; val=T() )
	{
		pfirst = new T[n]; // calls T() n times
		plast = pend = pfirst+n;
		std::fill( pfirst, plast, val );
	}

	/*	DynArray copy-constructor

	*/
	DynArray( const DynArray&amp; d )
	{
		size_t _capacity = d.pend-d.pfirst;
		pfirst = new T[_capacity];
		plast  = std::copy( d.pfirst, d.pfirst+(d.plast-d.pfirst), pfirst );
		pend   = pfirst + _capacity;
	}

	/*	DynArray destructor
		Destructs the container object. This calls each of the contained element's 
		destructors, and deallocates all the storage capacity allocated by the DynArray.
	*/
	~DynArray(void){
		T typ;
		for(unsigned int i=0;i&lt;size();i++)
		{
			typ.~T();	
		}
		pfirst = plast = pend = NULL;
		delete [] pfirst;
		delete [] plast;
		delete [] pend;	
	}

	/*	Return size of allocated storage capacity
		Returns the size of the allocated storage space for the elements of the 
		DynArray container.
	*/
	size_t capacity() const { return pend-pfirst; }

	/*	Return size
		Returns the number of elements in the DynArray container.
		This is the number of actual objects held in the DynArray, 
		which is not necessarily equal to its storage capacity. 
	*/
	size_t size() const { return plast-pfirst; }	

	/*	Clear content
		All the elements of the DynArray are dropped by setting size to zero.
		Notice that no element destructor is called and no memory is deallocated
	*/
	void clear() { plast = pfirst; }

	/* Add element at the end
	   Adds a new element at the end of the DynArray, after its current last element. 
	   The content of this new element is initialized to a copy of x.
	   This effectively increases the DynArray size by one, which causes a reallocation 
	   of the internal allocated storage if the DynArray size was equal to the DynArray
	   capacity before the call.
	   During reallocation the new storage capacity should be set to 
		newCapacity = 1					if capacity()=0
		newCapacity = capacity()*3/2+1	if capacity()&gt;0
	  */
	void push_back( const T&amp; val ){
	size_t sz = size()+1;
	size_t t = size();
	T* pdest = new T[sz];
	std::copy(pfirst,plast,pdest);
	delete [] pfirst;
	pfirst = pdest;
	plast = pend = plast+sz;                //&lt;---geht net :-(
	std::fill(pfirst+t,plast,val);
	*if(capacity() == 0){}
	if(capacity() &gt; 0){}*/
	}

	/*	Access element
		Returns a reference to the element at position n in the DynArray container.
	*/
	T&amp; operator[]( size_t i ) { return pfirst[i]; }
	const T&amp; operator[]( size_t i ) const { return pfirst[i]; }

	/*	Request a change in capacity
		Requests that the capacity of the allocated storage space for the elements of 
		the DynArray container be at least enough to hold n elements.
		Notice that the parameter n informs of a minimum, so the resulting capacity may be 
		any capacity equal or larger than this.
		A call to this function never affects the elements contained in the DynArray, 
		nor the DynArray size
	*/
	void reserve ( size_t n )
	{
		if(pend&lt;pfirst+n) 
		{
			T* ptmp = new T[n];	// calls n times T's standard contructor
			plast = std::copy( pfirst, plast, ptmp );
			delete [] pfirst; // calls capacity times T's destructor
			pfirst = ptmp; 
			pend = pfirst+n;
		}
	}

	/*	Change size
		Resizes the DynArray to contain sz elements.
		If sz is smaller than the current DynArray size, the content is reduced to its 
		first sz elements, the rest being dropped. If sz is greater than the current 
		DynArray size, the content is expanded by inserting at the end as many copies 
		of val as needed to reach a size of sz elements. This may cause a reallocation.
		Notice that this function changes the actual content of the DynArray by inserting 
		or erasing elements from the DynArray; it does not only change its storage capacity.
	*/

	void resize ( size_t sz, T val = T() ){

																	//sz = 4
		if (sz &gt; size()){											//h  = 1
																	//size() oder b = 1
			size_t b= size();										// e = 2
			T *pdest= new T[sz]; // calls T() n times				//neu int [2]
			std::copy(pfirst,plast,pdest);							//pdest size() = 1
			delete [] pfirst;										//delete [] pfirst ;pfirst = pdest
			pfirst = pdest;
			plast = pend = pfirst+sz;								
			std::fill(pfirst+b,plast, val);                         
		}
		if (sz &lt; size()){

			T* pdest = new T[sz];
			std::copy(pfirst,plast,pdest);
			delete [] pfirst;
			pfirst = pdest;
			plast = pend = pfirst+sz;
		}

	}

	/*	Copy DynArray content
		Assigns a copy of DynArray d as the new content for the DynArray object.
		The elements contained in the DynArray object before the call are dropped, and 
		replaced by copies of those in DynArray x, if any. After a call to this member 
		function, both the DynArray object and DynArray x will have the same size and 
		compare equal to each other.
		Return value *this
	*/
		DynArray&amp; operator=( const DynArray &amp;m ){ 
  if (this != &amp;m)  //oder if (*this != &amp;m) 
  { 
	/*pfirst = m.pfirst;*/  //&lt;----------warum geht das net 
  } 
  return *this; //Referenz auf das Objekt selbst zurückgeben 
}

};
</code></pre>
<p>Test-Unit header:</p>
<pre><code class="language-cpp">#pragma once

class Any {
	int val;
	static int count;
public:
	Any() { val=0; ++count; }
	Any( int val0 ) { val = val0; ++count; }
	Any( const Any&amp; a ) { val = a.val; ++count; }
	~Any() { --count; }
	static void clear(){count=0;}
	static size_t getCount() { return count; }
	bool operator==( const Any&amp;  a ) const { return (val==a.val); }
	bool operator!=( const Any&amp;  a ) const { return (val!=a.val); }
};

class TestDynArray
{
public:
	TestDynArray(void);
	~TestDynArray(void);
	void test_all();
	void test_ctors(); // size()and capacity() are also tested
	void test_dtor();
	void test_clear();
	void test_IndexOperator();
	void test_assignment();
	void test_push_back();
	void test_reserve();
	void test_resize();
};
</code></pre>
<p>Test-Unit cpp:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;iomanip&gt;
#include &quot;DynArray.h&quot;
#include &quot;TestDynArray.h&quot;
#include &lt;vector&gt;
#pragma warning(disable : 4996)

using namespace std;
#define fieldWidth 55

int Any::count = 0;

TestDynArray::TestDynArray(void)
{
	cout &lt;&lt; left;
}

TestDynArray::~TestDynArray(void)
{
}

void TestDynArray::test_all()
{
	std::cout &lt;&lt; &quot;\n************** TEST class DynArray ************************\n&quot;;
	test_ctors(); // size()and capacity() are also tested
	test_dtor();
	test_clear();
	test_IndexOperator();
	test_assignment();
	/*test_push_back();*/
	test_reserve();
	test_resize();
	/*test_eraseRange();*/
	cout&lt;&lt;endl;
}

void TestDynArray::test_ctors() // size()and capacity() are also tested
{
	int cnt = Any::getCount();
	bool pass = true;
	DynArray&lt;int&gt; d;
	if( d.size()!=0 ) pass=false;
	if( d.capacity()!=0 ) pass=false;
	cout&lt;&lt; setw(fieldWidth)  &lt;&lt; &quot;1  - DynArray()&quot; &lt;&lt; ( (pass) ? &quot;PASS&quot; : &quot;FAIL&quot; ) &lt;&lt; endl;

	pass = true;
	DynArray&lt;int&gt; d1(3,2);
	if( d1.size()    !=3 ) pass=false;
	if( d1.capacity()!=3 ) pass=false;
	if( d1.pfirst[0] !=2 ) pass=false;
	if( d1.pfirst[1] !=2 ) pass=false;
	if( d1.pfirst[2] !=2 ) pass=false;
	DynArray&lt;Any&gt; d2(3);
	if( d2.size()    !=3 ) pass=false;
	if( d2.capacity()!=3 ) pass=false;
	if( d2.pfirst[0] != Any() ) pass=false;
	if( d2.pfirst[1] != Any() ) pass=false;
	if( d2.pfirst[2] != Any() ) pass=false;
	cout&lt;&lt; setw(fieldWidth)  &lt;&lt; &quot;2  - DynArray(size_t n,const T&amp; val=T())&quot; &lt;&lt; ( (pass) ? &quot;PASS&quot; : &quot;FAIL&quot; ) &lt;&lt; endl;

	pass = true;
	DynArray&lt;int&gt; d3(d);
	if( d3.size()!=0 ) pass=false;
	if( d3.capacity()!=0 ) pass=false;
	DynArray&lt;Any&gt; d4(d2);
	if( d4.size()    !=3 ) pass=false;
	if( d4.capacity()!=3 ) pass=false;
	if( d4.pfirst[0] != Any() ) pass=false;
	if( d4.pfirst[1] != Any() ) pass=false;
	if( d4.pfirst[2] != Any() ) pass=false;
	cout&lt;&lt; setw(fieldWidth)  &lt;&lt; &quot;3  - DynArray(const DynArray&amp; d)&quot; &lt;&lt; ( (pass) ? &quot;PASS&quot; : &quot;FAIL&quot; ) &lt;&lt; endl;
}

void TestDynArray::test_dtor()
{
	int cnt = Any::getCount();
	bool pass = true;
	{
		DynArray&lt;Any&gt; d(3);
	}
	if( Any::getCount()!=cnt) pass=false;
	cnt = Any::getCount();
	DynArray&lt;Any&gt; d(3);
	d.~DynArray();
	if( Any::getCount()!=cnt) pass=false;
	if( d.size()!=0 ) pass=false;
	if( d.capacity()!=0 ) pass=false;
	cout&lt;&lt; setw(fieldWidth)  &lt;&lt; &quot;4  - ~DynArray()&quot; &lt;&lt; ( (pass) ? &quot;PASS&quot; : &quot;FAIL&quot; ) &lt;&lt; endl;
}

void TestDynArray::test_clear()
{
	int cnt = Any::getCount();
	bool pass = true;
	DynArray&lt;Any&gt; d(4);
	if( d.size()!=4 )		pass=false;
	if( d.capacity()!=4 )	pass=false;
	if( Any::getCount()!=cnt+4) pass=false;
	d.clear();
	if( d.size()!=0 )		pass=false;
	if( d.capacity()!=4 )	pass=false;
	if( Any::getCount()!=cnt+4) pass=false;
	cout&lt;&lt; setw(fieldWidth)  &lt;&lt; &quot;5  - clear()&quot; &lt;&lt; ( (pass) ? &quot;PASS&quot; : &quot;FAIL&quot; ) &lt;&lt; endl;
}

void TestDynArray::test_IndexOperator()
{
	int cnt = Any::getCount();
	bool pass = true;
	DynArray&lt;int&gt; d(2,3);
	if( d[0]!=3 )		pass=false;
	if( d[1]!=3 )		pass=false;
	d[0]=1;
	d[1]=2;
	if( d[0]!=1 )		pass=false;
	if( d[1]!=2 )		pass=false;
	cout&lt;&lt; setw(fieldWidth)  &lt;&lt; &quot;6  - operator[]( size_t i )&quot; &lt;&lt; ( (pass) ? &quot;PASS&quot; : &quot;FAIL&quot; ) &lt;&lt; endl;

	pass = true;
	const DynArray&lt;int&gt; d1(2,4);
	if( d1[0]!=4 )		pass=false;
	if( d1[1]!=4 )		pass=false;
	cout&lt;&lt; setw(fieldWidth)  &lt;&lt; &quot;7  - operator[]( size_t i ) const&quot; &lt;&lt; ( (pass) ? &quot;PASS&quot; : &quot;FAIL&quot; ) &lt;&lt; endl;
}

void TestDynArray::test_assignment()
{
	int cnt = Any::getCount();
	bool pass = true;
	cnt = Any::getCount();
	DynArray&lt;Any&gt; d1(1,Any(2)), d2(2,Any(3));
	if( Any::getCount()!=cnt+3) pass=false;
	d2 = d1;
	if( d2.size()!=1 )			pass=false; // d2 size ok?
	if( Any::getCount()!=cnt+2) pass=false; // d2 destructor called?
	if( d2.pfirst[0]!=Any(2) )	pass=false; // elements copied to d2?
	d1[0] = Any(4);							
	if( d2.pfirst[0]!=Any(2) )	pass=false; // deep copy implemented
	const DynArray&lt;Any&gt; d0;	
	d1 = d0;								// check for const-correctness
	if( Any::getCount()!=cnt+1) pass=false; 
	d2 = d2;								
	if( d2.size()!=1 )			pass=false; // correct handling of self-assignment?
	if( d2[0]!=Any(2) )			pass=false; // correct handling of self-assignment?
	cout&lt;&lt; setw(fieldWidth)  &lt;&lt; &quot;8  - operator=( const DynArray&amp; d )&quot; &lt;&lt; ( (pass) ? &quot;PASS&quot; : &quot;FAIL&quot; ) &lt;&lt; endl;
}

//void TestDynArray::test_push_back()//Fehler beim ausführen?
//{
//	int cnt = Any::getCount();
//	bool pass = true;
//	DynArray&lt;Any&gt; d;
//	d.push_back(Any(0));
//	if( Any::getCount()!=cnt+1) pass=false;
//	if( d.size()!=1 )			pass=false;
//	if( d.capacity()!=1 )		pass=false;
//	if( d.pfirst[0]!=Any(0) )	pass=false;
//	int cap = d.capacity();
//	d.push_back(Any(1));
//	if( Any::getCount()!=cnt+2) pass=false;
//	if( d.size()!=2 )			pass=false;
//	if( d.capacity()!=cap*3/2+1 )		pass=false;
//	if( d.pfirst[0]!=Any(0) )	pass=false;
//	if( d.pfirst[1]!=Any(1) )	pass=false;
//	cap = d.capacity();
//	d.push_back(Any(2));
//	if( Any::getCount()!=d.capacity())	pass=false;
//	if( d.size()!=3 )			pass=false;
//	if( d.capacity()!=cap*3/2+1 )		pass=false;
//	if( d.pfirst[0]!=Any(0) )	pass=false;
//	if( d.pfirst[1]!=Any(1) )	pass=false;
//	if( d.pfirst[2]!=Any(2) )	pass=false;
//	cout&lt;&lt; setw(fieldWidth)  &lt;&lt; &quot;9  - push_back(const T&amp; elem)&quot; &lt;&lt; ( (pass) ? &quot;PASS&quot; : &quot;FAIL&quot; ) &lt;&lt; endl;
//}

void TestDynArray::test_reserve()
{
	int cnt = Any::getCount();
	bool pass = true;
	DynArray&lt;int&gt; d;
	d.reserve(3);
	if( d.capacity()&lt;3 )		pass=false;
	if( d.size()!=0 )			pass=false;

	DynArray&lt;int&gt; d1(3,2);
	d1[0]=0;
	d1[1]=1;
	d1.reserve(4);
	if( d1.capacity()&lt;4 )		pass=false;
	if( d1.size()!=3 )			pass=false;
	if( d1.pfirst[0]!=0 )		pass=false;
	if( d1.pfirst[1]!=1 )		pass=false;
	if( d1.pfirst[2]!=2 )		pass=false;

	d1.reserve(2);
	if( d1.capacity()&lt;3 )		pass=false;
	if( d1.size()!=3 )			pass=false;
	if( d1.pfirst[0]!=0 )		pass=false;
	if( d1.pfirst[1]!=1 )		pass=false;
	if( d1.pfirst[2]!=2 )		pass=false;
	cout &lt;&lt; setw(fieldWidth)  &lt;&lt; &quot;10 - reserve (size_t n)&quot; &lt;&lt; ( (pass) ? &quot;PASS&quot; : &quot;FAIL&quot; ) &lt;&lt; endl;
}

void TestDynArray::test_resize()
{
	Any::clear();//warum ist count von any 6???  Any::clear(); ist von mir 
                     //provisorisch hinzugefügt wurden, sonnst Failt resize();
	int cnt = Any::getCount();
	bool pass = true;

	DynArray&lt;Any&gt; d;
	d.resize(2);
	if( Any::getCount()!=d.capacity() )	pass=false;
	if( d.capacity()&lt;2 )		pass=false;
	if( d.size()!=2 )			pass=false;
	if( d.pfirst[0]!=Any() )	pass=false;
	if( d.pfirst[1]!=Any() )	pass=false;

	d[0] = Any(0);
	d[1] = Any(1);
	d.resize(4,Any(3));
	if( Any::getCount()!=d.capacity() )	pass=false;
	if( d.capacity()&lt;4 )		pass=false;
	if( d.size()!=4 )			pass=false;
	if( d.pfirst[0]!=Any(0) )	pass=false;
	if( d.pfirst[1]!=Any(1) )	pass=false;
	if( d.pfirst[2]!=Any(3) )	pass=false;
	if( d.pfirst[3]!=Any(3) )	pass=false;

	d.resize(3,Any(4));
	if( Any::getCount()!=d.capacity() )	pass=false;
	if( d.capacity()&lt;3 )		pass=false;
	if( d.size()!=3 )			pass=false;
	if( d.pfirst[0]!=Any(0) )	pass=false;
	if( d.pfirst[1]!=Any(1) )	pass=false;
	if( d.pfirst[2]!=Any(3) )	pass=false;
	cout &lt;&lt; setw(fieldWidth)  &lt;&lt; &quot;11 - resize(size_t n)&quot; &lt;&lt; ( (pass) ? &quot;PASS&quot; : &quot;FAIL&quot; ) &lt;&lt; endl;
}
</code></pre>
<p><img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/topic/308179/dynamische-array-container-implementierung</link><generator>RSS for Node</generator><lastBuildDate>Thu, 06 Aug 2026 03:00:25 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/308179.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 17 Sep 2012 13:11:27 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Dynamische Array Container Implementierung on Mon, 17 Sep 2012 13:11:27 GMT]]></title><description><![CDATA[<p>Hi,<br />
ich soll einen Dynamischen Array Container Implementieren und komme einfach net weiter <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /> . Bitte um Hilfe Tipp und Tricks sehr erwünscht.</p>
<p>DynArray:</p>
<pre><code class="language-cpp">#pragma once
#pragma warning(disable:4996)

#include &lt;algorithm&gt;

template &lt; typename T &gt;
class DynArray
{
	T* pfirst, *plast, *pend;
public:
	friend class TestDynArray;

	/*	DynArrays default constructor
		Constructs an empty DynArray, with no content and a size of zero.
	*/
	DynArray(void)
	{
		pfirst = plast = pend = NULL;    
	}

	/*	Repetitive sequence constructor
		Initializes the DynArray with its content set to a repetition, n times, of copies 
		of value. The constructed container has a size and capacity of n.
	*/
	DynArray( size_t n, const T&amp; val=T() )
	{
		pfirst = new T[n]; // calls T() n times
		plast = pend = pfirst+n;
		std::fill( pfirst, plast, val );
	}

	/*	DynArray copy-constructor

	*/
	DynArray( const DynArray&amp; d )
	{
		size_t _capacity = d.pend-d.pfirst;
		pfirst = new T[_capacity];
		plast  = std::copy( d.pfirst, d.pfirst+(d.plast-d.pfirst), pfirst );
		pend   = pfirst + _capacity;
	}

	/*	DynArray destructor
		Destructs the container object. This calls each of the contained element's 
		destructors, and deallocates all the storage capacity allocated by the DynArray.
	*/
	~DynArray(void){
		T typ;
		for(unsigned int i=0;i&lt;size();i++)
		{
			typ.~T();	
		}
		pfirst = plast = pend = NULL;
		delete [] pfirst;
		delete [] plast;
		delete [] pend;	
	}

	/*	Return size of allocated storage capacity
		Returns the size of the allocated storage space for the elements of the 
		DynArray container.
	*/
	size_t capacity() const { return pend-pfirst; }

	/*	Return size
		Returns the number of elements in the DynArray container.
		This is the number of actual objects held in the DynArray, 
		which is not necessarily equal to its storage capacity. 
	*/
	size_t size() const { return plast-pfirst; }	

	/*	Clear content
		All the elements of the DynArray are dropped by setting size to zero.
		Notice that no element destructor is called and no memory is deallocated
	*/
	void clear() { plast = pfirst; }

	/* Add element at the end
	   Adds a new element at the end of the DynArray, after its current last element. 
	   The content of this new element is initialized to a copy of x.
	   This effectively increases the DynArray size by one, which causes a reallocation 
	   of the internal allocated storage if the DynArray size was equal to the DynArray
	   capacity before the call.
	   During reallocation the new storage capacity should be set to 
		newCapacity = 1					if capacity()=0
		newCapacity = capacity()*3/2+1	if capacity()&gt;0
	  */
	void push_back( const T&amp; val ){
	size_t sz = size()+1;
	size_t t = size();
	T* pdest = new T[sz];
	std::copy(pfirst,plast,pdest);
	delete [] pfirst;
	pfirst = pdest;
	plast = pend = plast+sz;                //&lt;---geht net :-(
	std::fill(pfirst+t,plast,val);
	*if(capacity() == 0){}
	if(capacity() &gt; 0){}*/
	}

	/*	Access element
		Returns a reference to the element at position n in the DynArray container.
	*/
	T&amp; operator[]( size_t i ) { return pfirst[i]; }
	const T&amp; operator[]( size_t i ) const { return pfirst[i]; }

	/*	Request a change in capacity
		Requests that the capacity of the allocated storage space for the elements of 
		the DynArray container be at least enough to hold n elements.
		Notice that the parameter n informs of a minimum, so the resulting capacity may be 
		any capacity equal or larger than this.
		A call to this function never affects the elements contained in the DynArray, 
		nor the DynArray size
	*/
	void reserve ( size_t n )
	{
		if(pend&lt;pfirst+n) 
		{
			T* ptmp = new T[n];	// calls n times T's standard contructor
			plast = std::copy( pfirst, plast, ptmp );
			delete [] pfirst; // calls capacity times T's destructor
			pfirst = ptmp; 
			pend = pfirst+n;
		}
	}

	/*	Change size
		Resizes the DynArray to contain sz elements.
		If sz is smaller than the current DynArray size, the content is reduced to its 
		first sz elements, the rest being dropped. If sz is greater than the current 
		DynArray size, the content is expanded by inserting at the end as many copies 
		of val as needed to reach a size of sz elements. This may cause a reallocation.
		Notice that this function changes the actual content of the DynArray by inserting 
		or erasing elements from the DynArray; it does not only change its storage capacity.
	*/

	void resize ( size_t sz, T val = T() ){

																	//sz = 4
		if (sz &gt; size()){											//h  = 1
																	//size() oder b = 1
			size_t b= size();										// e = 2
			T *pdest= new T[sz]; // calls T() n times				//neu int [2]
			std::copy(pfirst,plast,pdest);							//pdest size() = 1
			delete [] pfirst;										//delete [] pfirst ;pfirst = pdest
			pfirst = pdest;
			plast = pend = pfirst+sz;								
			std::fill(pfirst+b,plast, val);                         
		}
		if (sz &lt; size()){

			T* pdest = new T[sz];
			std::copy(pfirst,plast,pdest);
			delete [] pfirst;
			pfirst = pdest;
			plast = pend = pfirst+sz;
		}

	}

	/*	Copy DynArray content
		Assigns a copy of DynArray d as the new content for the DynArray object.
		The elements contained in the DynArray object before the call are dropped, and 
		replaced by copies of those in DynArray x, if any. After a call to this member 
		function, both the DynArray object and DynArray x will have the same size and 
		compare equal to each other.
		Return value *this
	*/
		DynArray&amp; operator=( const DynArray &amp;m ){ 
  if (this != &amp;m)  //oder if (*this != &amp;m) 
  { 
	/*pfirst = m.pfirst;*/  //&lt;----------warum geht das net 
  } 
  return *this; //Referenz auf das Objekt selbst zurückgeben 
}

};
</code></pre>
<p>Test-Unit header:</p>
<pre><code class="language-cpp">#pragma once

class Any {
	int val;
	static int count;
public:
	Any() { val=0; ++count; }
	Any( int val0 ) { val = val0; ++count; }
	Any( const Any&amp; a ) { val = a.val; ++count; }
	~Any() { --count; }
	static void clear(){count=0;}
	static size_t getCount() { return count; }
	bool operator==( const Any&amp;  a ) const { return (val==a.val); }
	bool operator!=( const Any&amp;  a ) const { return (val!=a.val); }
};

class TestDynArray
{
public:
	TestDynArray(void);
	~TestDynArray(void);
	void test_all();
	void test_ctors(); // size()and capacity() are also tested
	void test_dtor();
	void test_clear();
	void test_IndexOperator();
	void test_assignment();
	void test_push_back();
	void test_reserve();
	void test_resize();
};
</code></pre>
<p>Test-Unit cpp:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;iomanip&gt;
#include &quot;DynArray.h&quot;
#include &quot;TestDynArray.h&quot;
#include &lt;vector&gt;
#pragma warning(disable : 4996)

using namespace std;
#define fieldWidth 55

int Any::count = 0;

TestDynArray::TestDynArray(void)
{
	cout &lt;&lt; left;
}

TestDynArray::~TestDynArray(void)
{
}

void TestDynArray::test_all()
{
	std::cout &lt;&lt; &quot;\n************** TEST class DynArray ************************\n&quot;;
	test_ctors(); // size()and capacity() are also tested
	test_dtor();
	test_clear();
	test_IndexOperator();
	test_assignment();
	/*test_push_back();*/
	test_reserve();
	test_resize();
	/*test_eraseRange();*/
	cout&lt;&lt;endl;
}

void TestDynArray::test_ctors() // size()and capacity() are also tested
{
	int cnt = Any::getCount();
	bool pass = true;
	DynArray&lt;int&gt; d;
	if( d.size()!=0 ) pass=false;
	if( d.capacity()!=0 ) pass=false;
	cout&lt;&lt; setw(fieldWidth)  &lt;&lt; &quot;1  - DynArray()&quot; &lt;&lt; ( (pass) ? &quot;PASS&quot; : &quot;FAIL&quot; ) &lt;&lt; endl;

	pass = true;
	DynArray&lt;int&gt; d1(3,2);
	if( d1.size()    !=3 ) pass=false;
	if( d1.capacity()!=3 ) pass=false;
	if( d1.pfirst[0] !=2 ) pass=false;
	if( d1.pfirst[1] !=2 ) pass=false;
	if( d1.pfirst[2] !=2 ) pass=false;
	DynArray&lt;Any&gt; d2(3);
	if( d2.size()    !=3 ) pass=false;
	if( d2.capacity()!=3 ) pass=false;
	if( d2.pfirst[0] != Any() ) pass=false;
	if( d2.pfirst[1] != Any() ) pass=false;
	if( d2.pfirst[2] != Any() ) pass=false;
	cout&lt;&lt; setw(fieldWidth)  &lt;&lt; &quot;2  - DynArray(size_t n,const T&amp; val=T())&quot; &lt;&lt; ( (pass) ? &quot;PASS&quot; : &quot;FAIL&quot; ) &lt;&lt; endl;

	pass = true;
	DynArray&lt;int&gt; d3(d);
	if( d3.size()!=0 ) pass=false;
	if( d3.capacity()!=0 ) pass=false;
	DynArray&lt;Any&gt; d4(d2);
	if( d4.size()    !=3 ) pass=false;
	if( d4.capacity()!=3 ) pass=false;
	if( d4.pfirst[0] != Any() ) pass=false;
	if( d4.pfirst[1] != Any() ) pass=false;
	if( d4.pfirst[2] != Any() ) pass=false;
	cout&lt;&lt; setw(fieldWidth)  &lt;&lt; &quot;3  - DynArray(const DynArray&amp; d)&quot; &lt;&lt; ( (pass) ? &quot;PASS&quot; : &quot;FAIL&quot; ) &lt;&lt; endl;
}

void TestDynArray::test_dtor()
{
	int cnt = Any::getCount();
	bool pass = true;
	{
		DynArray&lt;Any&gt; d(3);
	}
	if( Any::getCount()!=cnt) pass=false;
	cnt = Any::getCount();
	DynArray&lt;Any&gt; d(3);
	d.~DynArray();
	if( Any::getCount()!=cnt) pass=false;
	if( d.size()!=0 ) pass=false;
	if( d.capacity()!=0 ) pass=false;
	cout&lt;&lt; setw(fieldWidth)  &lt;&lt; &quot;4  - ~DynArray()&quot; &lt;&lt; ( (pass) ? &quot;PASS&quot; : &quot;FAIL&quot; ) &lt;&lt; endl;
}

void TestDynArray::test_clear()
{
	int cnt = Any::getCount();
	bool pass = true;
	DynArray&lt;Any&gt; d(4);
	if( d.size()!=4 )		pass=false;
	if( d.capacity()!=4 )	pass=false;
	if( Any::getCount()!=cnt+4) pass=false;
	d.clear();
	if( d.size()!=0 )		pass=false;
	if( d.capacity()!=4 )	pass=false;
	if( Any::getCount()!=cnt+4) pass=false;
	cout&lt;&lt; setw(fieldWidth)  &lt;&lt; &quot;5  - clear()&quot; &lt;&lt; ( (pass) ? &quot;PASS&quot; : &quot;FAIL&quot; ) &lt;&lt; endl;
}

void TestDynArray::test_IndexOperator()
{
	int cnt = Any::getCount();
	bool pass = true;
	DynArray&lt;int&gt; d(2,3);
	if( d[0]!=3 )		pass=false;
	if( d[1]!=3 )		pass=false;
	d[0]=1;
	d[1]=2;
	if( d[0]!=1 )		pass=false;
	if( d[1]!=2 )		pass=false;
	cout&lt;&lt; setw(fieldWidth)  &lt;&lt; &quot;6  - operator[]( size_t i )&quot; &lt;&lt; ( (pass) ? &quot;PASS&quot; : &quot;FAIL&quot; ) &lt;&lt; endl;

	pass = true;
	const DynArray&lt;int&gt; d1(2,4);
	if( d1[0]!=4 )		pass=false;
	if( d1[1]!=4 )		pass=false;
	cout&lt;&lt; setw(fieldWidth)  &lt;&lt; &quot;7  - operator[]( size_t i ) const&quot; &lt;&lt; ( (pass) ? &quot;PASS&quot; : &quot;FAIL&quot; ) &lt;&lt; endl;
}

void TestDynArray::test_assignment()
{
	int cnt = Any::getCount();
	bool pass = true;
	cnt = Any::getCount();
	DynArray&lt;Any&gt; d1(1,Any(2)), d2(2,Any(3));
	if( Any::getCount()!=cnt+3) pass=false;
	d2 = d1;
	if( d2.size()!=1 )			pass=false; // d2 size ok?
	if( Any::getCount()!=cnt+2) pass=false; // d2 destructor called?
	if( d2.pfirst[0]!=Any(2) )	pass=false; // elements copied to d2?
	d1[0] = Any(4);							
	if( d2.pfirst[0]!=Any(2) )	pass=false; // deep copy implemented
	const DynArray&lt;Any&gt; d0;	
	d1 = d0;								// check for const-correctness
	if( Any::getCount()!=cnt+1) pass=false; 
	d2 = d2;								
	if( d2.size()!=1 )			pass=false; // correct handling of self-assignment?
	if( d2[0]!=Any(2) )			pass=false; // correct handling of self-assignment?
	cout&lt;&lt; setw(fieldWidth)  &lt;&lt; &quot;8  - operator=( const DynArray&amp; d )&quot; &lt;&lt; ( (pass) ? &quot;PASS&quot; : &quot;FAIL&quot; ) &lt;&lt; endl;
}

//void TestDynArray::test_push_back()//Fehler beim ausführen?
//{
//	int cnt = Any::getCount();
//	bool pass = true;
//	DynArray&lt;Any&gt; d;
//	d.push_back(Any(0));
//	if( Any::getCount()!=cnt+1) pass=false;
//	if( d.size()!=1 )			pass=false;
//	if( d.capacity()!=1 )		pass=false;
//	if( d.pfirst[0]!=Any(0) )	pass=false;
//	int cap = d.capacity();
//	d.push_back(Any(1));
//	if( Any::getCount()!=cnt+2) pass=false;
//	if( d.size()!=2 )			pass=false;
//	if( d.capacity()!=cap*3/2+1 )		pass=false;
//	if( d.pfirst[0]!=Any(0) )	pass=false;
//	if( d.pfirst[1]!=Any(1) )	pass=false;
//	cap = d.capacity();
//	d.push_back(Any(2));
//	if( Any::getCount()!=d.capacity())	pass=false;
//	if( d.size()!=3 )			pass=false;
//	if( d.capacity()!=cap*3/2+1 )		pass=false;
//	if( d.pfirst[0]!=Any(0) )	pass=false;
//	if( d.pfirst[1]!=Any(1) )	pass=false;
//	if( d.pfirst[2]!=Any(2) )	pass=false;
//	cout&lt;&lt; setw(fieldWidth)  &lt;&lt; &quot;9  - push_back(const T&amp; elem)&quot; &lt;&lt; ( (pass) ? &quot;PASS&quot; : &quot;FAIL&quot; ) &lt;&lt; endl;
//}

void TestDynArray::test_reserve()
{
	int cnt = Any::getCount();
	bool pass = true;
	DynArray&lt;int&gt; d;
	d.reserve(3);
	if( d.capacity()&lt;3 )		pass=false;
	if( d.size()!=0 )			pass=false;

	DynArray&lt;int&gt; d1(3,2);
	d1[0]=0;
	d1[1]=1;
	d1.reserve(4);
	if( d1.capacity()&lt;4 )		pass=false;
	if( d1.size()!=3 )			pass=false;
	if( d1.pfirst[0]!=0 )		pass=false;
	if( d1.pfirst[1]!=1 )		pass=false;
	if( d1.pfirst[2]!=2 )		pass=false;

	d1.reserve(2);
	if( d1.capacity()&lt;3 )		pass=false;
	if( d1.size()!=3 )			pass=false;
	if( d1.pfirst[0]!=0 )		pass=false;
	if( d1.pfirst[1]!=1 )		pass=false;
	if( d1.pfirst[2]!=2 )		pass=false;
	cout &lt;&lt; setw(fieldWidth)  &lt;&lt; &quot;10 - reserve (size_t n)&quot; &lt;&lt; ( (pass) ? &quot;PASS&quot; : &quot;FAIL&quot; ) &lt;&lt; endl;
}

void TestDynArray::test_resize()
{
	Any::clear();//warum ist count von any 6???  Any::clear(); ist von mir 
                     //provisorisch hinzugefügt wurden, sonnst Failt resize();
	int cnt = Any::getCount();
	bool pass = true;

	DynArray&lt;Any&gt; d;
	d.resize(2);
	if( Any::getCount()!=d.capacity() )	pass=false;
	if( d.capacity()&lt;2 )		pass=false;
	if( d.size()!=2 )			pass=false;
	if( d.pfirst[0]!=Any() )	pass=false;
	if( d.pfirst[1]!=Any() )	pass=false;

	d[0] = Any(0);
	d[1] = Any(1);
	d.resize(4,Any(3));
	if( Any::getCount()!=d.capacity() )	pass=false;
	if( d.capacity()&lt;4 )		pass=false;
	if( d.size()!=4 )			pass=false;
	if( d.pfirst[0]!=Any(0) )	pass=false;
	if( d.pfirst[1]!=Any(1) )	pass=false;
	if( d.pfirst[2]!=Any(3) )	pass=false;
	if( d.pfirst[3]!=Any(3) )	pass=false;

	d.resize(3,Any(4));
	if( Any::getCount()!=d.capacity() )	pass=false;
	if( d.capacity()&lt;3 )		pass=false;
	if( d.size()!=3 )			pass=false;
	if( d.pfirst[0]!=Any(0) )	pass=false;
	if( d.pfirst[1]!=Any(1) )	pass=false;
	if( d.pfirst[2]!=Any(3) )	pass=false;
	cout &lt;&lt; setw(fieldWidth)  &lt;&lt; &quot;11 - resize(size_t n)&quot; &lt;&lt; ( (pass) ? &quot;PASS&quot; : &quot;FAIL&quot; ) &lt;&lt; endl;
}
</code></pre>
<p><img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2251929</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2251929</guid><dc:creator><![CDATA[Broly]]></dc:creator><pubDate>Mon, 17 Sep 2012 13:11:27 GMT</pubDate></item><item><title><![CDATA[Reply to Dynamische Array Container Implementierung on Mon, 17 Sep 2012 13:32:44 GMT]]></title><description><![CDATA[<p>Dieser Thread wurde von Moderator/in <a href="http://www.c-plusplus.net/forum/u100590" rel="nofollow">Martin Richter</a> aus dem Forum <a href="http://www.c-plusplus.net/forum/f1" rel="nofollow">MFC (Visual C++)</a> in das Forum <a href="http://www.c-plusplus.net/forum/f15" rel="nofollow">C++ (auch C++0x und C++11)</a> verschoben.</p>
<p>Im Zweifelsfall bitte auch folgende Hinweise beachten:<br />
<a href="http://www.c-plusplus.net/forum/39405" rel="nofollow">C/C++ Forum :: FAQ - Sonstiges :: Wohin mit meiner Frage?</a></p>
<p><em>Dieses Posting wurde automatisch erzeugt.</em></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2251948</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2251948</guid><dc:creator><![CDATA[C++ Forumbot]]></dc:creator><pubDate>Mon, 17 Sep 2012 13:32:44 GMT</pubDate></item><item><title><![CDATA[Reply to Dynamische Array Container Implementierung on Mon, 17 Sep 2012 13:59:27 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">T* pfirst, *plast, *pend;
</code></pre>
<p>Da alles andere auch kommentiert wird, solltest du auch ein paar Worte zu den Membern verlieren.</p>
<pre><code class="language-cpp">~DynArray(void){
        T typ;
        for(unsigned int i=0;i&lt;size();i++)
        {
            typ.~T();   
        }
        pfirst = plast = pend = NULL;
        delete [] pfirst;
        delete [] plast;
        delete [] pend;   
    }
</code></pre>
<p>was stellst du dir vor, das hier passiert?</p>
<pre><code class="language-cpp">void push_back( const T&amp; val ){
	size_t sz = size()+1;
	size_t t = size();
	T* pdest = new T[sz];
	std::copy(pfirst,plast,pdest);
	delete [] pfirst;
	pfirst = pdest;
	plast = pend = plast+sz;                //&lt;---geht net :-(
	std::fill(pfirst+t,plast,val);
	*if(capacity() == 0){}
	if(capacity() &gt; 0){}*/
	}
</code></pre>
<p>&quot;geht nicht&quot; ist eine recht unpräzise Beschreibung.</p>
<pre><code class="language-cpp">DynArray&amp; operator=( const DynArray &amp;m ){ 
  if (this != &amp;m)  //oder if (*this != &amp;m) 
  { 
	/*pfirst = m.pfirst;*/  //&lt;----------warum geht das net 
  } 
  return *this; //Referenz auf das Objekt selbst zurückgeben
</code></pre>
<p>ditto</p>
<p>Sicher gibt es noch andere Probleme, die beim Überfliegen nicht auffallen. Wenn du noch ein main() spendierst, könnten wir das Programm auch selbst testen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2251968</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2251968</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Mon, 17 Sep 2012 13:59:27 GMT</pubDate></item><item><title><![CDATA[Reply to Dynamische Array Container Implementierung on Mon, 17 Sep 2012 14:06:08 GMT]]></title><description><![CDATA[<p>Was ist deine Frage? Bitte lies den ersten Link in meiner Signatur. Einfach nur &quot;komme nicht weiter&quot; und 400 Zeilen Code hinrotzen ist keine Frage.</p>
<p>Allgemein scheinst du dynamischen Speicher gründlich missverstanden zu haben, außerdem ist dein Code viel zu kompliziert. Ein paar besondere Hauer, die mir auf den ersten Blick auffallen:</p>
<pre><code class="language-cpp">delete [] pfirst;
        delete [] plast;
        delete [] pend;
</code></pre>
<p>plast und pend sind bei dir keine Pointer auf dynamischen Speicher. Folgt in der Regel ein Absturz.</p>
<pre><code class="language-cpp">for(unsigned int i=0;i&lt;size();i++)
        {
            typ.~T();   
        }
</code></pre>
<p>Guck mal, was delete wohl macht. Im allgemeinen ist ein expliziter Konstruktoraufruf eine sehr spezielle Sache und für Anfänger ein sicheres Zeichen, dass man gerade Müll programmiert.</p>
<pre><code class="language-cpp">void push_back( const T&amp; val ){
    size_t sz = size()+1;
    size_t t = size();
    T* pdest = new T[sz];
    std::copy(pfirst,plast,pdest);
    delete [] pfirst;
    pfirst = pdest;
    plast = pend = plast+sz;                //&lt;---geht net :-(
    std::fill(pfirst+t,plast,val);
    *if(capacity() == 0){}
    if(capacity() &gt; 0){}*/
    }
</code></pre>
<p>Viel zu umständlich. Außerdem ignorierst du deine capacity. Vorgehen sollte sein:<br />
- Falls noch Platz:<br />
- Hinten anfügen<br />
- Fertig<br />
- Falls kein Platz:<br />
- Neuen Speicher anfordern (X Mal so viel wie vorher, nicht nur einen mehr)<br />
- Kopieren<br />
- Hinten anfügen<br />
- Alten Speicher freigeben<br />
- Fertig</p>
<p>Du solltest auch stärker abstrahieren. Bei dir kommt mindestens 5x das gleiche Schema von new, copy, delete vor. Schreib eine Funktion zur Reallokation, die du stattdessen benutzt.</p>
<p>Mehr Hinweise gibt's nur gegen eine richtige Frage.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2251973</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2251973</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Mon, 17 Sep 2012 14:06:08 GMT</pubDate></item><item><title><![CDATA[Reply to Dynamische Array Container Implementierung on Mon, 17 Sep 2012 15:22:48 GMT]]></title><description><![CDATA[<p>Sorry war ein bisschen zu allgemein formuliert.<br />
Die Frage ist wie implementiere ich den Destruktor,die push_back Funktion und den Operator= in diesem Fall richtig?</p>
<p>Der Destruktor hat den Test bestanden.</p>
<pre><code class="language-cpp">~DynArray(void){
		T typ;
		for(unsigned int i=0;i&lt;size();i++)//das habe ich gemacht, weil
		{                                 //im test vom Destruktor -&gt;
			typ.~T();	
		}
		pfirst = plast = pend = NULL;
		delete [] pfirst;

	}
</code></pre>
<pre><code class="language-cpp">void TestDynArray::test_dtor()
{
	int cnt = Any::getCount();
	bool pass = true;
	{
		DynArray&lt;Any&gt; d(3);
	}                    //&lt;- an dieser all Objekte gelöscht werden sollen
	if( Any::getCount()!=cnt) pass=false;
	cnt = Any::getCount();
	DynArray&lt;Any&gt; d(3);
	d.~DynArray();
	if( Any::getCount()!=cnt) pass=false;
	if( d.size()!=0 ) pass=false;
	if( d.capacity()!=0 ) pass=false;
	cout&lt;&lt; setw(fieldWidth)  &lt;&lt; &quot;4  - ~DynArray()&quot; &lt;&lt; ( (pass) ? &quot;PASS&quot; : &quot;FAIL&quot; ) &lt;&lt; endl;
}
</code></pre>
<p>Die push_back Funktion lässt sich nicht testen. Absturz beim Ausführen.<br />
Was mache ich hier falsch? danke &quot;&quot;</p>
<pre><code class="language-cpp">void push_back( const T&amp; val ){

		if(capacity() != 0){
			size_t Pos = plast-pfirst;
			*(pfirst+Pos) = val;
			plast= plast+1;
		}

		if(capacity() == 0){
			size_t sz = size()*2;
			size_t t = size();
			T* pdest = new T[sz];
			std::copy(pfirst,plast,pdest);
			*(pdest+t) = val;
			delete [] pfirst;
			pfirst = pdest;
			plast = pend = plast+sz;
		}
	}
</code></pre>
<p>Und ich weiß bei besten willen nicht wieso das nicht Funktioniert?<br />
Die Überladung des Operators &quot;=&quot;:</p>
<pre><code class="language-cpp">DynArray&amp; operator=( const DynArray &amp;m ){ 
  if (this != &amp;m)  //oder if (*this != rhs) 
  { 
	pfirst = m.pfirst;  
  } 
  return *this; 
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2252011</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2252011</guid><dc:creator><![CDATA[Broly]]></dc:creator><pubDate>Mon, 17 Sep 2012 15:22:48 GMT</pubDate></item><item><title><![CDATA[Reply to Dynamische Array Container Implementierung on Mon, 17 Sep 2012 15:40:47 GMT]]></title><description><![CDATA[<p>Broly schrieb:</p>
<blockquote>
<p>Der Destruktor hat den Test bestanden.</p>
</blockquote>
<p>Dann ist dein Test falsch oder erkennt den Fehler nicht, weil sich mehrere Fehler gegenseitig wegheben (was dann auch ein Fehler im Test ist, wenn er das nicht erkennen kann).</p>
<p>Der eigentlich Fehler liegt übrigens im Konstruktor, wo du fröhlich bestehende Objekte mit dem fill überschreibst. Am Ende hast du doppelt so viele Objekte erzeugt wie geplant.</p>
<blockquote>
<p>Die push_back Funktion lässt sich nicht testen. Absturz beim Ausführen.<br />
Was mache ich hier falsch? danke &quot;&quot;</p>
</blockquote>
<p>Du nutzt keinen Debugger, um die Ursache zu finden.</p>
<blockquote>
<pre><code class="language-cpp">void push_back( const T&amp; val ){

		if(capacity() != 0){
			size_t Pos = plast-pfirst;
			*(pfirst+Pos) = val;
			plast= plast+1;
		}

		if(capacity() == 0){
			size_t sz = size()*2;
			size_t t = size();
			T* pdest = new T[sz];
			std::copy(pfirst,plast,pdest);
			*(pdest+t) = val;
			delete [] pfirst;
			pfirst = pdest;
			plast = pend = plast+sz;
		}
	}
</code></pre>
</blockquote>
<p>Ich weiß nicht, wo ich ansetzen soll. So ziemlich jede Zeile hat ein Problem. Ich finde deine Definition von capacity() nicht, aber sie ist schon einmal anders als bei std::vector oder der Code ist falsch. Was soll das Gerechne mit pos? Du hast doch schon einen Zeiger auf das ende, benutz ihn doch! Du erstellst beim Rallokieren wieder elemente und überschreibst sie direkt, gleiches Problem wie oben. Dein Wert für pend ist falsch. Ach, kurz: Alles Mist <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /> . Klingt hart, ist aber so, muss daher gesagt werden dürfen.</p>
<blockquote>
<p>Und ich weiß bei besten willen nicht wieso das nicht Funktioniert?<br />
Die Überladung des Operators &quot;=&quot;:</p>
<pre><code class="language-cpp">DynArray&amp; operator=( const DynArray &amp;m ){ 
  if (this != &amp;m)  //oder if (*this != rhs) 
  { 
	pfirst = m.pfirst;  
  } 
  return *this; 
}
</code></pre>
</blockquote>
<p>Und was ist mit dem Rest deiner Member? Implementier am besten das Copy&amp;Swap-Idiom, da sparst du dir viele mögliche Fehler und kannst auch den Selbstzuweisungstest sein lassen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2252020</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2252020</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Mon, 17 Sep 2012 15:40:47 GMT</pubDate></item><item><title><![CDATA[Reply to Dynamische Array Container Implementierung on Mon, 17 Sep 2012 15:42:38 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">T typ;                               // erstellt ein Objekt des Typs T; falls T eine nicht-POD-Klasse ist, wird auch noch der Defaultkonstruktor aufgerufen
		for(unsigned int i=0;i&lt;size();i++)
		{                                 
			typ.~T();	             // ruft den Destruktor des Objekts typ size-mal auf; falls dieser nicht trivial und size &gt; 1 ist das undefiniert
		}
		pfirst = plast = pend = NULL;        // im Prinzip überflüssig; da die Lebenszeit der Member sowieso endet, kommt es nicht darauf an, welchen Inhalt sie zum Schluss haben
		delete [] pfirst;                    // delete auf Nullzeiger bewirkt nichts; würde delete[] vor dem Nullsetzen aufgerufen wird das array normal zerstört und alle Destruktoren nach bedarf aufgerufen werden
                                                     // hier wird typ nochmal zerstört; wie oben, falls der Destruktur nicht-trivial und size &gt; 0 resultiert UB
</code></pre>
<p>Das Ganze sollte also so aussehen:</p>
<pre><code class="language-cpp">~DynArray(){
        if ( pfirst != 0 )  // optional
            delete [] pfirst;
    }
</code></pre>
<p>Die push_back Funktion lässt sich nicht testen. Absturz beim Ausführen.<br />
Was mache ich hier falsch? danke &quot;&quot;</p>
<pre><code class="language-cpp">void push_back( const T&amp; val ){

		if(capacity() != 0){
			size_t Pos = plast-pfirst;
			*(pfirst+Pos) = val;
			plast= plast+1;
		}

		if(capacity() == 0){
			size_t sz = size()*2;
			size_t t = size();
			T* pdest = new T[sz];
			std::copy(pfirst,plast,pdest);
			*(pdest+t) = val;
			delete [] pfirst;
			pfirst = pdest;
			plast = pend = plast+sz;
		}
	}
</code></pre>
<p>Die Logik ist fehlerhaft. Eine Reallokation ist genau dann erforderlich, wenn die Kapazität erschöpft ist, also size()==capacity().<br />
Ansonsten sollte die bereits implementierte Copy-Funktionalität verwendet werden. Mit einer zusätzlichen Funktion swap wird es viel einfacher</p>
<pre><code class="language-cpp">DynArray( const DynArray&amp; d, std::size_t capacity )
        : pfirst(new T[capacity]), plast(pfirst+std::min(d.size(), capacity)), pend(pfirst+capacity)
    {
        std::copy( d.pfirst, d.pfirst+std::min(d.size(), capacity), pfirst );
    }
    void swap(DynArray&amp; other) {
        std::swap( pfirst, other.pfirst );
        std::swap( plast, other.plast );
        std::swap( pend, other.pend );
    }
    void push_back( const T&amp; val ){
        if ( size() == capacity() )
            DynArray( *this, capacity()+1 ).swap( *this );
        *plast++ = val;
    }
</code></pre>
<pre><code class="language-cpp">DynArray&amp; operator=( const DynArray &amp;m ){ // copy&amp;swap
    DynArray( m ).swap( *this );
    return *this;
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2252021</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2252021</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Mon, 17 Sep 2012 15:42:38 GMT</pubDate></item><item><title><![CDATA[Reply to Dynamische Array Container Implementierung on Mon, 17 Sep 2012 15:47:29 GMT]]></title><description><![CDATA[<p>camper schrieb:</p>
<blockquote>
<p>Sicher gibt es noch andere Probleme, die beim Überfliegen nicht auffallen. Wenn du noch ein main() spendierst, könnten wir das Programm auch selbst testen.</p>
</blockquote>
<p>Die main();</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &quot;DynArray.h&quot;
#include &quot;TestDynArray.h&quot;

using namespace std;

int main(){

	TestDynArray test;
	test.test_all();

	return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2252024</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2252024</guid><dc:creator><![CDATA[Broly]]></dc:creator><pubDate>Mon, 17 Sep 2012 15:47:29 GMT</pubDate></item><item><title><![CDATA[Reply to Dynamische Array Container Implementierung on Mon, 17 Sep 2012 16:30:54 GMT]]></title><description><![CDATA[<p>camper schrieb:</p>
<blockquote>
<pre><code class="language-cpp">~DynArray(){
        if ( pfirst != 0 )  // optional
            delete [] pfirst;
    }
</code></pre>
</blockquote>
<p>Optional? Du meinst wohl &quot;vollkommen wirkungslos und verwirrend&quot;.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2252040</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2252040</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Mon, 17 Sep 2012 16:30:54 GMT</pubDate></item><item><title><![CDATA[Reply to Dynamische Array Container Implementierung on Mon, 17 Sep 2012 16:35:03 GMT]]></title><description><![CDATA[<p>SeppJ schrieb:</p>
<blockquote>
<p>camper schrieb:</p>
<blockquote>
<pre><code class="language-cpp">~DynArray(){
        if ( pfirst != 0 )  // optional
            delete [] pfirst;
    }
</code></pre>
</blockquote>
<p>Optional? Du meinst wohl &quot;vollkommen wirkungslos und verwirrend&quot;.</p>
</blockquote>
<p>Jetzt bin ich verwirrt :p</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2252042</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2252042</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Mon, 17 Sep 2012 16:35:03 GMT</pubDate></item><item><title><![CDATA[Reply to Dynamische Array Container Implementierung on Mon, 17 Sep 2012 17:08:54 GMT]]></title><description><![CDATA[<p>camper schrieb:</p>
<blockquote>
<p>SeppJ schrieb:</p>
<blockquote>
<p>camper schrieb:</p>
<blockquote>
<pre><code class="language-cpp">~DynArray(){
        if ( pfirst != 0 )  // optional
            delete [] pfirst;
    }
</code></pre>
</blockquote>
<p>Optional? Du meinst wohl &quot;vollkommen wirkungslos und verwirrend&quot;.</p>
</blockquote>
<p>Jetzt bin ich verwirrt :p</p>
</blockquote>
<p>Na, wenn <code>pfirst == 0</code> wäre, dann würde <code>delete[] pfirst;</code> genau gar nix machen (vom Standard garantiert). Dafür verwirrst du nun Broly, der vielleicht denkt, dass diese Abfrage auch nur irgendwie nützlich wäre.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2252051</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2252051</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Mon, 17 Sep 2012 17:08:54 GMT</pubDate></item><item><title><![CDATA[Reply to Dynamische Array Container Implementierung on Mon, 17 Sep 2012 17:34:34 GMT]]></title><description><![CDATA[<p>Danke erst mal für die Antworten.</p>
<p>Wenn ich im Destruktor den Speicherplatz freigebe, bevor ich die Membervariablen auf NULL setzte, stürzt das Programm ab. warum, weiß ich nicht.</p>
<pre><code class="language-cpp">~DynArray(){
        if ( pfirst != 0 )  // optional
            delete [] pfirst;
    }
</code></pre>
<p>Der Lösungsansatz für die push_back Funktion ist recht kompliziert zb. habe ich<br />
solche Implementierungsansätzte</p>
<pre><code class="language-cpp">DynArray( const DynArray&amp; d, std::size_t capacity )
  : pfirst(new T[capacity]), plast(pfirst+std::min(d.size(), capacity)), &lt;- ?
</code></pre>
<p>noch nie gesehen <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f62e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_open_mouth"
      title=":open_mouth:"
      alt="😮"
    /> . Ich bin mir sicher, dass diese Lösung richtig ist und sie ihren zweck erfüllt. Aber da ich sie nicht so recht versteh, kann ich sie nicht einfach übernehmen, außerdem besteht sie nicht den Test, in der Test_Unit vom Prof.^^ Meine Frage ist daher zur Funktion push_back, gibt es nicht einen<br />
Lösungsansatz, wobei man nicht einen weiteren Constructor überladen muss?</p>
<pre><code class="language-cpp">DynArray( const DynArray&amp; d, std::size_t capacity )
        : pfirst(new T[capacity]), plast(pfirst+std::min(d.size(), capacity)), pend(pfirst+capacity)
    {
        std::copy( d.pfirst, d.pfirst+std::min(d.size(), capacity), pfirst );
    }
    void swap(DynArray&amp; other) {
        std::swap( pfirst, other.pfirst );
        std::swap( plast, other.plast );
        std::swap( pend, other.pend );
    }
    void push_back( const T&amp; val ){
        if ( size() == capacity() )
            DynArray( *this, capacity()+1 ).swap( *this );
        *plast++ = val;
    }
</code></pre>
<p>Ach und danke, der &quot;=&quot; Operator funktioniert jetzt und die Implementierung war sehr verständlich. <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="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2252064</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2252064</guid><dc:creator><![CDATA[Broly]]></dc:creator><pubDate>Mon, 17 Sep 2012 17:34:34 GMT</pubDate></item><item><title><![CDATA[Reply to Dynamische Array Container Implementierung on Mon, 17 Sep 2012 18:47:22 GMT]]></title><description><![CDATA[<p>SeppJ schrieb:</p>
<blockquote>
<p>camper schrieb:</p>
<blockquote>
<p>SeppJ schrieb:</p>
<blockquote>
<p>camper schrieb:</p>
<blockquote>
<pre><code class="language-cpp">~DynArray(){
        if ( pfirst != 0 )  // optional
            delete [] pfirst;
    }
</code></pre>
</blockquote>
<p>Optional? Du meinst wohl &quot;vollkommen wirkungslos und verwirrend&quot;.</p>
</blockquote>
<p>Jetzt bin ich verwirrt :p</p>
</blockquote>
<p>Na, wenn <code>pfirst == 0</code> wäre, dann würde <code>delete[] pfirst;</code> genau gar nix machen (vom Standard garantiert). Dafür verwirrst du nun Broly, der vielleicht denkt, dass diese Abfrage auch nur irgendwie nützlich wäre.</p>
</blockquote>
<p>Genau gar nix ist auch recht unpräzise (der Standard an dieser Stelle allerdings auch). Die Diskussion über das Für und Wider einer solchen Abfrage wurde an anderer Stelle bereits geführt, das möchte ich nicht aufwärmen.<br />
Es gibt im Grunde zwei Argumente, die für eine solche Abfrage sprechen:<br />
1. Symmetrie: zu jeder Deallokation gehört eine erfolgreiche Allokation (das ist das einzige Argument, das für mich persönlich relevant ist - Programme sollten lesbar bein)<br />
2. &quot;Genau nix&quot; ist immer noch ein Funktionsaufruf mehr als nötig</p>
<p>n3337 3.7.4.2 schrieb:</p>
<blockquote>
<p>3 If a deallocation function terminates by throwing an exception, the behavior is undefined. <strong>The value</strong> of the<br />
first argument <strong>supplied to a deallocation function may be a null pointer value</strong>; if so, <strong>and if the deallocation<br />
function is one supplied in the standard library, the call has no effect</strong>. Otherwise, the behavior is undefined<br />
if the value supplied to operator delete(void*) in the standard library is not one of the values returned<br />
by a previous invocation of either operator new(std::size_t) or operator new(std::size_t, const<br />
std::nothrow_t&amp;) in the standard library, and the behavior is undefined if the value supplied to operator<br />
delete[](void*) in the standard library is not one of the values returned by a previous invocation of<br />
either operator new[](std::size_t) or operator new[](std::size_t, const std::nothrow_t&amp;) in the<br />
standard library.</p>
</blockquote>
<p>Natürlich würde ich jede Ersatzfunktion, die bei einem Nullargument etwas anderes als nix tut als hoffnungslos defekt ansehen. In <em>jedem Fall</em> (mal von unwahrscheinlicher whole-program-Optimierung abgesehen) wird allerdings bei 0 überhaupt ein Funktionsaufruf durchgeführt.</p>
<p>Ich habe nichts dagegen, die Abfrage wegzulassen, halte diese Wahl aber eben nicht für <em>so eindeutig</em> besser, also schreibe ich optional. Ich glaube auch nicht, das das zu Verwirrung führt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2252094</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2252094</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Mon, 17 Sep 2012 18:47:22 GMT</pubDate></item><item><title><![CDATA[Reply to Dynamische Array Container Implementierung on Mon, 17 Sep 2012 19:16:26 GMT]]></title><description><![CDATA[<p>camper schrieb:</p>
<blockquote>
<p>SeppJ schrieb:</p>
<blockquote>
<p>camper schrieb:</p>
<blockquote>
<p>SeppJ schrieb:</p>
<blockquote>
<p>camper schrieb:</p>
<blockquote>
<pre><code class="language-cpp">~DynArray(){
        if ( pfirst != 0 )  // optional
            delete [] pfirst;
    }
</code></pre>
</blockquote>
<p>Optional? Du meinst wohl &quot;vollkommen wirkungslos und verwirrend&quot;.</p>
</blockquote>
<p>Jetzt bin ich verwirrt :p</p>
</blockquote>
<p>Na, wenn <code>pfirst == 0</code> wäre, dann würde <code>delete[] pfirst;</code> genau gar nix machen (vom Standard garantiert). Dafür verwirrst du nun Broly, der vielleicht denkt, dass diese Abfrage auch nur irgendwie nützlich wäre.</p>
</blockquote>
<p>Genau gar nix ist auch recht unpräzise (der Standard an dieser Stelle allerdings auch). Die Diskussion über das Für und Wider einer solchen Abfrage wurde an anderer Stelle bereits geführt, das möchte ich nicht aufwärmen.<br />
Es gibt im Grunde zwei Argumente, die für eine solche Abfrage sprechen:<br />
1. Symmetrie: zu jeder Deallokation gehört eine erfolgreiche Allokation (das ist das einzige Argument, das für mich persönlich relevant ist - Programme sollten lesbar bein)</p>
</blockquote>
<p>Als würde <code>new</code> einen Nullzeiger zurückgeben, wenn es fehlschlägt.. Was meinst du also mit Symmetrie?</p>
<p>camper schrieb:</p>
<blockquote>
<p>2. &quot;Genau nix&quot; ist immer noch ein Funktionsaufruf mehr als nötig</p>
</blockquote>
<p>Mikrooptimierungen rechtfertigen natürlich jeden Mist.<br />
Woher weißt du, ob nicht viel mehr Folgendes zutrifft: In den meisten Fällen ist der Zeiger ungleich Null und dann hat man eine Abfrage mehr als nötig. Hunderte solcher Abfragen im Programm blähen den Code auf und verschwenden Platz im Cache und damit Ladezeit. Die Abfrage in <code>delete</code> pro Typ liegt jedoch fast immer schon im Cache.</p>
<p>camper schrieb:</p>
<blockquote>
<p>Ich habe nichts dagegen, die Abfrage wegzulassen, halte diese Wahl aber eben nicht für <em>so eindeutig</em> besser, also schreibe ich optional. Ich glaube auch nicht, das das zu Verwirrung führt.</p>
</blockquote>
<p>Offensichtlich führt es zu Verwirrung, weil sehr viele die redundante Abfrage machen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2252099</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2252099</guid><dc:creator><![CDATA[TyRoXx]]></dc:creator><pubDate>Mon, 17 Sep 2012 19:16:26 GMT</pubDate></item><item><title><![CDATA[Reply to Dynamische Array Container Implementierung on Mon, 17 Sep 2012 19:18:28 GMT]]></title><description><![CDATA[<p>Broly schrieb:</p>
<blockquote>
<p>Wenn ich im Destruktor den Speicherplatz freigebe, bevor ich die Membervariablen auf NULL setzte, stürzt das Programm ab. warum, weiß ich nicht.</p>
</blockquote>
<p>Dann ist das einer der zahlreichen Fehler, auf die du schon hingewiesen wurdest. Wenn du die Variablen auf 0 setzt und dann freigibst, geschieht wie erwähnt nichts. Es wird aber auch nichts freigegeben. Speicherloch, setzen, Sechs.</p>
<blockquote>
<p>solche Implementierungsansätzte</p>
<pre><code class="language-cpp">DynArray( const DynArray&amp; d, std::size_t capacity )
  : pfirst(new T[capacity]), plast(pfirst+std::min(d.size(), capacity)), &lt;- ?
</code></pre>
<p>noch nie gesehen <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f62e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_open_mouth"
      title=":open_mouth:"
      alt="😮"
    /> .</p>
</blockquote>
<p>Das ist bloß eine Initialisierungsliste. Mal googlen. Ist nicht schwierig, aber wichtig.</p>
<blockquote>
<p>Aber da ich sie nicht so recht versteh, kann ich sie nicht einfach übernehmen, außerdem besteht sie nicht den Test, in der Test_Unit vom Prof.^^</p>
</blockquote>
<p>Du hast viele Fehler im Programm. Du wurdest auch schon auf die wichtigsten hingewiesen. Hast du sie bereinigt? Ich wette, Nein. Und falls ich damit Recht habe, sind insbesondere deine anderen Konstruktoren noch falsch.</p>
<blockquote>
<p>Meine Frage ist daher zur Funktion push_back, gibt es nicht einen<br />
Lösungsansatz, wobei man nicht einen weiteren Constructor überladen muss?</p>
</blockquote>
<p>Klar, mach einfach effektiv das gleiche, bloß alles in push_back. Das was camper gezeigt hat, ist die Logik aus meinem Algorithmus (meine erste Antwort) kombiniert mit dem Hinweis, die ganzen Reallokationen in eine Funktion zu packen (und zwar eine, die auch richtig(!) funktioniert). Das darfst du auch gerne umständlich ohne Zusatzfunktion machen, an das Grundrezept musst du dich aber schon halten.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2252100</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2252100</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Mon, 17 Sep 2012 19:18:28 GMT</pubDate></item><item><title><![CDATA[Reply to Dynamische Array Container Implementierung on Mon, 17 Sep 2012 20:44:51 GMT]]></title><description><![CDATA[<p>TyRoXx schrieb:</p>
<blockquote>
<p>camper schrieb:</p>
<blockquote>
<p>1. Symmetrie: zu jeder Deallokation gehört eine erfolgreiche Allokation (das ist das einzige Argument, das für mich persönlich relevant ist - Programme sollten lesbar bein)</p>
</blockquote>
<p>Als würde <code>new</code> einen Nullzeiger zurückgeben, wenn es fehlschlägt.</p>
</blockquote>
<p>Ich kann nicht folgen. Nochmal genau lesen.</p>
<p>TyRoXx schrieb:</p>
<blockquote>
<p>camper schrieb:</p>
<blockquote>
<p>2. &quot;Genau nix&quot; ist immer noch ein Funktionsaufruf mehr als nötig</p>
</blockquote>
<p>Mikrooptimierungen rechtfertigen natürlich jeden Mist.<br />
Woher weißt du, ob nicht viel mehr Folgendes zutrifft: In den meisten Fällen ist der Zeiger ungleich Null und dann hat man eine Abfrage mehr als nötig. Hunderte solcher Abfragen im Programm blähen den Code auf und verschwenden Platz im Cache und damit Ladezeit. Die Abfrage in <code>delete</code> pro Typ liegt jedoch fast immer schon im Cache.</p>
</blockquote>
<p>Allgemeinplätze. Ich weiss es nicht, könnte auch umgekehrt sein. Geht am Thema vorbei.<br />
Nimmt man deine Argumentation wörtlich, bist du es, der durch Weglassen der Prüfung mikrooptimiert.</p>
<p>TyRoXx schrieb:</p>
<blockquote>
<p>Offensichtlich führt es zu Verwirrung, weil sehr viele die redundante Abfrage machen.</p>
</blockquote>
<p>Ich kann nicht folgen.</p>
<p>a oder b ist gleichermaßen möglich.<br />
viele machen b<br />
--------------<br />
es herrscht Verwirrung ??</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2252125</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2252125</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Mon, 17 Sep 2012 20:44:51 GMT</pubDate></item><item><title><![CDATA[Reply to Dynamische Array Container Implementierung on Mon, 17 Sep 2012 20:46:47 GMT]]></title><description><![CDATA[<p>camper schrieb:</p>
<blockquote>
<p>a oder b ist gleichermaßen möglich.<br />
viele machen b<br />
--------------<br />
es herrscht Verwirrung ??</p>
</blockquote>
<p>Wie viele von denen, die b machen, können so wie du argumentieren, warum sie es machen? Du dürftest so ziemlich der einzige sein. Die anderen machen es bloß bei diesem Typen mit Ahnung im C++-Forum nach, auch wenn sie nicht verstehen, was es überhaupt soll.</p>
<p>Aber lass uns das wirklich nicht nochmal aufwärmen. Ich bin schon ganz still und hätte dies hier gar nicht schreiben sollen, aber wo ich es sowieso schon getan habe, kann ich es auch absenden… <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="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2252128</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2252128</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Mon, 17 Sep 2012 20:46:47 GMT</pubDate></item><item><title><![CDATA[Reply to Dynamische Array Container Implementierung on Mon, 17 Sep 2012 21:10:07 GMT]]></title><description><![CDATA[<p>SeppJ schrieb:</p>
<blockquote>
<p>... hätte dies hier gar nicht schreiben sollen, aber wo ich es sowieso schon getan habe, kann ich es auch absenden… <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="🙂"
    /></p>
</blockquote>
<p>Für gewöhnlich bin ich auch zu faul, überhaupt darauf einzugehen <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="🙂"
    /></p>
<p>SeppJ schrieb:</p>
<blockquote>
<p>Wie viele von denen, die b machen, können so wie du argumentieren, warum sie es machen?</p>
</blockquote>
<p>Genau das sollte imo der Sinn dieses Forums sein.<br />
Wofür sich jemand am Ende ist nicht wichtig, aber die Frage: &quot;Warum?&quot; sollte stets beantwortet werden können.<br />
Es sollten nicht einfach nur Lösungen vermittelt werden (die haben eine mehr oder minder kurze Halbwertszeit), sondern Grundlagen und Hintergründe.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2252133</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2252133</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Mon, 17 Sep 2012 21:10:07 GMT</pubDate></item><item><title><![CDATA[Reply to Dynamische Array Container Implementierung on Mon, 17 Sep 2012 22:03:23 GMT]]></title><description><![CDATA[<p>camper schrieb:</p>
<blockquote>
<p>TyRoXx schrieb:</p>
<blockquote>
<p>camper schrieb:</p>
<blockquote>
<p>2. &quot;Genau nix&quot; ist immer noch ein Funktionsaufruf mehr als nötig</p>
</blockquote>
<p>Mikrooptimierungen rechtfertigen natürlich jeden Mist.<br />
Woher weißt du, ob nicht viel mehr Folgendes zutrifft: In den meisten Fällen ist der Zeiger ungleich Null und dann hat man eine Abfrage mehr als nötig. Hunderte solcher Abfragen im Programm blähen den Code auf und verschwenden Platz im Cache und damit Ladezeit. Die Abfrage in <code>delete</code> pro Typ liegt jedoch fast immer schon im Cache.</p>
</blockquote>
<p>Allgemeinplätze. Ich weiss es nicht, könnte auch umgekehrt sein. Geht am Thema vorbei.<br />
Nimmt man deine Argumentation wörtlich, bist du es, der durch Weglassen der Prüfung mikrooptimiert.</p>
</blockquote>
<p>Du hast mit den Optimierungen angefangen, die jetzt auf einmal &quot;am Thema vorbeigehen&quot;.<br />
Durch Weglassen der Prüfung wird Redundanz vermieden. Das sollte wichtiger sein als hypothetische Mikrooptimierungen.<br />
Ich wollte nur zeigen, dass &quot;ein Funktionsaufruf mehr als nötig&quot; kein Argument für die Abfrage ist. Dass es mit Abfrage schneller ist, ist eine unbelegte Vermutung, genau wie meine Argumentation für das Weglassen der Abfrage.<br />
Ich wage mal zu behaupten, dass das stark von System und Compiler abhängt. Ein schlauer Compiler lässt das <code>if</code> vielleicht auch einfach weg.<br />
In jedem Fall spielt es keine Rolle, was schneller ist. Der Destruktor und das anschließende Freigeben des Speichers sind um Größenordnungen langsamer als ein Funktionsaufruf.<br />
Folglich gibt es kein Argument für die Abfrage. Das mit der &quot;Symmetrie&quot; hast du nicht erklärt, es liest sich wie wirres Zeug. Dass <code>delete</code> mit Null klarkommt, hat man wahrscheinlich von <code>free</code> übernommen. Und das verhält sich vermutlich so, weil Aufräum-Code häufig mit Nullzeigern zu tun hat.<br />
Die eingebaute Abfrage ist ein Feature von C++, eine Abstraktion. Man sollte nicht so tun als gäbe es die nicht.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2252140</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2252140</guid><dc:creator><![CDATA[TyRoXx]]></dc:creator><pubDate>Mon, 17 Sep 2012 22:03:23 GMT</pubDate></item><item><title><![CDATA[Reply to Dynamische Array Container Implementierung on Mon, 17 Sep 2012 22:51:47 GMT]]></title><description><![CDATA[<p>Ich bin nicht sicher, was daran schwer verständlich sein sollte, dass Ressourcen nur freigegeben werden können, sofern sie zuvor angefordert wurden.<br />
Der Versuch, Ressourcen die nicht angefordert wurden, zurückzugeben, ist danach ein Fehler (Nichts als Kategorie).<br />
Andererseits kann es sinnvoll sein, eine Menge Ressourcen freizugeben, auch wenn diese Menge zufällig leer ist (Nichts als Quantität).<br />
In Bezug auf Speicher kann in C++ beides ohne Umstände modelliert werden.</p>
<p>TyRoXx schrieb:</p>
<blockquote>
<p>Die eingebaute Abfrage ist ein Feature von C++, eine Abstraktion. Man sollte nicht so tun als gäbe es die nicht.</p>
</blockquote>
<p>Macht ja auch keiner.</p>
<p>Oder meinst du eher, dass man dieses Feature nutzen sollte, weil es einmal da ist? Dann interessiert mich, unter welcher Regel du diesen Schluss vom Sein aufs Sollen subsumierst.</p>
<p>Reichst du beim Lotto deinen Tippschein auch dann ein, wenn du weisst, dass es eine Niete ist?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2252145</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2252145</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Mon, 17 Sep 2012 22:51:47 GMT</pubDate></item><item><title><![CDATA[Reply to Dynamische Array Container Implementierung on Tue, 18 Sep 2012 00:48:50 GMT]]></title><description><![CDATA[<p>camper schrieb:</p>
<blockquote>
<p>Ich bin nicht sicher, was daran schwer verständlich sein sollte, dass Ressourcen nur freigegeben werden können, sofern sie zuvor angefordert wurden.<br />
Der Versuch, Ressourcen die nicht angefordert wurden, zurückzugeben, ist danach ein Fehler (Nichts als Kategorie).<br />
Andererseits kann es sinnvoll sein, eine Menge Ressourcen freizugeben, auch wenn diese Menge zufällig leer ist (Nichts als Quantität).<br />
In Bezug auf Speicher kann in C++ beides ohne Umstände modelliert werden.</p>
</blockquote>
<p>Was hat das mit <code>delete[]</code> zu tun?</p>
<p>camper schrieb:</p>
<blockquote>
<p>TyRoXx schrieb:</p>
<blockquote>
<p>Die eingebaute Abfrage ist ein Feature von C++, eine Abstraktion. Man sollte nicht so tun als gäbe es die nicht.</p>
</blockquote>
<p>Macht ja auch keiner.</p>
</blockquote>
<p>Doch, mit der Abfrage tut man das. Das ist so ähnlich wie <code>while ((i != 0) == true)</code> . Kann man machen, sollte man aber nicht.</p>
<p>camper schrieb:</p>
<blockquote>
<p>Oder meinst du eher, dass man dieses Feature nutzen sollte, weil es einmal da ist? Dann interessiert mich, unter welcher Regel du diesen Schluss vom Sein aufs Sollen subsumierst.</p>
</blockquote>
<p>Was willst du eigentlich? <code>delete[]</code> hat Eigenschaften, die man bei der Benutzung beachten sollte. Ich behaupte nicht, dass man <code>delete[]</code> jemals benutzen sollte oder dass seine Eigenschaften immer gut sind.</p>
<p>camper schrieb:</p>
<blockquote>
<p>Reichst du beim Lotto deinen Tippschein auch dann ein, wenn du weisst, dass es eine Niete ist?</p>
</blockquote>
<p>Millionen Menschen tun das, ich nicht.<br />
Was hat das mit <code>delete[]</code> zu tun?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2252151</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2252151</guid><dc:creator><![CDATA[TyRoXx]]></dc:creator><pubDate>Tue, 18 Sep 2012 00:48:50 GMT</pubDate></item><item><title><![CDATA[Reply to Dynamische Array Container Implementierung on Tue, 18 Sep 2012 00:45:03 GMT]]></title><description><![CDATA[<p>TyRoXx schrieb:</p>
<blockquote>
<p>camper schrieb:</p>
<blockquote>
<p>Ich bin nicht sicher, was daran schwer verständlich sein sollte, dass Ressourcen nur freigegeben werden können, sofern sie zuvor angefordert wurden.<br />
Der Versuch, Ressourcen die nicht angefordert wurden, zurückzugeben, ist danach ein Fehler (Nichts als Kategorie).<br />
Andererseits kann es sinnvoll sein, eine Menge Ressourcen freizugeben, auch wenn diese Menge zufällig leer ist (Nichts als Quantität).<br />
In Bezug auf Speicher kann in C++ beides ohne Umstände modelliert werden.</p>
</blockquote>
<p>Was hat das mit <code>delete[]</code> zu tun?</p>
</blockquote>
<p>um...<br />
Speicher ist ein Ressource.<br />
delete gibt Speicher frei.<br />
delete kann auch auf Nullzeiger angewandt werden.<br />
Nullzeiger können repräsentieren, dass nie Speicher angefordert wurde.</p>
<p>TyRoXx schrieb:</p>
<blockquote>
<p>camper schrieb:</p>
<blockquote>
<p>TyRoXx schrieb:</p>
<blockquote>
<p>Die eingebaute Abfrage ist ein Feature von C++, eine Abstraktion. Man sollte nicht so tun als gäbe es die nicht.</p>
</blockquote>
<p>Macht ja auch keiner.</p>
</blockquote>
<p>Doch, mit der Abfrage tut man das. Das ist das so ähnlich wie <code>while ((i != 0) == true)</code> . Kann man machen, sollte man aber nicht.</p>
</blockquote>
<p>&quot;Man sollte nicht so als gäbe es die nicht.&quot; -&gt; &quot;so tun als ob&quot; impliziert Intention oder Wille. Dein while-Beispiel deutet eher auf Unwissenheit oder Ignoranz hin. Weil also nicht klar ist, was du eigentlich sagen wolltest, habe ich die Aussage erst einmal wörtlich interpretiert.</p>
<p>TyRoXx schrieb:</p>
<blockquote>
<p>camper schrieb:</p>
<blockquote>
<p>Oder meinst du eher, dass man dieses Feature nutzen sollte, weil es einmal da ist? Dann interessiert mich, unter welcher Regel du diesen Schluss vom Sein aufs Sollen subsumierst.</p>
</blockquote>
<p>Was willst du eigentlich? <code>delete[]</code> hat Eigenschaften, die man bei der Benutzung beachten sollte. Ich behaupte nicht, dass man <code>delete[]</code> jemals benutzen sollte oder dass seine Eigenschaften immer gut sind.</p>
</blockquote>
<p>Das ist die zweite Interpretation. Was du sagst, ist im Prinzip:<br />
X hat die Eigenschaft A. Also sollte man die Eigenschaft A ausnutzen. (X=delete; A=kann auch mit Nullzeigern genutzt werden)<br />
Das ist (ohne eine entsprechende Ableitungsregel) ein <a href="http://en.wikipedia.org/wiki/Is%E2%80%93ought_problem" rel="nofollow">Trugschluss</a>.</p>
<p>TyRoXx schrieb:</p>
<blockquote>
<p>camper schrieb:</p>
<blockquote>
<p>Reichst du beim Lotto deinen Tippschein auch dann ein, wenn du weisst, dass es eine Niete ist?</p>
</blockquote>
<p>Millionen Menschen tun das, ich nicht.<br />
Was hat das mit <code>delete[]</code> zu tun?</p>
</blockquote>
<p>Es ist ein anschauliches, analoges Beispiel.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2252154</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2252154</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Tue, 18 Sep 2012 00:45:03 GMT</pubDate></item><item><title><![CDATA[Reply to Dynamische Array Container Implementierung on Tue, 18 Sep 2012 01:49:20 GMT]]></title><description><![CDATA[<p>camper schrieb:</p>
<blockquote>
<p>TyRoXx schrieb:</p>
<blockquote>
<p>camper schrieb:</p>
<blockquote>
<p>Oder meinst du eher, dass man dieses Feature nutzen sollte, weil es einmal da ist? Dann interessiert mich, unter welcher Regel du diesen Schluss vom Sein aufs Sollen subsumierst.</p>
</blockquote>
<p>Was willst du eigentlich? <code>delete[]</code> hat Eigenschaften, die man bei der Benutzung beachten sollte. Ich behaupte nicht, dass man <code>delete[]</code> jemals benutzen sollte oder dass seine Eigenschaften immer gut sind.</p>
</blockquote>
<p>Das ist die zweite Interpretation. Was du sagst, ist im Prinzip:<br />
X hat die Eigenschaft A. Also sollte man die Eigenschaft A ausnutzen. (X=delete; A=kann auch mit Nullzeigern genutzt werden)<br />
Das ist (ohne eine entsprechende Ableitungsregel) ein <a href="http://en.wikipedia.org/wiki/Is%E2%80%93ought_problem" rel="nofollow">Trugschluss</a>.</p>
</blockquote>
<p>Wow, wir sind schon bei der Philosophie angekommen. Wenn du nichts mehr über C++ zu sagen hast, sind wir ja fertig.</p>
<p>camper schrieb:</p>
<blockquote>
<p>TyRoXx schrieb:</p>
<blockquote>
<p>camper schrieb:</p>
<blockquote>
<p>Reichst du beim Lotto deinen Tippschein auch dann ein, wenn du weisst, dass es eine Niete ist?</p>
</blockquote>
<p>Millionen Menschen tun das, ich nicht.<br />
Was hat das mit <code>delete[]</code> zu tun?</p>
</blockquote>
<p>Es ist ein anschauliches, analoges Beispiel.</p>
</blockquote>
<p>Ich frage mich so langsam wer hier die Niete ist.</p>
<p>EDIT: Ah, du meinst den Zettel, mit dem man seinen Gewinn abholen kann.<br />
Das unnötige Prüfen des Scheins entspricht in meinen Augen eher einem <code>assert(ptr)</code> , wenn die Programmlogik das hergibt. Beides kostet nichts und kann in Ausnahmefällen helfen.<br />
Die Abfrage vor <code>delete</code> bringt aber <em>nichts</em>, <em>gar nichts</em>, unter keinen Umständen nie und niemals. Das ist wie das Einreichen einer Rolle Klopapier bei der Lottostelle. Du kannst noch so viel Klopapier herbeischleppen, aber du wirst höchstens einen Tritt in den Hintern gewinnen (= Redundanz, schlechte Lesbarkeit, Verwirrung).<br />
Wo hier die Analogie zu deinen Gunsten ist, weiß ich immer noch nicht.<br />
Ist die Niete etwa ein Nullzeiger und <code>delete</code> das Einreichen? Wie gesagt, <code>delete</code> überprüft, ob du gewonnen hast.<br />
<code>delete</code> ist die LottoCard von C++.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2252155</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2252155</guid><dc:creator><![CDATA[TyRoXx]]></dc:creator><pubDate>Tue, 18 Sep 2012 01:49:20 GMT</pubDate></item><item><title><![CDATA[Reply to Dynamische Array Container Implementierung on Tue, 18 Sep 2012 06:34:29 GMT]]></title><description><![CDATA[<p>TyRoXx schrieb:</p>
<blockquote>
<p>Wow, wir sind schon bei der Philosophie angekommen.</p>
</blockquote>
<p>Es tut mir leid, wenn du damit überfordert bist.</p>
<p>TyRoXx schrieb:</p>
<blockquote>
<p>Wenn du nichts mehr über C++ zu sagen hast, sind wir ja fertig.</p>
</blockquote>
<p>Das glaube ich auch. Wenn du meinst, Logik über Bord werfen zu können, weil du ja sowieso recht hast.</p>
<p>TyRoXx schrieb:</p>
<blockquote>
<p>Die Abfrage vor <code>delete</code> bringt aber <em>nichts</em>, <em>gar nichts</em>, unter keinen Umständen nie und niemals.</p>
</blockquote>
<p>Ich sehe das anders. Da es gerade um diese Frage geht, taugt diese Behauptung nicht als Argument.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2252167</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2252167</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Tue, 18 Sep 2012 06:34:29 GMT</pubDate></item><item><title><![CDATA[Reply to Dynamische Array Container Implementierung on Tue, 18 Sep 2012 06:46:40 GMT]]></title><description><![CDATA[<p>Könnt ihr bitte zum Threadthema zurück kommen, insbesondere wenn eure Argumente an der Grenze zu persönlichen Beleidigungen angekommen sind (*TyRoXx anguck*)? Falls ihr weiter dieses Thema diskutieren wollt, spalte ich es hier ab oder macht selber einen neuen Thread auf. Aber ich glaube, es gibt nichts neues zu sagen, was hier im Forum nicht schon 1000x gesagt worden ist (Und TyRoXx hat sowieso Recht, er kann nur nicht gut diskutieren :p ).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2252170</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2252170</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Tue, 18 Sep 2012 06:46:40 GMT</pubDate></item></channel></rss>