block allocation of classes

N

not_a_commie

I've got a search algorithm where I would like to allocate my nodes in
blocks. In other words, I would like to reserve enough memory for 200k
nodes and then just keep my own pointer into that block. I don't want
to do 200k small memory allocation calls. It seemed that this made
more sense when I was using structs. I could allocate a struct node[]
and then just work through that as I needed more nodes. Can I do the
same thing with classes?
 
N

Nicholas Paldino [.NET/C# MVP]

Technically, you can't, not in the way you would an array of structures,
because each instance of the class has to be placed on the heap and you will
have to call the constructor individually to populate that array.

You would just be moving the time to allocate all those instances from
other areas of your program to the beginning, and I am guessing that it's
not going to make much of a difference.

Have you determined that memory allocation is a bottleneck for your
program? If not, then why change it?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top