Dynamic array reference within function

Hey Guys, just a quick questions about what I am doing here. This function currently works, but I feel I may be violating the standard rule by not deleting this dynamic array here.

Basically, I have created a pointer variable in my main program, called time, step here is supposed to be a pointer reference to time. The issue is I cannot delete 'step'(
delete [] step
) in this function because if I should, I will lose the values that should be read into *time (which is my pointer variable in main).

So my question is; is this standard practice? And would this not affect the working of my program as it a quite a big project and I wouldn't want any issues going forward.

Thanks in anticipation.

Cheers.




1
2
3
4
5
6
7
 void time_file(ifstream &infile, double*& step)
{
     infile >> n;
     step = new double [n];
     for (int i = 0; i < n; i++) infile >> step[i];
}


Last edited on
Hi,

If you are writing this project, there is no reason why you should ever use new at all, consider using std::vector ,and all those nasty memory management problems will go away :+)

Are you aware that STL containers store their data on the heap?

Good Luck !!
Thanks for the idea, I have gone with vectors and all seem to work well now.

And yes, I am aware, thank you.
Registered users can post here. Sign in or register to post.