Re:Defensive Programming
by dragontamer on Wednesday December 23, 2020 @04:39PM
(#25522535)
> I'm actually really curious what you all do in C/C++ to prevent bad operations from ever being performed in the first place.
1. Go as far as you can with static memory. Don't call the allocator: its slow, sometimes requires inter-thread communications.
2. Dynamic uses often can use the stack: almost always in L1 of your local core. Stack allocations are self-cleaning, just don't pass those pointers to anyone else.
3. unique_ptr<blah> covers most of the true dynamic issues.
4. shared_ptr<blah> covers the rest of them.
---------
The above advice is just general-purpose / low-performance code.
If you're entering high performance coding (aka: you're actually worried about fragmentation, memory sizes, and such), then things get more complicated. Embedded is often RAM-restricted, so you end up in this complicated place where you need to think of highly-efficient techniques.
That's almost always a complexity / resource tradeoff. Efficient techniques are just innately more complex than general purpose techniques. Try not to reach for efficient coding unless you know its something you need to do
1. Go as far as you can with static memory. Don't call the allocator: its slow, sometimes requires inter-thread communications.
2. Dynamic uses often can use the stack: almost always in L1 of your local core. Stack allocations are self-cleaning, just don't pass those pointers to anyone else.
3. unique_ptr<blah> covers most of the true dynamic issues.
4. shared_ptr<blah> covers the rest of them.
---------
The above advice is just general-purpose / low-performance code.
If you're entering high performance coding (aka: you're actually worried about fragmentation, memory sizes, and such), then things get more complicated. Embedded is often RAM-restricted, so you end up in this complicated place where you need to think of highly-efficient techniques.
That's almost always a complexity / resource tradeoff. Efficient techniques are just innately more complex than general purpose techniques. Try not to reach for efficient coding unless you know its something you need to do