It mainly depends on your compiler and the amount of memory. If you have more than a few KB of memory, dynamic memory allocation helps. If the malloc implementation from the standard library that you have is not configured for the size of your memory, you can write your own or there are good examples, for example mm_malloc from Ralph Hempel , which you can use to write new ones and delete statements from above.
I disagree with repeating mem that exceptions and stl containers are too slow or too bloated, etc. Of course, he adds a little more code than a simple C malloc, but judicious use of exceptions can make the code much clearer and avoid too many errors related to an error in C.
It should be borne in mind that STL distributors increase their power distributions by two, which means that sometimes it will perform some redistributions until it reaches the desired size, which you can prevent by using a backup , so it becomes just as cheap as one malloc of the desired size if you know the size to be allocated anyway.
If you have a large buffer in a vector, for example, at some point it can redistribute and ends up using a 1.5-fold memory size, which you intend to use at some point when redistributing and moving data. (For example, at some point it allocates N bytes, you add data through an append or insert iterator, and it allocates 2N bytes, copies the first N and frees N. At some point you allocate 3N bytes).
So, in the end, he has many advantages, and pays if you know what you are doing. You need to know a little about how C ++ works in order to use it in embedded projects without surprises.
And for a guy with fixed buffers and reset, you can always reset inside a new statement or something else if you have a shortage of memory, but that would mean that you made a bad design that could run out of memory.
The exception is ARM realview 3.1:
--- OSD\#1504 throw fapi_error("OSDHANDLER_BitBlitFill",res); S:218E72F0 E1A00000 MOV r0,r0 S:218E72F4 E58D0004 STR r0,[sp,#4] S:218E72F8 E1A02000 MOV r2,r0 S:218E72FC E24F109C ADR r1,{pc}-0x94 ; 0x218e7268 S:218E7300 E28D0010 ADD r0,sp,#0x10 S:218E7304 FA0621E3 BLX _ZNSsC1EPKcRKSaIcE <0x21a6fa98> S:218E7308 E1A0B000 MOV r11,r0 S:218E730C E1A0200A MOV r2,r10 S:218E7310 E1A01000 MOV r1,r0 S:218E7314 E28D0014 ADD r0,sp,#0x14 S:218E7318 EB05C35F BL fapi_error::fapi_error <0x21a5809c> S:218E731C E3A00008 MOV r0,#8 S:218E7320 FA056C58 BLX __cxa_allocate_exception <0x21a42488> S:218E7324 E58D0008 STR r0,[sp,#8] S:218E7328 E28D1014 ADD r1,sp,#0x14 S:218E732C EB05C340 BL _ZN10fapi_errorC1ERKS_ <0x21a58034> S:218E7330 E58D0008 STR r0,[sp,#8] S:218E7334 E28D0014 ADD r0,sp,#0x14 S:218E7338 EB05C36E BL _ZN10fapi_errorD1Ev <0x21a580f8> S:218E733C E51F2F98 LDR r2,0x218e63ac <OSD\#1126> S:218E7340 E51F1F98 LDR r1,0x218e63b0 <OSD\#1126> S:218E7344 E59D0008 LDR r0,[sp,#8] S:218E7348 FB056D05 BLX __cxa_throw <0x21a42766>
Doesn’t seem so scary, and no overhead is added inside the {} blocks or functions unless an exception is thrown.