Code Snippet I : Wrapper for CLR string to native string marshalling
A frequent (too much frequent) need in Managed C++ is going from System::String objects to char* or wchar_t* strings. The boring side of the marshalling is that the memory allocated with the StringToHGlobalxxx functions should be freed with a matching call to FreeHGlobal.
The solution is simple : using the power of C++ destructors we can create a temporary object for the entire time we need the string. Whenever the object goes out of scope, the string is automagically freed.
You can find the code (released to public domain) here.
Please note that assignments (and copy-constructor) are disabled (they are in an empty private method) to preserve the destruction safety (the free is thus execute once and only once).
As an example you can do :
void mystrcpy(char* dest, const System::String __gc* source)
{
strcpy(dest, StringAutoMarshal(source));
}
with no need to manage the marshalling manually.
.NET Type Viewer
I’ve uploaded a page with a small utility I’ve written yesterday in about 2 hours to solve a problem I had with some code of mine.
.NET Type Viewer is a small program using reflection to inspect .NET modules, with special power features for mixed mode DLLs.
Hanoi towers as a pure recursion benchmark, part 2 - Code Optimizations
As an aside from the 1st part, we try to optimize the performances of the solutions found in part 1, with special attention to the assembly version.
First, let’s take two timings of the versions presented in part 1, compiling in release with optimizations turned on, running on an Athlon64 3000+ 1.8GHz and with times taken with timeGetTime.
These are the times (native C++, assembly) :
- Assembly : 1012ms
- C++ : 1605ms
There are a couple of optimization we could try to make the code faster.
Hanoi towers as a pure recursion benchmark, part 1 - Introduction
The final purpose of this will be analyzing the performance differences between native C++ and managed C++, with a focus on them coexisting in the same process.
Too often, whenever one mentions .NET technologies, he gets various comments on performance hits for his applications. Is this truth or myth ?
The first benchmark we’re going to use is a simple one. Simple doesn’t mean it isn’t effective as a measurement solution; however its result might not reflect real world situations. The example is a classic problem in recursion : Hanoi towers. You can get more information, as well as a running demo of the problem, here.
