4 ms·
It's more that certain features can be way more expensive than they look. e.g. say you're working on an embedded system, and you want a string, so you do:
by david-given 10y ago
It's more that certain features can be way more expensive than they look.
e.g. say you're working on an embedded system, and you want a string, so you do:
std::string s = "fnord";
At first it seems to work fine --- but you firmware image's RAM requirements have just gone up by 32kB, and a week later there's a crisis when adding another feature causes the system to stop linking because the RAM address space is full.
What happened is that std::string uses the heap to store the string data, so adding the line above caused the linker to pull in all the heap code and allocate a 32kB block of RAM to put the heap in. Because previously, the product wasn't using a heap: it was using static memory allocation throughout.
That example's contrived, but only a little. I've done the must-avoid-dynamic-memory-allocation dance many times in real life. (I've also discovered that printf() requires a raise() implementation on some platforms.)
A more realistic one is that embedded platforms typically have RTTI turned off, which means no exceptions, which means no throwing exceptions from constructors, which means two-phase construction throughout your program and you have to be really careful about which bits of the STL you use...
- minipci1321 10y agoI do of course understand what you mean. > At first it seems to work fine It so happens that I have been working in OS- and low level areas for many years, and independently am also a pretty early adopter of C++. I have a reflex of routinely giving a quick glance to the link map, and am frequently dumping assembly for areas of code I have doubts in. All this to say that reading 'it seems to work fine' in the context of OS development kind of provokes a skin reaction in me.
- deleted 10y ago[deleted]
- digler999 10y agoIn your string example (and in RTOS/C++ programming in general) couldn't you just change the default allocator to not use heap memory ? Then continue using std::whatever ? of course you'd have to keep a very close eye on your memory pool, but wouldn't this be one way to solve the problem ?
- pjc50 10y agoQuite a lot of embedded architects would say that allocation should never happen at all. That rules out the STL and much modern C++ style.
- mlvljr 10y agoHow would you throw from a constructor even in the presence of exceptions, anyway?