Nothrow, malloc & new

Hi,
I’ve gotten around and made solid firmware by making sure I only use Preallocated memory. Now I’m in need of using Dynamic memory allocation and I’m a little confused. According to http://www.cplusplus.com/doc/tutorial/dynamic/ one can just add (nothrow) and you’ll not get an error thrown if memory allocation fails. This fails to compile with the error:

'nothrow' was not declared in this scope

I poked around in the Particle firmware and found that nothrow exists, so it’s likely supported. By adding the following to the top of my class, the file compiles as it should:

using std::nothrow;

This looks to be working well, but it would be great if we could add a bit more information to the rather thin portion of the docs that mention dynamic memory allocation. https://docs.particle.io/faq/particle-devices/code-size-tips/photon/#memory-allocated-with-malloc-new-etc-

Anyone got ideas for what I should add apart from the above?

Particle system and user firmware is built with -fno-exceptions so nothrow is the default and C++ memory allocation never throws an exception. Well, nothing can throw an exception because they’re disabled in the compiler.

You just need to check for a non-null response from new to to make sure the allocation succeeded. There is no need to add nothrow.

That’s good to know! Mind if I submit that to the Docs @rickkas7? :wink:

I’ve already added a task to document exception behavior and new.

2 Likes

Thanks! :smiley: