1

I am dealing with the following puzzling issue

First of all, I have to open some files and do some operations. for these I have to define some arrays. What I would like to do, is delete the array before going to the next file. I know that I could dynamically define the arrays, but

  1. I don't know a priori the size
  2. I was advised not to mess with dynamic allocation

So is there a way to "erase" a non dynamically-defined array?

A sample code is the following

void Analyze(unsigned int first_run, unsigned int last_run, unsigned int last_segment){

    TFile *fin = new TFile(TString::Format("HPGe_%d_%d_%d.root", first_run, last_run, last_segment));
    for (int segm = 2; segm<=3; segm++){//loop for different files
        std::vector<double> left;
        std::vector<double> right;
        std::vector<int> amplitude;
        Function_that_fill_arrays_PUSHBACK(left, right, amplitude);
        for (int i=0; i<left.size(); i++){
            if (right[i]<=0.01){
                cout << "Left side : " << left[i] << ", Right side : " << right[i] << ", Amplitude : " << amplitude[i] << endl;
            }
        }
        DELETE ARRAYS HERE
    }// end of loop over files

}//end of function

Any idea on how can something like that be achieved?

Thanos
  • 594
  • 2
  • 7
  • 28
  • 6
    Use vectors and let them go out of scope, much like you already do with the `left`, `right` and `amplitude` vectors. – Some programmer dude Jun 27 '16 at 11:55
  • You do realize that `Function_that_fill_arrays_PUSHBACK` works on vectors which delete the contents automatically, right? – Bartek Banachewicz Jun 27 '16 at 11:56
  • @JoachimPileborg What do you mean? – Thanos Jun 27 '16 at 11:58
  • As far as I know, in C++, the only ways to create an array are either by knowing the size a priori, or by using dynamic allocation. So I would advise to do what Joachim said, or to use the C++ classes. – DrDonut Jun 27 '16 at 11:58
  • 1
    there is a `.clear()` method for vectors if you really want it. – Revolver_Ocelot Jun 27 '16 at 11:59
  • @BartekBanachewicz Actually the function is a `Function_that_fill_arrays_PUSHBACK(vector&)` function – Thanos Jun 27 '16 at 11:59
  • 3
    @Thanos The way code is written your vectors are already destroyed at the end of each pass of `for` loop and re-created at the beginning of next pass. – mvidelgauz Jun 27 '16 at 12:00
  • @mvidelgauz : So when recreated, any previous information is lost? – Thanos Jun 27 '16 at 12:01
  • 1
    `std::vector` *is* a dynamic array, it can change size when needed, you can add more elements to it, you can remove elements, and after removing elements you can even "compact" it be asking it to do a rellocation to free the unused memory. This is all done by dynamically allocating and freeing memory, and when a a vector object goes out of scope its destructor will make sure that the memory is released, it "deletes" the "array". – Some programmer dude Jun 27 '16 at 12:01
  • @JoachimPileborg But in my code, I don't call any `delete` – Thanos Jun 27 '16 at 12:02
  • @Thanos Yes, those arrays are empty every time before `Function_that_fill_arrays_PUSHBACK` is called – mvidelgauz Jun 27 '16 at 12:02
  • 1
    Also, since you declare these object inside the loop, each time the loop iterates the objects goes out of scope, and the vectors are constructed again anew. – Some programmer dude Jun 27 '16 at 12:02
  • 1
    I think you better [pick up a beginners book on C++](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and learn about constructors and destructors, and what they are for, as well as read more about scoping and what happens when variables goes out of scope. – Some programmer dude Jun 27 '16 at 12:03
  • 2
    Unrelated: it seems to be more readable, intention-revealing, efficient and less error prone, to create a `struct Data { double left, right; int amplitude; }` and use a single `std::vector`. It also allows you to use a simple foreach loop. – Elazar Jun 27 '16 at 12:04
  • You may also want in this case to make the function *return* that `vector` instead of accepting a reference to it. The first `new Tfile` which seems to be unnecessary. – Elazar Jun 27 '16 at 14:29

1 Answers1

5

You don't need to do anything at all.

The vectors left, right, and amplitude are all created locally within the body of the loop. So each one is created anew (with zero elements, since that is how they are initialised) at the start of the loop body, and destroyed at the end - which releases the memory they use ..... on EVERY iteration of the loop.

You can check that by printing the sizes of the vectors immediately after they are declared (i.e. before the call of Function_that_fill_arrays_PUSHBACK()). You will find the size is zero, every time.

Peter
  • 35,646
  • 4
  • 32
  • 74