I work with a code base that is poorly written and has a lot of memory leaks.
It uses many structures containing source pointers, which are mainly used as dynamic arrays.
Although structures are often passed between functions, the distribution and release of these pointers are placed in random places and cannot be easily tracked / justified / understood.
I changed some of them to classes, and those pointers that should be RAII are the classes themselves. They work well and do not look very ugly, except that I forbade copying and copying these classes simply because I do not want to waste time implementing them.
Now I think I'm reinventing the wheel again? Why can't I replace a C-style array with std: array or std :: valarray?
I would prefer std :: valarray because it uses heap and RAIIed memory. And std :: array is not yet available in my development environment.
Edit1 . Another plus of std :: array is that most of these dynamic arrays are POD arrays (mostly int16_t, int32_t and float), and the numerical API can make life easier.
Is there anything I should know about before I get started?
I might think that there might be a hard way to convert std :: valarray or std :: array into C-style arrays, and part of our code uses pointer arithmetic and needs to be presented as simple C-style arrays.
Anything else?
EDIT 2
I recently met this question . The VERY BAD thing about std::valarray is that it is not reliably assigned to copy before C ++ 11.
As indicated in this answer, in C ++ 03 and earlier, UB if the source and destination are of different sizes.
source share