I love C# because the powerful features of the .NET framework make it so easy to develop for Windows. However I also love standard C++ primarily because it gives me fine-tuned control over memory management. Is there a way to have the best of both worlds? Are there C++ libraries that can compete with the rich set of libraries in the .NET framework? Or is there a way to manually deallocate memory in one of the .NET languages that I've never used? OR is it possible to use a .NET dll in a standard C++ application? I know, I'm really stretching here, but I believe in magic.
Are there C++ libraries that can compete with the rich set of libraries in the .NET framework?
QT comes to mind straightaway but I doubt that it is as rich as .NET library.
is there a way to manually deallocate memory in one of the .NET languages that I've never used?
The answer to this is Yes and No. There are certain methods in .NET to take control of memory. There are weak references, strong references, (sort of) Forced Garbage collection etc. But these things are already discussed in huge details here.
Here are some details about how Memory Management in .NET works.
Garbage collection and generations
Best practices of Forcing GC in .NET
Better still, read the links in this search query
is it possible to use a .NET dll in a standard C++ application?
Yes, you can generate COM Wrapper/unmanaged wrappers for .NET DLL and can use it in C++ applications.
You can use .NET dll in C++ project via COM or you can host CLR and interact with CLR entities, it would be quite cumbersome coding but yes its possible. Setting Com Visible true makes .NET object accessible via COM in VB/MFC.
.NET has few methods of Garbage Collector that you can run explicitily to collect garbate, plust it has IDisposable intrface which you can use to dispose your members in class and with "using" keyword you can explicitily call dispose method at end of using example shown below.
using(FileStream fs = File.OpenRead("xyz.txt"){ fs.Read(buff,0,100); } // here fs gets closed in dispose //method automatically...
- Visual C++ CLI project lets you integrate usual C++ and .NET into one project. You can find it in various application wizards of Visual Studio.
You can invoke unmanaged code from managed code (via PInvoke), and invoke managed code from unmanaged code (as a kind of COM object).
Have you looked at Boost?
Alternatively, you can use "C++/CLI" (a.k.a. managed C++, a.k.a. C++.NET); code written in this language can call into .NET APIs and can also manually manage memory via traditional Win32 APIs like HeapAlloc
/HeapFree
. In my experience, though, this language is most frequently used for writing "glue code", and not for building applications from the ground up.
You can also 'pin' segments of code in C#, if you really want to have explicit access pointers and such (from MSDN):
unsafe static void TestMethod()
{
// assume class Point { public int x, y; }
// pt is a managed variable, subject to garbage collection.
Point pt = new Point();
// Using fixed allows the address of pt members to be
// taken, and "pins" pt so it isn't relocated.
fixed (int* p = &pt.x)
{
*p = 1;
}
}
But really, I think you're better off determining why specifically you're looking to manage memory yourself--most developers I know at this point would rather focus on the actual design and construction of systems, rather than worrying about deallocating chunks of memory. There are other good suggestions here, such as Boost (though honestly, if you've got the cajones to announce you're better than the GC, you're probably already aware of Boost), Qt, and .NET bridging (hell, you can host the CLR in an unmanaged process, if you like), but I suspect that for the few places where it actually may behoove you to manage memory yourself, you'll be much better off constraining them to a VC++ project referenced by the rest of your solution.
STL + Boost + Qt is solid competition for C# and .NET.
I had to work on a Compact Framework app recently and I was disappointed with the WinForms way compared to Qt's easy layouts. The disposal pattern was also very annoying, it was not clear which resources needed disposal and which didn't, so I ended up googling for what's safe to apply using() on.
In my opinion, unmanaged resource handling is more complicated than C++'s RAII.