for( int i = 0; i < lines; i++ ) {
std::unique_ptr<BYTE[]> pLine( new BYTE[lineSize] );
//Do stuff
}
Now, pLine is declared inside the loop because it's only used in the loop body. However, wouldn't allocating only once outside the loop reduce the amount of allocations performed (avoiding memory fragmentation)?
std::unique_ptr<BYTE[]> pLine( new BYTE[lineSize] );
for( int i = 0; i < lines; i++ ) {
//Do stuff
}
I could believe the compiler would be able to optimize the first version easily if it knew lineSize stays the same throughout iterations; but it does change throughout function calls, so I can't make it a constant.
I also think micro optimizations like this should be avoided until a performance problem is detected, so I guess I would stick with the first version. What do you guys think?