<?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[Kurz beurteilen!]]></title><description><![CDATA[<p>Hilfe! Kann jemand kurz und schnell mal schauen, ob mein Code hier sauber ist? mein prof will heute noch wissen, ob ich template-programmierung verstanden habe!<br />
danke!</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/245798/kurz-beurteilen</link><generator>RSS for Node</generator><lastBuildDate>Fri, 18 Sep 2026 13:08:53 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/245798.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 19 Jul 2009 10:25:57 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Kurz beurteilen! on Sun, 19 Jul 2009 10:25:57 GMT]]></title><description><![CDATA[<p>Hilfe! Kann jemand kurz und schnell mal schauen, ob mein Code hier sauber ist? mein prof will heute noch wissen, ob ich template-programmierung verstanden habe!<br />
danke!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1745654</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1745654</guid><dc:creator><![CDATA[Hilfe!]]></dc:creator><pubDate>Sun, 19 Jul 2009 10:25:57 GMT</pubDate></item><item><title><![CDATA[Reply to Kurz beurteilen! on Sun, 19 Jul 2009 10:27:07 GMT]]></title><description><![CDATA[<p>Code vergessen... bin durch den wind</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;cstdlib&gt;
#include &lt;string&gt;
#include &lt;string&gt;

using namespace std;

template &lt;class T&gt;
class Allocator {

    public:
        Allocator() { }
        T* alloc() {
           return reinterpret_cast&lt;T*&gt;(malloc(sizeof(T)));
        }

        void destroy(T *obj) {
            obj-&gt;~T();
            free(obj);
        } 
};

template &lt;class T, class Alloc = Allocator&lt;T&gt; &gt;
class Test {
  private:
        T *val;
        Alloc *alloc;

  public:
        Test(T param) {
            alloc = new Alloc();
            val = alloc-&gt;alloc();
            *val = param;
            std::cout &lt;&lt; *val &lt;&lt; std::endl;
        }

        ~Test() {
            alloc-&gt;destroy(val);
            delete alloc;
        }      

};

int main() {
    Test&lt;string&gt; t(&quot;Ich mag Eis.&quot;);
    system(&quot;pause&quot;);
    return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1745655</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1745655</guid><dc:creator><![CDATA[Hilfe!]]></dc:creator><pubDate>Sun, 19 Jul 2009 10:27:07 GMT</pubDate></item><item><title><![CDATA[Reply to Kurz beurteilen! on Sun, 19 Jul 2009 10:39:41 GMT]]></title><description><![CDATA[<p>Hätteste statt 'kurz beurteilen' nicht irgendwas mit 'template' wählen können?<br />
Da kann man so was leichter widerfinden, wenn man es suchen sollte <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="😉"
    /></p>
<p>Zu deinem eigentlichen Anliegen kann ich nicht viel beitragen, außer warum<br />
includierst du 'string' 2x ?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1745663</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1745663</guid><dc:creator><![CDATA[f.-th.]]></dc:creator><pubDate>Sun, 19 Jul 2009 10:39:41 GMT</pubDate></item><item><title><![CDATA[Reply to Kurz beurteilen! on Sun, 19 Jul 2009 10:51:29 GMT]]></title><description><![CDATA[<p>Was mich noch ein wenig verwirrt:<br />
Zeile 6 oder Zeile 34 solltest du noch mal kritisch beurteilen!</p>
<p>malloc/free und new/delete im selben Quelltext gibt es da keine andere Lösung?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1745671</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1745671</guid><dc:creator><![CDATA[f.-th.]]></dc:creator><pubDate>Sun, 19 Jul 2009 10:51:29 GMT</pubDate></item><item><title><![CDATA[Reply to Kurz beurteilen! on Sun, 19 Jul 2009 11:48:26 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">template &lt;class T, class Alloc = Allocator&lt;T&gt; &gt; 
class Test { 
  private: 
        T *val; 
        Alloc *alloc; 

  public: 
        Test(T param) { 
            alloc = new Alloc(); 
            val = alloc-&gt;alloc(); 
            *val = param; 
            std::cout &lt;&lt; *val &lt;&lt; std::endl; 
        } 

        ~Test() { 
            alloc-&gt;destroy(val); 
            delete alloc; 
        }       
};
</code></pre>
<p>würd ich so nicht machen...</p>
<pre><code class="language-cpp">template &lt;class T, class Alloc = Allocator&lt;T&gt; &gt; 
class Test { 
  private: 
        T *val; 
        Alloc alloc; 

  public: 
        Test(T param)
        { 
            val = alloc.alloc(); 
            *val = param; //das hier ist so gar falsch...
            std::cout &lt;&lt; *val &lt;&lt; std::endl; 
        } 

        ~Test() { 
            alloc.destroy(val);
        }       
};
</code></pre>
<p>allocator sollte eine construct-function bereitstellen...<br />
hab so was auch ma für die uni gemacht - so sieht mein allocator aus:</p>
<pre><code class="language-cpp">//includes wären
#include &lt;algorithm&gt; //max
#include &lt;cstddef&gt; //size_t / ptrdiff_t
#include &lt;new&gt; //placement new

//und namespace std_copyed kannst du mit std ersetzen, falls ihr die standard-lib verwenden dürft - wir durften es nicht und so musst ich selbst was schreiben ^^
//kann auch sein, dass manchmal kein namespace mit davor steht, weil der allocator auch im namespace std_copyed steht^^

#ifdef COMPILER_MSVC
#	pragma warning(push, 3) //http://msdn.microsoft.com/en-us/library/26kb9fy0.aspx
#	pragma warning(disable: 4100) //C4100 can also be issued when code calls a destructor on a otherwise unreferenced parameter of primitive type. This is a limitation of the Visual C++ compiler.
#endif

template &lt;typename T&gt;
class allocator
{
public:
	typedef T value_type;
	typedef value_type* pointer;
	typedef const value_type* const_pointer;
	typedef value_type&amp; reference;
	typedef const value_type&amp; const_reference;

	typedef std_copyed::ptrdiff_t difference_type;
	typedef std_copyed::size_t size_type;

	template&lt;class T_&gt;
	struct rebind
	{
		typedef typename allocator&lt;T_&gt; other;
	};
public:
	allocator()
	{}

	template&lt;typename T_&gt;
	allocator(const allocator&lt;T_&gt; &amp;)
	{}

	pointer address(reference r) const
	{ return &amp;r; }
	const_pointer address(const_reference r) const
	{ return &amp;r; }

	pointer allocate(size_type count, allocator&lt;void&gt;::const_pointer = nullptr)
	{
		size_type object_size = sizeof(T);
		size_type byte_count = object_size * count;
		void* address = ::operator new(byte_count);
		return reinterpret_cast&lt;pointer&gt; (address);
	}

	void deallocate(pointer p, size_type /*count*/)
	{
		return ::operator delete(p);
	}

	void construct(pointer p, const_reference value)
	{
		::new(p) T(value);
	}

	void destroy(pointer p)
	{
		p-&gt;~value_type();
	}

	size_type max_size() const
	{
		size_type addresses = size_type(~0);
		size_type object_size = sizeof(T);
		return max(addresses/object_size, 1); //allocate(max_size()) shall gave us a well defined behavior - and allocate(0) is undefined
	}
};

template &lt; &gt;
struct allocator &lt;void&gt;
{
	typedef const void* const_pointer;
	typedef void* pointer;
};

template&lt;typename T&gt;
void swap(typename allocator&lt;T&gt; &amp;lhs, typename allocator&lt;T&gt; &amp;rhs)
{ /*do nothing*/ }

#ifdef COMPILER_MSVC
#	pragma warning(pop)
#endif
</code></pre>
<p>benutzung wäre so in etwa:</p>
<pre><code class="language-cpp">template&lt; typename T, typename TAlloc = allocator&lt;T&gt; &gt;
struct x
{
private:
  TAlloc alloc;
  T* a;
public:
  x()
  //: alloc() --- wird implizit aufgerufen, würd ich also weglassen ^^
  {
    a = alloc.allocate(1);
    try
    {
      alloc.construct(a, T());
    }
    catch(...)
    {
      alloc.deallocate(a, 1);
      throw;
    }
  }

  ~x()
  {
    alloc.deallocate(a, 1);
  }
}

int main()
{
  x a;
  x b;
}
</code></pre>
<p>bb</p>
<p>ich hoffe, es hilft dir ein wenig - falls nicht, dann frag einfach ^^</p>
<p>edit: hatte noch was vergessen ^^</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1745703</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1745703</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Sun, 19 Jul 2009 11:48:26 GMT</pubDate></item><item><title><![CDATA[Reply to Kurz beurteilen! on Sun, 19 Jul 2009 12:57:39 GMT]]></title><description><![CDATA[<blockquote>
<p>&gt; *val = param; //das hier ist so gar falsch...</p>
</blockquote>
<p>Wieso? <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>Dein Allokator ist ja im STL-Stil! Ich wollte das nicht soo komplex machen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1745747</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1745747</guid><dc:creator><![CDATA[Hilfe!]]></dc:creator><pubDate>Sun, 19 Jul 2009 12:57:39 GMT</pubDate></item><item><title><![CDATA[Reply to Kurz beurteilen! on Sun, 19 Jul 2009 14:05:06 GMT]]></title><description><![CDATA[<p>ok - dann machst du es eben anders ^^<br />
auf jeden fall passiert beim assignment operator ja im prinzip (in der praxis sieht die reihenfolge anders aus, aber das soll hier nicht stören):</p>
<p>- lösche lhs<br />
- kopiere rhs nach lhs</p>
<p>so bald T also ein typ ist, der einen destruktor besitzt, wird der aufgerufen - leider befindet sich da aber nicht wirklich ein objekt - es wurde bisher nur speicher angefordert und an der stelle steht irgendetwas...<br />
dafür gibt es das placement new - was hier durch construct gekapselt wird..</p>
<p>dem zu folge brauchst du mindestens das hier:</p>
<pre><code class="language-cpp">template &lt;typename T&gt;
struct allocator
{
    typedef std::size_t size_type;

    pointer allocate(size_type count = size_type(1))
    {
        size_type object_size = sizeof(T);
        size_type byte_count = object_size * count;
        void* address = ::operator new(byte_count);
        return reinterpret_cast&lt;pointer&gt; (address);
    }

    void deallocate(pointer p)
    {
        return ::operator delete(p);
    }

    void construct(pointer p, const_reference value)
    {
        ::new(p) T(value); //erstelle an position p das objekt T mit dem wert value
    }

    void destroy(pointer p)
    {
        p-&gt;~value_type();
    }
};
</code></pre>
<p>dabei fällt mir gerade auf, dass ich vergessen hatte, destroy in dem bsp aufzurufen...</p>
<p>bb</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1745787</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1745787</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Sun, 19 Jul 2009 14:05:06 GMT</pubDate></item></channel></rss>