views:

153

answers:

4

Is there a placement new in .NET (like C++)?

In other words if I allocate some memory in VB.NET


Dim Foo(64) as Byte

I want to instance BarClass in the Foo memory (something like...)


Dim Bar as New BarClass(Foo)

However, I don't see any syntax to do this.

Instead I have to do something like:


Dim Foo(1) as BarClass
Foo(0) = new BarClass(x, y, z)
Foo(1) = new BarClass(x, y, z)
+1  A: 

No, there is no equivelent in .NET.

In addition, the GC is free to move your object around in memory whenever it wants (provided it hasn't been pinned to a location). This type of behavior is typically something that's avoided in most .NET code.

You can work around this by using native code and interop.

Reed Copsey
Interesting, I would say that once instanced the GC could still move the memory as the reference is updated anyway. So, what is a good (efficient) syntax to initialize an array of classes... I know, I know use a list.
Just initialize them in a tight loop. One benefit of the GC (when compared to C++) is that memory initialization of classes is typically MUCH faster, so these types of games don't matter anymore. The GC will automatically move things to compact memory, and the allocations are usually just a clearing of bits, not a full OS memory allocation, so it's tons faster than C++ in most cases.
Reed Copsey
A: 

Take a look at LayoutKind. Setting to explicit will give you c++ union like behaviour.

DanDan
This is very different than placement new. Placement new lets you place any object (in C++) in any specific location of memory... It has more to do with fine grained control of memory allocations, which in .NET is all handled by the GC.
Reed Copsey
A: 

The reason you use placements in C++ is because when you ask for 64 bytes of memory, the operating system really gives you 64 bytes of memory. So if you need to create a bunch of objects at once it's more efficient to allocate a larger block of memory up front and create each new object with a segment of that block.

In managed languages like C#, this is already done for you. When you ask for 64 bytes of memory the framework allocates an entire block and gives you 64 bytes out of that block (or another previously allocated block).

Joel Coehoorn
I don't believe your first sentence is true Joel. The operating system is going give the malloc call whatever the page size is. 4k for Intel? So at minimum you're going to get 4k.
A: 

Why don't you just do an array initialization? Dim values() As BarClass = {new BarClass(x,y,z), new BarClass(x,y,z)}

Rune FS
They are probably working with arrays much larger than 1 or 2 elements making that construct tedious at best. As another poster stated, a loop to initialize each element is best.
well you might vrey well be right but the question is talking about 64 bytes, and unless it's an array of bits you can't store a lot of entries in that space. I definately see your point and that might very well be the intent of the question but it's not the wording of the question
Rune FS