Allocating the CDC
object on the heap to save stack memory - this is ridiculous! (forgive me for the harsh language).
It's a small object, just 16 byes (on Win32 architecture). It's just 4 "integer" variables.
Actually one could work with raw HDC
(which is only one "integer"), however MFC's wrapper for some reason has another 3 variables defined.
Allocating memory on the heap, apart from unneeded code, leads to a serious performance degradation. Also it has significant memory overhead, plus you need another variable (pointer to the allocated memory). So that the overall memory wasted is much more. In addition when working with a pointer to a dynamically allocated data you actually have an extra-indirection.
One has to worry about consuming too much stack memory if the allocated objects are of size comparable to the total accessible stack size. Let's say, on Win32 with default thread stack size of 1MB, I'd worry about allocating objects starting from tens of KBs. Or, alternatively, you should worry if you use deep recursion extensively (Then all your allocated local variables are multiplied by the recursion depth).
But allocating 16 bytes "to save stack memory" - this sounds insane, compared to the awful performance degradation, code complexity, memory fragmentation and etc.