0

How do I delete this properly?

float** binsRowPtrs = new float *[_nbins];

The items are not dynamic created with new.

float** binsRowPtrs = new float *[_nbins];
for (int i = 0; i < _nbins ;i++)
{
    binsRowPtrs[i] = (float*) (bins[i].row(y).data);
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Poul K. Sørensen
  • 16,950
  • 21
  • 126
  • 283
  • 1
    `just delete binRowPtrs ?` Yeah, pretty much. If any of the pointers were also dynamically allocated, you'll need to free those as well if they are done with. – Mysticial Mar 02 '12 at 03:55

3 Answers3

2

How do i delete this properly[?]

Properly? You use RAII and let something else handle the deletion for you. Depending on what you're trying to do, you may want to use:

  • std::vector<std::vector<float>>: an array of arrays of floats;
  • std::vector<float*>: an array of non-owning pointers to floats (i.e. something else handles correct allocation and deallocation);
  • or maybe just std::vector<float>: an array of floats.
R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
1

The rule is:

Call as many delete or delete[] respectively as new or new[] you used and on the exact same addresses.

So if you just called new on binRowPtrs then you just need to call delete binRowPtrs.

However, in the part of your code which you didn't show us, If you used dynamic allocation through new for each of the array elements then you need to loop through the array and call delete on each of the element as well.

Note that ideally,
In C++ You should use dynamic allocations only when you cannot avoid them &
If et all you must, never use raw pointers, always use RAII through smart pointers to avoid the explicit memory management(you already noticed the perils of doing so in your case).

Community
  • 1
  • 1
Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • It was some sample code i found online and after 3 hours of debugging i finally found the new and no delete. Was just not sure about what float *[] meant. Anyhow, i think its fine to use new float[] for small arrays for temp values. I find it easyer to type then using a std::vector – Poul K. Sørensen Mar 02 '12 at 04:10
  • @s093294: With your Q edit, Yes just `delete binRowPtrs;` should be fine. As for ease of use, think about the problems that you are facing now, all these won't exist if you used smart pointers.You should choose to use RAII because it is difficult to go wrong while using it rather than manual memory management which might be very error prone and confusing at times. – Alok Save Mar 02 '12 at 04:12
  • @s093294 Yes, `new float[]` is fine if you prefer ease of typing to correctness. But if you value correctness, it isn't fine. – R. Martinho Fernandes Mar 02 '12 at 04:22
  • Thanks. I will look into the links you gave and find another solution. – Poul K. Sørensen Mar 02 '12 at 22:55
0

This will clear those pointers:

delete [] binsRowPtrs;

But the actual data you are storing with bins won't be touched.

yu quan
  • 161
  • 1
  • 1
  • 14