<?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[placement new auf stack]]></title><description><![CDATA[<p>geht das hier ohne weiteres:</p>
<pre><code class="language-cpp">#include &lt;new&gt;

template &lt;typename T&gt;
void foo()
{
  char data[sizeof(T)];
  T* these = new( static_cast&lt;void*&gt;(data) ) T();

  std::cout &lt;&lt; *these &lt;&lt; std::endl; //oder auch was sinnvolleres^^

  these-&gt;~T();
}
</code></pre>
<p>oder muss ich mir da irgendwelche gedanken machen, dass es probleme gibt?</p>
<p>hintergrund:<br />
ich möcht nen allocator schreiben, der für eine (zur compilezeit bekannte zahl) an objekten speicher auf dem stack reserviert...<br />
soll auch nur für nen vector sein - also muss ich mich auch nicht drum kümmern, welche speicherstücken weg sind sondern gebe gleich alles raus, was da ist und schmeiß einfach <code>bad_alloc</code> , wenn mehr angefordert wird...<br />
eigtl würd nen c-array fast schon reichen, aber das wär mir zu viel arbeit, da nen vector-clon zu schreiben, der dann doch nicht mehr kann^^</p>
<p>danke schon mal <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f921.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--clown_face"
      title=":clown:"
      alt="🤡"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/topic/255621/placement-new-auf-stack</link><generator>RSS for Node</generator><lastBuildDate>Fri, 11 Sep 2026 21:39:10 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/255621.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 02 Dec 2009 21:39:09 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to placement new auf stack on Wed, 02 Dec 2009 21:39:09 GMT]]></title><description><![CDATA[<p>geht das hier ohne weiteres:</p>
<pre><code class="language-cpp">#include &lt;new&gt;

template &lt;typename T&gt;
void foo()
{
  char data[sizeof(T)];
  T* these = new( static_cast&lt;void*&gt;(data) ) T();

  std::cout &lt;&lt; *these &lt;&lt; std::endl; //oder auch was sinnvolleres^^

  these-&gt;~T();
}
</code></pre>
<p>oder muss ich mir da irgendwelche gedanken machen, dass es probleme gibt?</p>
<p>hintergrund:<br />
ich möcht nen allocator schreiben, der für eine (zur compilezeit bekannte zahl) an objekten speicher auf dem stack reserviert...<br />
soll auch nur für nen vector sein - also muss ich mich auch nicht drum kümmern, welche speicherstücken weg sind sondern gebe gleich alles raus, was da ist und schmeiß einfach <code>bad_alloc</code> , wenn mehr angefordert wird...<br />
eigtl würd nen c-array fast schon reichen, aber das wär mir zu viel arbeit, da nen vector-clon zu schreiben, der dann doch nicht mehr kann^^</p>
<p>danke schon mal <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f921.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--clown_face"
      title=":clown:"
      alt="🤡"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1816760</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1816760</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Wed, 02 Dec 2009 21:39:09 GMT</pubDate></item><item><title><![CDATA[Reply to placement new auf stack on Wed, 02 Dec 2009 21:49:51 GMT]]></title><description><![CDATA[<p>Ich finde, dass es gefährlich aussieht, kann aber nicht sagen, ob es Probleme geben kann..</p>
<p>Allerdings verstehe ich deinen Grund auch nicht ganz. Für was musst du noch was allokieren, wenn du den Speicher eh schon bereit hast? Da eine Range und ggf. einen Zeiger auf das &quot;Ende&quot; zu halten ist ja kein Problem und bringt dir das selbe..</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1816766</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1816766</guid><dc:creator><![CDATA[drakon]]></dc:creator><pubDate>Wed, 02 Dec 2009 21:49:51 GMT</pubDate></item><item><title><![CDATA[Reply to placement new auf stack on Wed, 02 Dec 2009 21:55:52 GMT]]></title><description><![CDATA[<p>naja, so gibts z.bsp. kein back_inserter... <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="😞"
    /></p>
<p>bb</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1816772</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1816772</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Wed, 02 Dec 2009 21:55:52 GMT</pubDate></item><item><title><![CDATA[Reply to placement new auf stack on Wed, 02 Dec 2009 22:02:17 GMT]]></title><description><![CDATA[<p>Hmm. Was mich da einfach stutzig macht ist, dass du 2 mal den gleichen Speicher anforderst und das einmal statisch und einmal dynamisch. Und vom dynamischen Teil hast du gerade mal was davon, dass da ein konstruiertes Objekt abgelegt wird. Aber das könntest du imo auch billiger haben..</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1816777</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1816777</guid><dc:creator><![CDATA[drakon]]></dc:creator><pubDate>Wed, 02 Dec 2009 22:02:17 GMT</pubDate></item><item><title><![CDATA[Reply to placement new auf stack on Wed, 02 Dec 2009 22:07:50 GMT]]></title><description><![CDATA[<p>Für richtige Ausrichtung ist noch zu sorgen:</p>
<pre><code class="language-cpp">#include &lt;type_traits&gt;

union
{
    char data[sizeof(T)];
    typename std::tr1::aligned_storage&lt;sizeof(T),std::tr1::alignment_of&lt;T&gt;::value&gt;::type align;
};
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1816782</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1816782</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Wed, 02 Dec 2009 22:07:50 GMT</pubDate></item><item><title><![CDATA[Reply to placement new auf stack on Wed, 02 Dec 2009 22:38:20 GMT]]></title><description><![CDATA[<p>&quot;dass du 2 mal den gleichen Speicher anforderst&quot;<br />
Imho forder ich ihn nur einmal an... wo soll denn das zweite mal sein? (das das erste mal die deklaration des arrays ist - da solltest du mir ja recht geben!?^^)</p>
<p>&quot;Aber das[konstruiertes objekt ablegen] könntest du imo auch billiger haben&quot;<br />
hmm... imho kostet placement new so gut wie nix und wird in meinem anwendungsfall vrmtl so und so wegoptimiert, weil ich es nur für pods nutze...</p>
<p>der allocator an sich würde so aussehen:</p>
<pre><code class="language-cpp">#ifndef H_MY_ALLOCATOR_ALLOCATE_AT_STACK_INCLUDED_20091202221055
#define H_MY_ALLOCATOR_ALLOCATE_AT_STACK_INCLUDED_20091202221055

#include &lt;cstddef&gt;
#include &lt;new&gt;
#include &lt;stdexcept&gt;

#ifdef _MSC_VER
#	pragma warning(push) //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

namespace my
{
	template &lt;typename T, std::size_t count&gt;
	struct stack_allocator;

	template &lt;std::size_t count&gt;
	struct stack_allocator &lt;void, count&gt;
	{
	public:
		typedef void* pointer;
		typedef const void* const_pointer;

		typedef void value_type;

		template &lt;typename U&gt;
		struct rebind
		{
			typedef stack_allocator&lt;U, count&gt; other;
		};
	};

	template &lt;typename T, std::size_t count&gt;
	struct stack_allocator
	{
	public:
		enum
		{
			allocating_size = count,
			allocating_byte = allocating_size*sizeof(T)
		};

		typedef std::size_t size_type;
		typedef std::ptrdiff_t difference_type;

		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;

		template &lt;typename U&gt;
		struct rebind
		{
			typedef stack_allocator&lt;U, count&gt; other;
		};

		stack_allocator() throw()
		{}

		stack_allocator(const stack_allocator&amp;) throw()
		{}

		template &lt;typename U, std::size_t Ucount&gt;
		stack_allocator(const stack_allocator&lt;U, Ucount&gt;&amp;) throw()
		{}

		~stack_allocator() throw()
		{}

		pointer address(reference x) const
		{
			return &amp;x;
		}

		const_pointer address(const_reference x) const
		{
			return &amp;x;
		}

		pointer allocate(size_type n, stack_allocator&lt;void, 0&gt;::const_pointer /*hint*/ = 0)
		{
			if(n &gt; allocating_size)
				throw std::bad_alloc();

			if(!empty)
				throw std::bad_alloc();

			empty = false;
			return reinterpret_cast&lt;pointer&gt;( data );
		}

		void deallocate(pointer /*p*/, size_type /*n*/)
		{
			/* nothing to do */
		}

		size_type max_size() const throw()
		{
			return allocating_size;
		}

		void construct(pointer p, const_reference val)
		{
			new ( static_cast&lt;void*&gt;(p) ) T(val);
		}

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

	private:
		bool empty;
		char data[allocating_byte];
	};

	template &lt;typename T, std::size_t Tc, typename U, std::size_t Uc&gt;
	bool operator== (const stack_allocator&lt;T, Tc&gt;&amp; /*lhs*/, const stack_allocator&lt;U, Uc&gt;&amp; /*rhs*/) throw()
	{
		return true;
	}

	template &lt;typename T, std::size_t Tc, typename U, std::size_t Uc&gt;
	bool operator!= (const stack_allocator&lt;T, Tc&gt;&amp; /*lhs*/, const stack_allocator&lt;U, Uc&gt;&amp; /*rhs*/) throw()
	{
		return false;
	}
}

#ifdef _MSC_VER
#	pragma warning(pop)
#endif

#endif //#ifndef H_MY_ALLOCATOR_ALLOCATE_AT_STACK_INCLUDED_20091202221055
</code></pre>
<p>sieht eigtl nicht so verkehrt aus...</p>
<p>und hier mal noch nen minimalbsp.:</p>
<pre><code class="language-cpp">#include &quot;allocate_at_stack.h&quot;

#include &lt;vector&gt;
#include &lt;iostream&gt;

int main()
{
	typedef std::vector&lt; int, my::stack_allocator&lt;int, 2048&gt; &gt; my_vector;
	my_vector the_vector;

	the_vector.reserve(2048);
	for(int tmp; std::cin &gt;&gt; tmp; the_vector.push_back(tmp))
		;

	for(my_vector::const_iterator i(the_vector.begin()), e(the_vector.end()); i != e; ++i)
		std::cout &lt;&lt; *i &lt;&lt; std::endl;

	system(&quot;PAUSE&quot;);
}
</code></pre>
<p>bb</p>
<p>edit:<br />
empty = true beim reserve ist natürlich doof xD<br />
fällt euch ne idee ein, wie man das erste reserve auf die max.-größe automatisieren kann?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1816799</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1816799</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Wed, 02 Dec 2009 22:38:20 GMT</pubDate></item><item><title><![CDATA[Reply to placement new auf stack on Wed, 02 Dec 2009 22:28:21 GMT]]></title><description><![CDATA[<p>camper schrieb:</p>
<blockquote>
<p>Für richtige Ausrichtung ist noch zu sorgen:</p>
<pre><code class="language-cpp">#include &lt;type_traits&gt;

union
{
    char data[sizeof(T)];
    typename std::tr1::aligned_storage&lt;sizeof(T),std::tr1::alignment_of&lt;T&gt;::value&gt;::type align;
};
</code></pre>
</blockquote>
<p>sicher, dass das notwendig ist?<br />
imho gibt sizeof ja eh schon die größe + alignment aus!? oder ist das zufall oder ist das nur nicht relevant?^^</p>
<p>bb</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1816800</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1816800</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Wed, 02 Dec 2009 22:28:21 GMT</pubDate></item><item><title><![CDATA[Reply to placement new auf stack on Wed, 02 Dec 2009 22:42:33 GMT]]></title><description><![CDATA[<p>Dein char-Array könnte sich aber an einer Position befinden, die das nötige Alignment für den Typ T nicht ermöglicht.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1816813</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1816813</guid><dc:creator><![CDATA[Mitleid]]></dc:creator><pubDate>Wed, 02 Dec 2009 22:42:33 GMT</pubDate></item><item><title><![CDATA[Reply to placement new auf stack on Wed, 02 Dec 2009 22:55:19 GMT]]></title><description><![CDATA[<p>hmm... stimmt, der anfang nat. nicht^^</p>
<p>würde das hier:</p>
<pre><code class="language-cpp">char data[allocating_size*sizeof(T) + std::tr1::alignment_of&lt;T&gt;];
</code></pre>
<p>nicht schon reichen?<br />
dann müsste ich(muss ich überhaupt?) nur noch mit bissl mit modulo rumspielen, um ne andere startadresse rauszugeben, richtig!?</p>
<p>falls das nicht stimmt, würde mir auch nen link reichen - hab gerade keine wirklich gute lektüre zu diesem thema finden können <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=":/"
      alt="😕"
    /></p>
<p>bb</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1816817</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1816817</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Wed, 02 Dec 2009 22:55:19 GMT</pubDate></item><item><title><![CDATA[Reply to placement new auf stack on Wed, 02 Dec 2009 23:01:45 GMT]]></title><description><![CDATA[<p>unskilled schrieb:</p>
<blockquote>
<p>hmm... stimmt, der anfang nat. nicht^^</p>
<p>würde das hier:</p>
<pre><code class="language-cpp">char data[allocating_size*sizeof(T) + std::tr1::alignment_of&lt;T&gt;];
</code></pre>
<p>nicht schon reichen?<br />
dann müsste ich(muss ich überhaupt?) nur noch mit bissl mit modulo rumspielen, um ne andere startadresse rauszugeben, richtig!?</p>
</blockquote>
<p>-1 reicht schon. Allerdings gibt es keinen standardkonformen Weg die Ausrichtung eines Zeigers (data) zu ermitteln. Damit hast du keine Rechengrundlage mehr.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1816821</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1816821</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Wed, 02 Dec 2009 23:01:45 GMT</pubDate></item><item><title><![CDATA[Reply to placement new auf stack on Wed, 02 Dec 2009 23:23:35 GMT]]></title><description><![CDATA[<p>camper schrieb:</p>
<blockquote>
<p>-1 reicht schon.</p>
</blockquote>
<p>stimmt^^</p>
<p>camper schrieb:</p>
<blockquote>
<p>Allerdings gibt es keinen standardkonformen Weg die Ausrichtung eines Zeigers (data) zu ermitteln. Damit hast du keine Rechengrundlage mehr.</p>
</blockquote>
<p>Jopp, ist mir bekannt - macht mir aber nicht so viel aus - gehen tut es in der praxis definitiv - und mir fällt außer &quot;der standard gibt keine garantien darüber&quot; kein grund ein, wieso ichs nciht tun 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>bb</p>
<p>edit:<br />
hmm...<br />
ich hätt es jz so:</p>
<pre><code class="language-cpp">template &lt;typename T, std::size_t count&gt;
struct stack_allocator
{
	enum
	{
		allocating_size = count,
		allocating_byte = allocating_size*sizeof(T) + std::tr1::alignment_of&lt;T&gt;-1
	};

/*...*/

	pointer allocate(size_type n, stack_allocator&lt;void, 0&gt;::const_pointer /*hint*/ = 0)
	{
		if(n &gt; allocating_size)
			throw std::bad_alloc();

		if(!empty)
			throw std::bad_alloc();

		empty = false;
		size_type begin = reinterpret_cast&lt;size_type&gt;( data );
		size_type offset = begin%std::tr1::alignment_of&lt;T&gt;::value;
		pointer first = reinterpret_cast&lt;pointer&gt;( begin+offset );
		return first;
	}
};
</code></pre>
<p>allerdings muss ich erst mal nen tr1 update suchen xD<br />
iwann hatte ich das doch mal installiert... *grml*</p>
<p>bb</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1816826</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1816826</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Wed, 02 Dec 2009 23:23:35 GMT</pubDate></item><item><title><![CDATA[Reply to placement new auf stack on Wed, 02 Dec 2009 23:28:54 GMT]]></title><description><![CDATA[<p>unskilled schrieb:</p>
<blockquote>
<p>camper schrieb:</p>
<blockquote>
<p>-1 reicht schon.</p>
</blockquote>
<p>stimmt^^</p>
</blockquote>
<p>Was!? <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="😕"
    /> Steh ich auf dem Schlauch? Wieso stimmt das?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1816835</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1816835</guid><dc:creator><![CDATA[Mitleid]]></dc:creator><pubDate>Wed, 02 Dec 2009 23:28:54 GMT</pubDate></item><item><title><![CDATA[Reply to placement new auf stack on Wed, 02 Dec 2009 23:35:43 GMT]]></title><description><![CDATA[<p>hmmm - ist wie bei restklassen... nen tollerer vergleich fällt mir gerad nicht ein...</p>
<p>bsp.:<br />
alignment = 4</p>
<p>start-adr.: 1</p>
<pre><code>1
2
3
4 -&gt; OK
</code></pre>
<p>=&gt; +3</p>
<p>start-adr.: 0</p>
<pre><code>0 -&gt; OK
</code></pre>
<p>=&gt; +0</p>
<p>start-adr.: 2</p>
<pre><code>2
3
4 -&gt; OK
</code></pre>
<p>=&gt; +2</p>
<p>start-adr.: 3</p>
<pre><code>3
4 -&gt; OK
</code></pre>
<p>=&gt; +1</p>
<p>max {0, 1, 2, 3} = 3 = 4-1 ;o)</p>
<p>---</p>
<p>bsp.:<br />
alignment = 1<br />
man braucht keine verschiebung, weil jedes element &quot;überall&quot; sein kann ;o)<br />
-&gt; = 0</p>
<p>bb</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1816836</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1816836</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Wed, 02 Dec 2009 23:35:43 GMT</pubDate></item><item><title><![CDATA[Reply to placement new auf stack on Thu, 03 Dec 2009 08:55:05 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>Ich bin mir nicht sicher, meine aber in einem der &quot;effective C++&quot; Bücher von Scott Meyers gelesen zu habe, dass placement new mit einem Stack-Objekt &quot;böse&quot; ist. (Wenn ich dran denke und dazu komme, schaue ich heute abend nach.)</p>
<p>Im übrigen frage ich mich ob nicht boost::array oder std::tr1::array deine Anforderungen nicht auch erfüllen?</p>
<p>Gruß</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1816919</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1816919</guid><dc:creator><![CDATA[Drehleiter]]></dc:creator><pubDate>Thu, 03 Dec 2009 08:55:05 GMT</pubDate></item><item><title><![CDATA[Reply to placement new auf stack on Thu, 03 Dec 2009 10:19:13 GMT]]></title><description><![CDATA[<p>camper schrieb:</p>
<blockquote>
<p>Für richtige Ausrichtung ist noch zu sorgen:</p>
<pre><code class="language-cpp">#include &lt;type_traits&gt;

union
{
    char data[sizeof(T)];
    typename std::tr1::aligned_storage&lt;sizeof(T),std::tr1::alignment_of&lt;T&gt;::value&gt;::type align;
};
</code></pre>
</blockquote>
<p>Hier scheinen einige Leser das</p>
<pre><code class="language-cpp">union
</code></pre>
<p>übersehen zu haben und überlegen nun, wie man das ohne union schafft.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1816964</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1816964</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Thu, 03 Dec 2009 10:19:13 GMT</pubDate></item><item><title><![CDATA[Reply to placement new auf stack on Thu, 03 Dec 2009 11:17:59 GMT]]></title><description><![CDATA[<p>Ich stelle jetzt mal die Gretchenfrage: Welchen Sinn hat placement new mit Stackspeicher?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1817002</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1817002</guid><dc:creator><![CDATA[knivil]]></dc:creator><pubDate>Thu, 03 Dec 2009 11:17:59 GMT</pubDate></item><item><title><![CDATA[Reply to placement new auf stack on Thu, 03 Dec 2009 11:19:32 GMT]]></title><description><![CDATA[<p>knivil schrieb:</p>
<blockquote>
<p>Ich stelle jetzt mal die Gretchenfrage: Welchen Sinn hat placement new mit Stackspeicher?</p>
</blockquote>
<p>Speed natürlich.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1817003</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1817003</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Thu, 03 Dec 2009 11:19:32 GMT</pubDate></item><item><title><![CDATA[Reply to placement new auf stack on Thu, 03 Dec 2009 11:21:24 GMT]]></title><description><![CDATA[<p>Ja, gegenueber new vielleicht, aber was ist denn mit std::array oder aber das Objekt gleich als lokale Variable anlegen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1817004</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1817004</guid><dc:creator><![CDATA[knivil]]></dc:creator><pubDate>Thu, 03 Dec 2009 11:21:24 GMT</pubDate></item><item><title><![CDATA[Reply to placement new auf stack on Thu, 03 Dec 2009 11:25:21 GMT]]></title><description><![CDATA[<p>knivil schrieb:</p>
<blockquote>
<p>Ja, gegenueber new vielleicht, aber was ist denn mit std::array oder aber das Objekt gleich als lokale Variable anlegen?</p>
</blockquote>
<p>Das ist natürlich besser. Deswegen kann ich meiner Vector-Klasse ja auch mitgeben, ob sie Freispeicher benutzt oder ein Member-Array.<br />
std::array ist keine Alternative, da es die supi vector-Funktionalität nicht hat. also push_back.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1817007</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1817007</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Thu, 03 Dec 2009 11:25:21 GMT</pubDate></item><item><title><![CDATA[Reply to placement new auf stack on Thu, 03 Dec 2009 12:16:05 GMT]]></title><description><![CDATA[<p>volkard schrieb:</p>
<blockquote>
<p>camper schrieb:</p>
<blockquote>
<p>Für richtige Ausrichtung ist noch zu sorgen:</p>
<pre><code class="language-cpp">#include &lt;type_traits&gt;

union
{
    char data[sizeof(T)];
    typename std::tr1::aligned_storage&lt;sizeof(T),std::tr1::alignment_of&lt;T&gt;::value&gt;::type align;
};
</code></pre>
</blockquote>
<p>Hier scheinen einige Leser das</p>
<pre><code class="language-cpp">union
</code></pre>
<p>übersehen zu haben und überlegen nun, wie man das ohne union schafft.</p>
</blockquote>
<p>soll so wie heißen, wie ich solls auch mit dem union machen?<br />
in meinen augen ist das bei nem einzelnen element zwar möglich, aber bei mehreren nur platzverschwendung... kann aber auch sein, dass ichs falsch verstanden hab - wie gesagt: hab dazu nix gefunden - allg. ist das tr1 auch 6jahre nach erscheinen noch so schlecht/wenig dokumentiert... :&lt;</p>
<p>volkard schrieb:</p>
<blockquote>
<p>std::array ist keine Alternative, da es die supi vector-Funktionalität nicht hat. also push_back.</p>
</blockquote>
<p>Japp - hatte ich auch weiter oben schon mal geschrieben</p>
<p>Drehleiter schrieb:</p>
<blockquote>
<p>Ich bin mir nicht sicher, meine aber in einem der &quot;effective C++&quot; Bücher von Scott Meyers gelesen zu habe, dass placement new mit einem Stack-Objekt &quot;böse&quot; ist. (Wenn ich dran denke und dazu komme, schaue ich heute abend nach.)</p>
</blockquote>
<p>das wäre cool : &gt;</p>
<p>bb</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1817049</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1817049</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Thu, 03 Dec 2009 12:16:05 GMT</pubDate></item><item><title><![CDATA[Reply to placement new auf stack on Thu, 03 Dec 2009 12:36:54 GMT]]></title><description><![CDATA[<p>unskilled schrieb:</p>
<blockquote>
<p>in meinen augen ist das bei nem einzelnen element zwar möglich, aber bei mehreren nur platzverschwendung...</p>
</blockquote>
<p>Richte doch das ganze Array aus und nicht einzeln jedes Element.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1817069</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1817069</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Thu, 03 Dec 2009 12:36:54 GMT</pubDate></item><item><title><![CDATA[Reply to placement new auf stack on Thu, 03 Dec 2009 12:39:23 GMT]]></title><description><![CDATA[<p>unskilled schrieb:</p>
<blockquote>
<p>volkard schrieb:</p>
<blockquote>
<p>std::array ist keine Alternative, da es die supi vector-Funktionalität nicht hat. also push_back.</p>
</blockquote>
<p>Japp - hatte ich auch weiter oben schon mal geschrieben</p>
</blockquote>
<p>Yup. Aber knivil hat's ignoriert und zur Gretchenfrage gemacht. Deswegen hab ich's ihm nochmal gesagt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1817071</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1817071</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Thu, 03 Dec 2009 12:39:23 GMT</pubDate></item><item><title><![CDATA[Reply to placement new auf stack on Thu, 03 Dec 2009 12:39:51 GMT]]></title><description><![CDATA[<p>volkard schrieb:</p>
<blockquote>
<p>unskilled schrieb:</p>
<blockquote>
<p>in meinen augen ist das bei nem einzelnen element zwar möglich, aber bei mehreren nur platzverschwendung...</p>
</blockquote>
<p>Richte doch das ganze Array aus und nicht einzeln jedes Element.</p>
</blockquote>
<p>Tu ich doch!?</p>
<pre><code class="language-cpp">template &lt;typename T, std::size_t count&gt; 
struct stack_allocator 
{ 
    enum 
    { 
        allocating_size = count, 
        allocating_byte = allocating_size*sizeof(T) + std::tr1::alignment_of&lt;T&gt;-1 
    }; 

/*...*/ 

    pointer allocate(size_type n, stack_allocator&lt;void, 0&gt;::const_pointer /*hint*/ = 0) 
    { 
        if(n &gt; allocating_size) 
            throw std::bad_alloc(); 

        if(!empty) 
            throw std::bad_alloc(); 

        empty = false; 
        size_type begin = reinterpret_cast&lt;size_type&gt;( data ); 
        size_type offset = begin%std::tr1::alignment_of&lt;T&gt;::value; 
        pointer first = reinterpret_cast&lt;pointer&gt;( begin+offset ); 
        return first; 
    } 
};
</code></pre>
<p>oder gibts hier ein problem?<br />
mit offset berechne ich ja, welchen zeiger ich rausgebe und somit, wo das erste element gespeichert wird...</p>
<p>bb</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1817074</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1817074</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Thu, 03 Dec 2009 12:39:51 GMT</pubDate></item><item><title><![CDATA[Reply to placement new auf stack on Thu, 03 Dec 2009 12:42:04 GMT]]></title><description><![CDATA[<p>Dieser Thread macht mich traurig.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1817076</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1817076</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Thu, 03 Dec 2009 12:42:04 GMT</pubDate></item><item><title><![CDATA[Reply to placement new auf stack on Thu, 03 Dec 2009 12:51:18 GMT]]></title><description><![CDATA[<p>volkard schrieb:</p>
<blockquote>
<p>Dieser Thread macht mich traurig.</p>
</blockquote>
<p>wie wärs, wenn du mal sagen würdest, was du für nen fehler hältst!? bzw. was dir hier nicht passt...</p>
<p>bb</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1817090</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1817090</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Thu, 03 Dec 2009 12:51:18 GMT</pubDate></item><item><title><![CDATA[Reply to placement new auf stack on Thu, 03 Dec 2009 13:05:23 GMT]]></title><description><![CDATA[<p>unskilled schrieb:</p>
<blockquote>
<p>volkard schrieb:</p>
<blockquote>
<p>Dieser Thread macht mich traurig.</p>
</blockquote>
<p>wie wärs, wenn du mal sagen würdest, was du für nen fehler hältst!?</p>
</blockquote>
<p>Alles. Vom Ansinnen, so einen allocator zu bauen übers Ignorieren des union-Tricks über Pseudogretchenfragen über falsche Erinnerungen an Scotty bis zum &quot;!?&quot;, wo doch nur ein &quot;?!&quot; passen kann. Es ist ja keiein Ausruf, der als Frage gemein ist, sondern eine Frage, die ausgerufen wird.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1817099</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1817099</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Thu, 03 Dec 2009 13:05:23 GMT</pubDate></item></channel></rss>