<?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>analog feelings, digital world &#187; c++</title>
	<atom:link href="http://notonlyzeroesandones.site40.net/tag/cpp/feed/" rel="self" type="application/rss+xml" />
	<link>http://notonlyzeroesandones.site40.net</link>
	<description>code is the modern poetry ;)</description>
	<lastBuildDate>Mon, 05 Oct 2009 22:06:02 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Multidimensional arrays in C++</title>
		<link>http://notonlyzeroesandones.site40.net/2009/02/28/multidimensional-arrays-in-c/</link>
		<comments>http://notonlyzeroesandones.site40.net/2009/02/28/multidimensional-arrays-in-c/#comments</comments>
		<pubDate>Sat, 28 Feb 2009 22:47:47 +0000</pubDate>
		<dc:creator>Maciek Talaska</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[c++]]></category>

		<guid isPermaLink="false">http://notonlyzeroesandones.site40.net/2009/02/28/multidimensional-arrays-in-c/</guid>
		<description><![CDATA[Dynamic, multidimensional arrays are not supported directly by C++. If you create such an array in following way:
array = new int*[_size];
for( unsigned int i = 0; i &#60; _size; i++ )
    array[i] = new int[_size];
you are in fact creating one dimensional array of pointers to other arrays. Main two disadvantages of this [...]]]></description>
			<content:encoded><![CDATA[<p align="justify">Dynamic, multidimensional arrays are not supported directly by C++. If you create such an array in following way:</p>
<pre class="cpp" name="code">array = new int*[_size];
for( unsigned int i = 0; i &lt; _size; i++ )
    array[i] = new int[_size];</pre>
<p align="justify">you are in fact creating one dimensional array of pointers to other arrays. Main two disadvantages of this approach:</p>
<ul>
<li>
<div align="justify">Initialization can take a while (image really big 2-3 dimensional array, lets say: 16384 x 16384: how many times memory allocation is being performed? 1 + 16384. Quite a lot, right?</div>
</li>
<li>
<div align="justify">dynamic arrays created like in the code above can not be used as a parameters for memcmp, memcpy, memset &amp; similar functions. the memory allocated is not a <strong>continuous</strong> memory block.</div>
</li>
</ul>
<p align="justify">As I needed a 2-dimensional array that occupies <strong>continuous</strong> block of memory – I started googling :) I have found a great solution by&#160; David Maisonave (Axter) (<a href="http://code.axter.com/allocate3darray.c">code</a>, <a href="http://code.axter.com/allocate3darray.h">header</a>). The code above could be rewritten as follows:</p>
<pre class="cpp" name="code">Pixel** Initialize2dArray( int size )
{
    Pixel **array2d;
    Pixel *temp;
    array2d = new Pixel*[size];
    temp = new Pixel[size * size];

    for( int i = 0; i &lt; size; i++ )
    {
        array2d[i] = temp;
        temp += size;
    }
    return array2d;
}

void Deinitialize2dArray( Pixel** array2d )
{
    delete [] *array2d;
    delete [] array2d;
}</pre>
<p>&#160;</p>
<p align="justify">How does it work? The idea is to put in every ‘cell’ of the array a pointer that will point to a part of the ‘temp’ array. The parts are of the ‘width’ size. So, when you type array[2][3] it actually selects the data that is indexed 2 * width + 3 = exactly what you’re expecting, right? But beware, there is a little difference when using array created in described way and all those mem* functions I wrote at the beginning, instead of writing:</p>
<pre class="cpp" name="code">memcpy( array2d, source, sizeof(int) * size * size );</pre>
<p>you actually need to write:</p>
<pre class="cpp" name="code">memcpy( &amp;array2d[0][0], source, sizeof(int) * size * size );</pre>
<p align="justify">It is great, right? And, of course, you could try using std::vector (it allocates continuous block of memory – correct me if I am wrong) or <a href="http://www.boost.org/doc/libs/1_38_0/libs/multi_array/doc/user.html">Boost Multi Array</a> instead – but if you’re worrying about performance&#8230; I think the presented solution is the best.</p>
]]></content:encoded>
			<wfw:commentRss>http://notonlyzeroesandones.site40.net/2009/02/28/multidimensional-arrays-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CUDA &amp; C++ integration</title>
		<link>http://notonlyzeroesandones.site40.net/2009/02/22/cuda-c-integration/</link>
		<comments>http://notonlyzeroesandones.site40.net/2009/02/22/cuda-c-integration/#comments</comments>
		<pubDate>Sun, 22 Feb 2009 21:38:54 +0000</pubDate>
		<dc:creator>Maciek Talaska</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[CUDA]]></category>

		<guid isPermaLink="false">http://notonlyzeroesandones.site40.net/2009/02/22/cuda-c-integration/</guid>
		<description><![CDATA[It is kind of a strange, but it is not so simple as you may think. There is a sample project in CUDA SDK, and it works, but&#8230; it is strange that if you exclude all .cu and .cpp files and add yours&#8230; surprisingly it stops working. Why? There were some problems while linking: I [...]]]></description>
			<content:encoded><![CDATA[<p align="justify">It is kind of a strange, but it is not so simple as you may think. There is a sample project in CUDA SDK, and it works, but&#8230; it is strange that if you exclude all .cu and .cpp files and add yours&#8230; surprisingly it stops working. Why? There were some problems while linking: I got messages that there are bunch of symbols already defined elsewhere. Well&#8230; in this case it is because of <strong>LIBCMTD</strong> (but read further please, it is not always the problem with LIBCMTD) – default library that is being linked by VS. How to overcome this problem? There are two solutions:</p>
<ul>
<li>
<div align="justify">you may add “/FORCE:MULTIPLE” in linker options (Project properties –&gt; Linker –&gt; Command Line) – but this does work&#8230; partially – it creates proper executable, but in my case I got some assert exception at the very end of executing my app (actually it was when all resources were to be freed – but I didn’t want to investigate it further – app worked, but it is strange to see the exception dialog all the time)</div>
</li>
<li>
<div align="justify">you may exclude one of the libraries that is causing the problem. Which library? Well, it depends on: what was your project type (Linker –&gt; System –&gt; Subsystem: /SUBSYSTEM:CONSOLE, /SUBSYSTEM:WINDOWS&#8230;). In the sample form CUDA SDK, the library that you should get rid of (Linker –&gt; Input –&gt; Ignore Specific Library; remember to write only library’s name, without extension) is: LIBCMTD, in my own project (created from scratch) it is: MSVCRTD for Debug, and LIBCMT for Release. The only advise is to look carefully at what is your linker spitting out, and just ignoring this specific library that makes the whole process to fail (and it is very good idea to have more verbose output: Linker –&gt; General –&gt; Show Progress: /VERBOSE:LIB or even /VERBOSE). In my case the BuildLog.htm (automatically produced by VS) contained following information:</div>
</li>
</ul>
<blockquote><p align="justify">[...]</p>
<p align="justify">MSVCRTD.lib(ti_inst.obj) : error LNK2005: &quot;private: __thiscall type_info::type_info(class type_info const &amp;)&quot; (??0type_info@@AAE@ABV0@@Z) already defined in LIBCMT.lib(typinfo.obj)      <br />MSVCRTD.lib(ti_inst.obj) : error LNK2005: &quot;private: class type_info &amp; __thiscall type_info::operator=(class type_info const &amp;)&quot; (??4type_info@@AAEAAV0@ABV0@@Z) already defined in LIBCMT.lib(typinfo.obj) </p>
<p align="justify">[...]     </p>
<p>Finished searching libraries LINK : warning LNK4098: defaultlib &#8216;MSVCRTD&#8217; conflicts with use of other libs; use /NODEFAULTLIB:library</p>
<p align="justify">[...]</p>
</blockquote>
<blockquote><p align="justify">As you see, the problem lies in linking MSVCRTD.lib. Although it is not always so easy. Sometimes different library should be excluded from the process of linking. </p>
</blockquote>
<p align="justify">It seemed to me, that the problem was fixed. I was wrong. Something bothered me. Why there were no simple solution for this problem? And why the problem was not always with the library the linker had problem with (the one, that it informed about problems with)? And than I started thinking about something I read about how Microsoft C++ compiler / linker works: there are bunch of libraries that are included by default. The libraries are organized in groups (compiled with debug info for debug purposes, etc.), and compilation or linking and mixing libraries with two different worlds may lead to errors. If you right click on your .cu or .cpp file in your project and navigate to “General C++ –&gt; Code Generation” (in case of .cpp file) or “CUDA Build Rule v.2.1.0 –&gt; Hybrid CUDA/C++ Options” (in case of .cu file), you’ll see “Runtime Library” option. And if all your files share the same setting, the problem with linking disappears, and now I am sure, that this is the right solution :) And remember, that this option for all the files must be <strong>exactly</strong> the same! “Multi-Threaded” and “Multi-Threaded DLL” are NOT the same (the have different linker switches), so just be careful, and pay attention what you change.</p>
<p align="justify">I hope it helps someone, because I have wasted <em>a bit</em> of time. </p>
]]></content:encoded>
			<wfw:commentRss>http://notonlyzeroesandones.site40.net/2009/02/22/cuda-c-integration/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Refactor for C++ &#8211; addendum</title>
		<link>http://notonlyzeroesandones.site40.net/2008/12/31/refactor-for-c-addendum/</link>
		<comments>http://notonlyzeroesandones.site40.net/2008/12/31/refactor-for-c-addendum/#comments</comments>
		<pubDate>Wed, 31 Dec 2008 17:31:35 +0000</pubDate>
		<dc:creator>Maciek Talaska</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[tools]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[Refactor for C++]]></category>
		<category><![CDATA[refactoring]]></category>
		<category><![CDATA[Visual Assist X]]></category>
		<category><![CDATA[Visual Studio plugins]]></category>

		<guid isPermaLink="false">http://notonlyzeroesandones.site40.net/2008/12/31/refactor-for-c-addendum/</guid>
		<description><![CDATA[A few days ago I have written a short note in which I have enclosed some of my thoughts about freely available Visual Studio 2005/2008 plugin for C++ developers (DevExpress Refactor for C++). I have pointed out that Refactor had some problems with simple refactorings in one of my projects. The simplified code snippet I [...]]]></description>
			<content:encoded><![CDATA[<p align="justify">A few days ago I have written a short note in which I have enclosed some of my thoughts about freely available Visual Studio 2005/2008 plugin for C++ developers (DevExpress Refactor for C++). I have pointed out that Refactor had some problems with simple refactorings in one of my projects. The simplified code snippet I have provided is not good enough to help understand the problem (the snippet seems to be too simple, and it is impossible to encounter the issue I was writing about). I have spent some of my spare time to find the code that caused problems and try to analyze it and provide a code snippet that illustrates the issue. The following code is very close to the one that I have checked to be error-prone:</p>
<pre class="cpp" name="code">// forward declaration    					//(1)
void functionEx( );    						//(2)
typedef void( *void_function_pointer)(void);			//(3)

class SimpleClass
{
private:
	void (*function_pointer)( );				//(4)
	void_function_pointer func_ptr;

	void SetCallBack( void (*function_pointer)(void) )
	{
		this-&gt;function_pointer = function_pointer;	//(5)
	}

public:
	void function( )
	{
		printf(&quot;<simpleclass />\n&quot;);
	}

	void Prepare( )
	{
		SetCallBack( functionEx );
	}

	void Invoke( )
	{
		function_pointer();
	}

};

SimpleClass *externalObject;

void functionEx( )
{
	externalObject-&gt;function();				//(6)
}

int _tmain(int argc, _TCHAR* argv[])
{
	externalObject = new SimpleClass();
	externalObject-&gt;Prepare();				//(7)
	externalObject-&gt;Invoke();				//(8)

	return 0;
}</pre>
<p align="justify">First three lines (marked with (1), (2) and (3)) are just simple forward declarations of global function and pointer to the function that takes no arguments and does not return anything (&#8217;procedure&#8217; in some languages). Declared function pointer is used inside SimpleClass (4) and SetCallBack assigns argument value (function pointer) to variable inside SimpleClass (5). Global function &#8216;functionEx&#8217; (6) does nothing more than calling &#8216;function&#8217; from object of type &#8216;SimpleClass&#8217;. Where&#8217;s the problem here? It seems everything is simple, right? Right. But it is the simplified version of the code I have <strong>after</strong> I manually renamed function names. The code I was working on was not object-oriented at all &#8211; at the beginning. Let&#8217;s imagine, that the name of global function is not &#8216;functionEx&#8217; but &#8216;function&#8217;. Code does not compile. Ok. Lets use Refactor C++ to rename the name of the global &#8216;function&#8217; to anything that will be different than the name of the method inside SimpleClass. The problem is, Refactor tries to rename things that shouldn&#8217;t be renamed, and skip those, we would like to have renamed. Moving cursor to &#8216;Prepare&#8217; function, selecting &#8216;function&#8217; (remember, we do not have any &#8216;functionEx&#8217; in our code!) makes something strange: refactor tries to rename &#8216;function&#8217; but&#8230; it renames the name of the method inside SimpleClass instead of the global function. And now imagine, that the code is a bit more complex, that there are a couple of files inside the projects &#8211; headers and .cpp files&#8230; This &#8216;unwanted&#8217; refactoring makes more harm than good (in this case). You may say, that it is because of the code, that looks the way it does&#8230; I do not think so. I would expect refactoring tool to be much more &#8216;intelligent&#8217;, but still it is better to have anything (even not the state-of-the-art refactoring tool) than to be forced to make all editing by hand.</p>
<p align="justify">I was very interested if the mentioned earlier Whole Tomato&#8217;s Visual Assist X will be intelligent enough to cope with renaming the &#8216;function&#8217;. Well, Visual Assist X provides its own mechanism for renaming (not so well integrated with Visual Studio editor as in case of Refactor for C++) and it is much easier to perform renaming, because Visual Assist X shows what actions it is going to perform (and you may uncheck those, you don&#8217;t want to happen). Visual Assist X by default wanted to rename the method inside SimpleClass &#8211; just like Refactor for C++. So it seems that there is no perfect solution, and one has to be very careful while refactoring anything that contains any non standard pieces of code. Just be aware that something like the issue I have described above may happen, and that&#8217;s it. </p>
<p>Happy coding in 2009 :)</p>
]]></content:encoded>
			<wfw:commentRss>http://notonlyzeroesandones.site40.net/2008/12/31/refactor-for-c-addendum/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>All developers are equal, but some are more equal&#8230;</title>
		<link>http://notonlyzeroesandones.site40.net/2008/12/25/all-developers-are-equal-but-some-are-more-equal/</link>
		<comments>http://notonlyzeroesandones.site40.net/2008/12/25/all-developers-are-equal-but-some-are-more-equal/#comments</comments>
		<pubDate>Thu, 25 Dec 2008 00:18:55 +0000</pubDate>
		<dc:creator>Maciek Talaska</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[hlsl]]></category>
		<category><![CDATA[Intellishade.net]]></category>
		<category><![CDATA[Refactor for C++]]></category>
		<category><![CDATA[Resharper]]></category>
		<category><![CDATA[Visual Assist X]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[Visual Studio plugins]]></category>

		<guid isPermaLink="false">http://notonlyzeroesandones.site40.net/2008/12/25/all-developers-are-equal-but-some-are-more-equal/</guid>
		<description><![CDATA[Microsoft Visual C++ from Visual C# developer perspective&#8230;
After a while developing apps only using .NET (mainly C#) I felt a need to get a deep dive into C++ programming (DirectX). First impressions? It seems that VS is completely different (much harder) to use if you are C++ developer. There are many things that do not [...]]]></description>
			<content:encoded><![CDATA[<p align="justify"><strong>Microsoft Visual C++ from Visual C# developer perspective&#8230;</strong></p>
<p align="justify">After a while developing apps only using .NET (mainly C#) I felt a need to get a deep dive into C++ programming (DirectX). First impressions? It seems that VS is completely different (much harder) to use if you are C++ developer. There are many things that do not work as good as when using C#. First problem is that Intellisense for C++ is much poorer. You got to deal with .ncb files (sometimes the only solution is to delete .ncb file in your project and then&#8230; surprisingly IntelliSense gets back from grave).</p>
<p align="justify">Worth noting is that DevXpress (Microsoft partner) released another great plugin for Visual Studio without charging for it: <a href="http://www.devexpress.com/Products/Visual_Studio_Add-in/RefactorCPP/">Refactor for C++</a> (the plugin is mentioned also on MSDN site: <a title="http://msdn.microsoft.com/en-us/visualc/bb737896.aspx" href="http://msdn.microsoft.com/en-us/visualc/bb737896.aspx">http://msdn.microsoft.com/en-us/visualc/bb737896.aspx</a>). It is definitely something you should give a try (and I bet you won&#8217;t stop using it). Don&#8217;t be fooled by installer &#8211; the latest version DOES work with Visual Studio 2008 (and not only 2005 as you could think from what is being displayed during installation). Having Refactor for C++ is much better than not having any opportunity to refactor code, but&#8230; there are many things that any Visual C# developer is used to, and think of as a &#8216;must be&#8217; feature of Visual Studio IDE. The refactorings offered by DevExpress&#8217; product are really simple, do not expect possibility to move function body between header / cpp file, extracting parts of a function to another one does not work as I expected, and even simple renaming caused me some problems. Let&#8217;s take under consideration following piece of code:</p>
<pre class="cpp" name="code">// forward declaration
void RenamedFunction( );

class SimpleClass
{
public:
    void function( )
    {
        printf("&lt;SimpleClass/&gt;n");
    }
};

SimpleClass *externalObject;

void RenamedFunction( )
{
    externalObject-&gt;function();
}</pre>
<p align="justify">As you can see Function is a global function name, and also a name of the SimpleClass. I had similar situation in my code. I wanted to rename Function, and what happend? Well&#8230; Refactor renamed it, but&#8230; it also renamed the name of the method (SimpleClass-&gt;Function became SimpleClass-&gt;NewName). I have fixed function names in several places, and created a new project to test if it is safe to rename functions using Refactor for C++ but I couldn&#8217;t get the same behaviour &#8211; so maybe it really WAS my fault ;) Well&#8230; I have checked it in my project&#8230; and yes, it still renames the global function AND method inside SimpleClass&#8230; I wonder why this bug does not occur in the sample I have provided above&#8230; I&#8217;ll try to investigate this issue further on.</p>
<p align="justify">Most of the things I am missing in Visual Studio IDE itself and Refactor for C++ are available through great plugin by <a href="http://www.wholetomato.com/">Whole Tomato Software &#8211; Visual Assist X</a>. I am sure, you have heard of its biggest opponent &#8211; <a href="http://www.jetbrains.com/resharper/features/index.html">Resharper</a> from <a href="http://www.jetbrains.com/index.html">JetBrains</a>, so why should be interested in <a href="http://www.wholetomato.com/">Visual Assist X</a>? The answer is simple, if you never ever use any other language than C#/VB.NET &#8211; you have two options: <a href="http://www.wholetomato.com/">Visual Assist X</a> or <a href="http://www.jetbrains.com/resharper/features/index.html">Resharper</a> &#8211; they both increase coding speed and make you being less afraid even if there are some complicated refactorings to be made. And if sometimes you use C++ as your development language (especially unmanaged)&#8230;<a href="http://www.wholetomato.com/">Visual Assist X</a> is the only option available.</p>
<p align="justify">The last thing that made me mad was&#8230; VS seems to &#8216;lost&#8217; support for .hlsl / fx files (pixel/vertex/geometry shaders written in HLSL / Cg). It is kind of a strange, because I remember that it worked long, long time ago (around August 2005 &#8211; Visual Studio 2005 Beta with DirectX SDK from August 2005 as far as I can remember). Why doesn&#8217;t it work now? It is hard to say. I don&#8217;t even know if it&#8217;s an issue of VS2008/2005 or just something was broken inside DirectX SDK (I am currently using November 2008). It is funny how quick one became used to something that was a &#8216;wow&#8217; feature not so long ago :) I missed syntax highlighting in effect / hlsl files so much, that I started looking for some alternative or plugin for VS or some .xsd file (with definition of shader key words) at least&#8230; And I found one &#8211; <a href="http://intelishade.net/">Intellishade.Net</a>. It is great that someone gives for free something I thought Microsoft offers :)</p>
<p align="justify">After all this, I am really curious what has been said on one of the PDC 2008 sessions concerning new Visual Studio (I think the title of the session was: &#8220;VC10 is the new VC6&#8243;). I really hope that there will be enough encouragement from all C++ developers, so that MS creates a lot more convenient and powerful IDE for VC++ developers. There is still need for the good C++ IDE for Windows, and I think it is much behind Visual C#. C++ is not dead, and it seems that it won&#8217;t be for a long time.</p>
]]></content:encoded>
			<wfw:commentRss>http://notonlyzeroesandones.site40.net/2008/12/25/all-developers-are-equal-but-some-are-more-equal/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

<!-- www.000webhost.com Analytics Code -->
<script type="text/javascript" src="http://analytics.hosting24.com/count.php"></script>
<noscript><a href="http://www.hosting24.com/"><img src="http://analytics.hosting24.com/count.php" alt="web hosting" /></a></noscript>
<!-- End Of Code -->
