<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Axel Schumacher</title>
	<atom:link href="http://www.axelschumacher.fr/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.axelschumacher.fr</link>
	<description>Axel Schumacher</description>
	<lastBuildDate>Tue, 29 May 2012 07:28:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.4</generator>
		<item>
		<title>Call a constructor on an already allocated memory in C++</title>
		<link>http://www.axelschumacher.fr/157/call-a-constructor-on-an-already-allocated-memory-in-c/</link>
		<comments>http://www.axelschumacher.fr/157/call-a-constructor-on-an-already-allocated-memory-in-c/#comments</comments>
		<pubDate>Fri, 25 May 2012 08:45:49 +0000</pubDate>
		<dc:creator>Axel</dc:creator>
				<category><![CDATA[C/C++ tricks]]></category>

		<guid isPermaLink="false">http://www.axelschumacher.fr/?p=157</guid>
		<description><![CDATA[While taking a test for a video games company based in California, I had to create elements in an already allocated buffer. The tricky part of this was that the [...]]]></description>
			<content:encoded><![CDATA[<p>While taking a test for a video games company based in California, I had to create elements in an already allocated buffer.</p>
<p>The tricky part of this was that the constructor of the object saved &#8216;this&#8217; in a attribute field, and the test program always compared this value to the address of the object, failing if different.</p>
<p>As I was working with generic objects, I wasn&#8217;t supposed to know that, but still had to handle it. So I couldn&#8217;t update this address field inside the object after having copied it in my buffer to its new address.</p>
<p>One can think that we just have to call the object constructor again once it has been initialized:</p>
<pre>unsigned size = 10;
// This is already given, we have to cope with it...
char *charBuffer = new char[sizeof(T)*size];
// We have a generic type T with a constructor T(args)
T *buffer = reinterpret_cast&lt;T*&gt;(charBuffer);
for (unsigned i=0; i &lt; sizeof(T)*size; i++) {
    buffer[i].T(args);
}</pre>
<p>But this would mean that we have forgotten that constructors cannot be directly called:</p>
<pre>error: invalid use of ‘T::T(args)’</pre>
<p>The only way was to create the object <strong>directly</strong> inside the buffer, so that the constructor will be called with the correct address.</p>
<p>After having combed the web, I found <a title="Solution post" href="http://stackoverflow.com/questions/827552/explicitly-calling-a-constructor-in-c" target="_blank">this post</a>, giving the solution:</p>
<pre>unsigned size = 10;
// This is already given, we have to cope with it...
char *charBuffer = new char[sizeof(T)*size];
// We have a generic type T with a constructor T(args)
T *buffer = reinterpret_cast&lt;T*&gt;(charBuffer);
for (unsigned i=0; i &lt; size; i++) {
    new (buffer+i)T(args);
}</pre>
<p>This way, each element of my buffer will be constructed with the address it will have for its whole life.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.axelschumacher.fr/157/call-a-constructor-on-an-already-allocated-memory-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ROSE is over, at last&#8230;</title>
		<link>http://www.axelschumacher.fr/32/rose-is-over-at-last/</link>
		<comments>http://www.axelschumacher.fr/32/rose-is-over-at-last/#comments</comments>
		<pubDate>Fri, 29 Apr 2011 18:00:18 +0000</pubDate>
		<dc:creator>Axel</dc:creator>
				<category><![CDATA[Telecom ParisTech]]></category>

		<guid isPermaLink="false">http://www.axelschumacher.fr/?p=32</guid>
		<description><![CDATA[ROSE, for RObotique et Systèmes Embarqués (Robotic and Embedded Systems), my specialization unit is finally over ! After two months of intense work, we are proud to present our Copterix. [...]]]></description>
			<content:encoded><![CDATA[<p>ROSE, for RObotique et Systèmes Embarqués (Robotic and Embedded Systems), my specialization unit is finally over ! After two months of intense work, we are proud to present our <a title="Copterix" href="http://www.axelschumacher.fr/?page_id=17">Copterix</a>.<br />
Come and grab some pieces of information there !</p>
]]></content:encoded>
			<wfw:commentRss>http://www.axelschumacher.fr/32/rose-is-over-at-last/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

