Why should C++ programmers minimize use of ‘new’?

There are two widely-used memory allocation techniques: automatic allocation and dynamic allocation. Commonly, there is a corresponding region of memory for each: the stack and the heap. Stack The stack always allocates memory in a sequential fashion. It can do so because it requires you to release the memory in the reverse order (First-In, Last-Out: … Read more

How ‘random’ is allocation of memory when I say “new Integer” in Java?

What algorithms are used? Java uses TLAB (Thread Local Allocation Buffers) for “normal” sized objects. This means each thread grab some Eden space and turns this grab of memory into individual objects. Thus small objects are typically sequential in memory for that thread, except if a new chunk of memory needs to be grabbed. Large … Read more

How to generator all combinations (memory efficient algorithm)?

Just create an iterator method using yield return, like so: public class Combinations: IEnumerable<Combination> { public IEnumerator<T> GetEnumerator() { Combination currentCombination = Combination.FirstCombination(); while (currentCombination.IsValid()) { yield return currentCombination; currentCombination = currentCombination.NextCombination(); // return invalid combo if currentCombination is the last possible combination } } } Of course you still have to write the Combination … Read more