views:

291

answers:

7

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.

+1  A: 

Qt seems like an extensive class library for C++.

Tommy Carlier
+1  A: 

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.

Aamir
+1  A: 
  1. 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.

  2. .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...
  1. 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.
Akash Kava
A: 

You can invoke unmanaged code from managed code (via PInvoke), and invoke managed code from unmanaged code (as a kind of COM object).

ChrisW
+3  A: 

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.

Reuben
+1 for C++/CLI. (I only added the period because comments have to be 15 chars long, just thought y'all might like to know that. now with this comment, its way longer than 15 chars)
TJB
(I add spaces when needing to get above that 15 char minimum :-))
Joey
"C++/CLI" seem to mean C++ with managed extensions ;)
FractalizeR
Nope, Managed C++ was a failed experiment, and is not source-compatible with C++/CLI. However, since both are languages targetting the .Net CLR, they are binary compatible. (But so are VB.Net and C#)
MSalters
+1  A: 

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.

Marc Bollinger
An advantage of C++ is that when you deallocate some memory, you know it's gone and a tangible error will occur if something tries to access that memory again. With the garbage collector, an object may not be deallocated at all because some other unknown object is referencing it, causing a subtle memory leak with no error whatsoever. So in that case it can actually be harder to debug a memory leak because the garbage collector decided to be a little too smart.
Phil
However I will admit your point on not having to worry about memory management at all is extremely valid for most situations.
Phil
A: 

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.

rpg