← Back to context

Comment by alextingle

3 months ago

Two reasons.

1. Dynamic memory allocation is error prone. Mixing it in with your control flow makes that hard to manage.

Many of the strategies for keeping track of dynamic memory add significantly to the complexity of the program... You need to consider object ownership, maybe introduce reference counting, even garbage collection, or "borrow checkers".

If you can avoid all of that by making good architectural choices up front then your code will be much simpler and robust.

2. Dynamic allocation is slow, especially in a multithreaded environment. It's doubly slow if you layer garbage checking or whatever on top of it.

Prefer fewer trips to the allocator, preferably none in your code's hot path.