For example, if deallocations of dynamic memory are always done in opposite direction to allocations. In that case, is it guaranteed that heap will not become fragmented?
And from theoretical point of view: Does there exist some realistic way for a nontrivial application to manage memory to completely avoid heap fragmentation? (After each atomic change in heap, is heap still unfragmented?)
Does there exist some realistic way for a nontrivial application to manage memory to completely avoid heap fragmentation?
Yes. Allocate everything on the stack. This may seem like a counsel of perfection, but I've done it in non-trivial programs.
For absolute clarity and avoidance of doubt, I said 'the stack'. By that I clearly mean automatic storage, i.e. the local variable stack, not any stack class. Some of the comments about this are basically obtuse.