GreyGnome:
because if you are using gcc 4.x and newer (I didn't check it with older versions) and you want to use templates, virtual inheritance and so on you must define some additional functions, which is not used now by compiler, but must be present to satisfy linker
Actually that is required for something different, not dynamic memory allocation, as this demonstrates:
class foo
{
public:
foo () { bar = 0; }
int bar;
};
void setup ()
{
static foo x;
}
void loop () {}
Compiling this:
sketch_oct04a.cpp.o: In function `setup':
sketch_oct04a.cpp:13: undefined reference to `__cxa_guard_acquire'
sketch_oct04a.cpp:13: undefined reference to `__cxa_guard_release'
No new or delete there. Just a statically-allocated class member inside a function.
AWOL:
Because in a RAM-constrained embedded environment, you shouldn't be using them.
If you are short of RAM, careful memory management is certainly required. However avoiding dynamic memory allocation is not necessarily that.
Just to give a couple of examples ...
Try this sketch on a Mega2560:
void setup ()
{
Serial.begin (115200);
Serial.print (sizeof (Serial1));
}
void loop () {}
That prints "19". So, 19 bytes used up for Serial1. In fact, without asking if you need them or not, you get Serial, Serial1, Serial2, and Serial3:
#if defined(UBRRH) || defined(UBRR0H)
extern HardwareSerial Serial;
#elif defined(USBCON)
#include "usb_api.h"
#endif
#if defined(UBRR1H)
extern HardwareSerial Serial1;
#endif
#if defined(UBRR2H)
extern HardwareSerial Serial2;
#endif
#if defined(UBRR3H)
extern HardwareSerial Serial3;
#endif
That's 76 bytes gone, when memory is short, and you might only need 19. Perhaps you should dynamically allocate Serial1, Serial2, and Serial3? Then you get them only if you need them.
Now look at the Wire (I2C) library. Internally that has 5 x statically-allocated buffers of 32 bytes each. That's 160 bytes in total. Out of only 2048 available on a Atmega328. Too bad if you need that memory for something else. Too bad if you need 40 byte buffers, you don't get a chance to change that 32-byte limit (short of editing the library code). Too bad if you only need 10 byte buffers.
If you only need 10 byte buffers, 5 of them would only be 50 bytes, compared to 160 bytes. So this static allocation doesn't seem to me, on the face of it, to be saving memory. It forces designers to design for the "common case" which means they allocate too much static memory for some cases, and too little for others. All this could be fixed by using dynamic allocation.
Now of course, dynamic allocation can be abused. Anything can be abused. People make recursive function calls when they shouldn't. They run off the end of buffers. They use variables without initializing them. Getting it right is part of the job. But you don't just remove something because it can be abused.
Probably the worst offender is the String library, not because it is bad per se, but because if you build up strings a byte at a time (eg. concatentating when reading from the serial port) you get major memory fragmentation. So yes, that isn't a good idea.
Early PCs (eg. the original Macintosh) used dynamic memory allocation. Not because they had a lot of memory. Because they were short of memory, and had to use it as cleverly as they could.