Should I malloc on Arduino?

I’ve been defining an Event class for Arduino processing so I don’t have to think quite so hard about timing loops myself. In the process of doing this, I got the following error:

undefined reference to `operator delete(void*)'

I discovered that the Arduino C++ doesn’t include new() or delete(). (Ok, I guess I shouldn’t be calling it C++….)

I did a quick web search and discovered replacement functions… but then I started thinking.

Why did these get removed from the Arduino C++ variant? On a device as limited as the ATmega328, it makes no sense to malloc() from a pool. Might as well just allocate the variables you want. (Or in my case, allocate an array of variables up front). That way, you know you don’t have a pointer leak that will hose you as you run.

So… I think the answer is no, I shouldn’t malloc on Arduino.

(Edit) Unless, of course, I’m writing a library.

One Response to Should I malloc on Arduino?

  1. Sev says:

    Now you should try again because Arduino IDE V1.0.4 is released with this core evolution :
    * Fixed malloc bug (Paul Stoffregen)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: