Jezze wrote:
I don't want to hijack this thread but since you asked I'll reply. The reasoning is I think keeping track of memory is something that in large complex systems is an exceptionally complex task. Anyone who thinks they can grasp it is probably delusional.
I think you're the delusional one... It's a fairly straightforward process, you're just failing to understand it.
Jezze wrote:
If I use dynamic memory allocation I can not trust myself that the system will be able to allocate and free memory correctly in all situations.
You never should anyway because you don't have an infinite amount of memory. That's what error checking is for.
Jezze wrote:
If I don't use it, a compiler will to a greater extent find most of the problems in my code through static analyzis, something that is near impossible in a system with dynamic memory allocation.
Nope. It's perfectly possible, you just don't know how to do it.
Jezze wrote:
Also there are other side effects to consider, you have memory fragmentation and the added time to allocate and deallocate memory which depending on the memory allocator and how the system is used can vary greatly.
That's why there are different memory managers. The operating system doesn't handle it. It just provides sbrk and brk functions that either increase or decrease the number of memory pages used by a particular program. These pages are virtual, translated into physical addresses by the MMU; so there's no need to worry about continuity. Libraries like malloc and talloc, which are part of the C runtime that the program is linked against, are responsible for micro-managing this memory given to them by sbrk/brk.
Jezze wrote:
I also think, and this is questionable i know, is that in many ways dynamic memory is a last resort many has to take because there is no other option available when the system does not allow or provide you with proper interfaces due to poor design.
It's not a last resort, it's an only resort. If the machine doesn't have an MMU to translate virtual addresses into physical ones, you have make use of either position independent code and/or overlays. An art of programming that has pretty much died since the introduction of MMU's.
EDIT:
Funny story, I once asked a question on stack overflow about the use of overlays, and people were completely oblivious as to what I was talking about. I had to explain to them what an overlay was, and my question went both unanswered and thumb-down'd.