I'm developing a physics simulation, and as I'm rather new to programming, I keep running into problems when producing large programs (memory issues mainly). I know about dynamic memory allocation and deletion (new / delete, etc), but I need a better approach to how I structure the program.
Let's say I'm simulating an experiment which is running for a few days, with a very large sampling rate. I'd need to simulate a billion samples, and run over them.
As a super-simplified version, we'll say a program takes voltages V[i], and sums them in fives:
i.e. NewV[0] = V[0] + V[1] + V[2] + V[3] + V[4]
then NewV[1] = V[1] + V[2] + V[3] + V[4] + V[5]
then NewV[2] = V[2] + V[3] + V[4] + V[5] + V[6] ...and this goes on for a billion samples.
In the end, I'd have V[0], V[1], ..., V[1000000000], when instead the only ones I'd need to store for the next step are the last 5 V[i]s.
How would I delete / deallocate part of the array so that the memory is free to use again (say V[0] after the first part of the example where it is no longer needed)? Are there alternatives to how to structure such a program?
I've heard about malloc / free, but heard that they should not be used in C++ and that there are better alternatives.
Thanks very much!
tldr; what to do with parts of arrays (individual elements) I don't need anymore that are taking up a huge amount of memory?
V
instead of in a new array. Fundamentally, though, I think your issue is either in your algorithms or your data structures, and since we don't have any details, it's hard to know how to do it efficiently.