Question

Pseudo code in C++

char* data = new char[determine_size()];
// ... do some stuff with data
delete[] data;
// ... repeat process

So basically, data gets repeatedly allocated to a new array with a different size each time. Though every time the previous allocation would always be de-allocated first.

Would this lead to memory fragmentation?

I'm not very familiar with memory managing in C++; my gut feeling tells me this is not a good idea.

Was it helpful?

Solution

You ask,

“Would this lead to memory fragmentation?”

Well memory allocation is necessary for fragmentation to occur, but it is not sufficient.

I.e. it depends, on a lot of things, including the pattern of allocations, and the memory manager.

So as stated the question is really unanswerable.

But some good advice can still be given.

Do not use raw arrays and new, use instead e.g. std::vector and std::string.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top