Hey, I hope I'm asking this in the right forum. Anyway, I'm somewhat new to c#, but I've done a lot of programming in C++ and I'm familiar with .NET. I'm working on a program that needs to rapidly recalculate and reset many variables in arrays. Now at first I was going to do it like I would with a C++ program and have a pointer array defined like this (I'll use integers for the example):
Code:
int numbers*;
And then I'd have it combined with a counter to keep track of the number of elements like this:
Code:
int numbers_count;
Then when I need to dynamically add something to the list I'd do it this way
Code:
int temp* = new int[numbers_count];
temp = numbers;
delete [] numbers;
numbers_count ++;
numbers = new int[numbers_count];
numbers[numbers_count-1] = /*the new value*/;
for (int i=0; i<numbers_count-1; i++) {
numbers[i] = temp[i];
}
delete [] temp;
Ok so that was probably a bit verbose, but I think it gets the idea of what I was trying to do across. The function to remove a member from "numbers" was similar. I put this whole thing into C# and the only parts that didn't work were the delete functions and the pointers. So I changed them to regular integers and took out the delete statements. I looked up some help that said that C# has automatic memory handling, so I figured it'd be fine. But when I run this type of code where variables are re-assigned with the "new" statement frequently, the program starts running fast and then drastically slows down over time. It is quickly filling memory that it doesn't need and using more CPU than it should. Normally if I were using c++, I'd say that it was allocating a ton of memory slots and then not deleting them when finished with them. This would also cause problematic memory leaks without the delete statements.
So what I want to know is if there is another, possibly easier, way to do what I described above without filling up ridiculous amounts of memory with useless variables.