Question

I am using an ATMega32 uc with Atmel Studio 6. I have some code that contains the new operator. When I try to use it, it says that it is not defined and I don't know why.

I searched for something on Google but I didn't find anything relevant yet. All I could find was pieces of code that defines the new and delete operators but I really don't wanna work this way. Also the nullptr is missing.

Any other solutions?

Était-ce utile?

La solution

It is perfectly possible to use dynamic memory management in embedded situations - you just need to be careful how you do it. In this case, using malloc() and free() is likely easier, though to help with compatibility you may want to define them as new and delete. A good source of information on the topic is over at AVR Freaks.

The reason these operators don't exist is simply that AVR-GCC does not fully support C++, only parts of it. This is partly due to the nature of embedded programming - some of the more advanced C++ features can rapidly chew up flash memory and RAM. The C vs C++ on embedded platforms argument is an old and often heated one, but usually comes down to the situation. Here is another another forum topic on the subject.

Autres conseils

It sounds kinda embedded stuff. It's not a rare case, that you can use only C, not C++.

Anyway, new/delete - and malloc()/free() as well - is not a good idea in embedded world. Your program must work in any circumstances. There is no way to fail. It's just n.a. You have no console nor log file to write a message, or if you have some, no one will check it, no one will handle the error. You can flash a red led indicator, but in most cases, it's not permitted, e.g. pressing brake pedal should operate the brake, not a red led diode.

You should set up fixed size pools instead of dynamically allocate/deallocate items, and you have no option to not to handle any possible input. Your code will be full of "MAX_..." defines.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top