cancel
Showing results for 
Search instead for 
Did you mean: 

NEW Operator under µVision 4

j-fischer-gundlach
Associate II
Posted on April 15, 2013 at 19:55

Hello Community,

i´m having a stm32f3Discovery under the Keil´s µVision4 Compiler programming in c++.

I got to the point, where i could use the ''NEW''  operator.

To be honest, i have no idea, what is really going on. I know in principle this operator

allocates space for the given type and returns a pointer in case of success.

As long i used this on a desktop PC i didn´t care, since there ia an OS under my feet.

I mean, if i allocate space i guess this trigger the OS to ''talk'' to some kind of

memory controller if there is enough space and where it is, and also handles

the cirumstances if the memory becomes fragmented. (?)

And this is where my understanding ends when it comes to bare µC´s. Do i really have to

import a complete OS just for getting new/delete oportunities?

And what is administrating the RAM in my STM32F3 i could ''talk'' to? Would i have do

make this RAM controller by my own?

For summing up the whole scenery in one question:

What is going on in my stm32F3 if the compiler finds a ''NEW''?

yours sincerly,

Justus
11 REPLIES 11
Posted on April 15, 2013 at 20:32

I'm not a C++ guy, but as I recall it's going to use the regular memory allocator, and presumably malloc/free, perhaps internal versions, or more effective ones you supply.

The available heap is going to be defined in the startup_stm32f30x.s file, as ''Heap_Size'', you'll definitely want this to be large enough for your task, as the stack is parked immediately above it.

http://www.keil.com/support/man/docs/armlib/armlib_Cihfiabf.htm

You'll want to read the documentation, and digest. It's doesn't take an OS, just a suitable memory allocator.

[DEAD LINK /public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/STM32%20%20Keil%20memory%20management%20in%20C%2B%2B&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B&currentviews=270]https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=%2Fpublic%2FSTe2ecommunities%2Fmcu%2FLists%2Fcortex_mx_stm32%2FSTM32%20%20Keil%20memory%20management%20in%20C%2B%2B&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B¤tviews=270

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
dthedens23
Associate II
Posted on April 16, 2013 at 00:43

Well, it does a little more than allocate the space.  As clive1 suggests, the space is allocated with a primitive and it is, most likely, malloc().  So make sure the heap is set up with appropriate space.

Secondly, the operator calls a class constructor.  Not the normal constructor, but the assignment constructor because of the ''=''.  If you did not provide one, then it will use the default. Maybe, or maybe not what you want.  So if there are no exceptions anywhere, then you get a pointer to the object.  Then use it like any pointer to an object, such as, p->mVar=1;   or p->Function();

You can create global objects, but beware when objects rely on other objects.  there is no defined order in which global object's constructors are called.

the object state (member variables) is in RAM, and the member functions are in program flash.

Now you can get tricky and use ''placement new'' which will place an object right over some memory mapped space such as a peripheral.  But it circumvents CMSIS and until CMSIS grows up and provides a CMSIS++ then I would not suggest using placement new.

you should really read up on 

constructor

copy constructor

assignment constructor

these are fundamental

and do not fall into the Microsoft hack where every destructor is virtual.  It was a flaw in there compiler and it will bloat you code to use a virtual destructor when the class uses no virtual methods.

I'll get off the soap box now

Posted on April 16, 2013 at 00:52

I think the important thing to do is not bring a PC mentality to embedded, resources are not infinite and speeds are not high. A different set of design choices must be made. I've seen C++ used effectively in embedded, but the systems have had far more memory to play with than the STM32.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
dthedens23
Associate II
Posted on April 16, 2013 at 01:05

BTW, I attended an Embedded System Conference where several classes showed the compiler assembly code generated for C vs C++ code and guess what?  It was the same except when you got to inheritance.  And then, the C++ code could be bigger or smaller depending upon the skill of the developer, just as I have seen good and bad C code, depending upon the developer.

I would say that you really really do not want to be creating and destroying objects at runtime.  The heap is of fixed size, it does become fragmented, it does NOT garbage collect, so it can and it will fail to allocate objects.  General policy is that objects may be created globally (constructor is run before main()) and also created in main() before entering the while(1) loop.

runtime new and delete of objects is dangerous.

Now, if all your objects are exactly the same size in bytes, then it may very well work.  But if they are of mixed sizes, then the fragmentation will eventually fail.

frankmeyer9
Associate II
Posted on April 16, 2013 at 08:40

runtime new and delete of objects is dangerous.

 

Well stated.

Languages like C++ and Java are in fact relying on abundant memory. That is, a MMU and swap support by the OS. They are wonderful tools where this preconditions are fulfilled, but often fail miserably where not. Think of MISRA C, which explicitly requires a static memory layout, or other safety-critical areas.

BTW, there is an ''Embedded C++'' dialect as a subset of C++, that had been developed for this purpose. In the last years it had been supported by IAR and Tasking, but I never used it.

j-fischer-gundlach
Associate II
Posted on April 16, 2013 at 20:46

Hello everyone,

i´m really happy aboud the informations and keywords wich i can feed into google.

I have a little advantage regarding the limited space. The ''project'' i´m working on is

simply a RC transmitter for models. So it´s not a server thing, where uncountable masses of people

might trigger more and more allocation of objects.

I just do the signal genaration and pass it to the

high freuency circuit.

So there are different types of channels (anlog or digital(on-off) on-off-on)) wich can also be mixed wich each other.

And i´m, thinking about to implement disable/enable switches.

The key idea is not to programm the sticks and switches with fix functions, rather than making the whole thing

configurable. It never got to my mind if i buy a special transmitter for planes it have tons of

switches and regulators which are all end up mixed in 4 or 5 channels, while for a multifunction

boat i would have to buy a second transmitter. In the end, a switch does only what the programm

defines it to do.

So my idea is at every startup to delete every object, load a model configuration and allocate

the nesessarry objects.

many thanks,

Justus
j-fischer-gundlach
Associate II
Posted on April 18, 2013 at 10:13

Hello everyone,

i have taken a look into ''Libraries and Floating Point Support Guide'' from KEIL http://www.keil.com/support/man/docs/armlib/armlib_cjaehgjb.htm But as a beginner i´m not able to understand this information. For example the scentence ''To use a heap implementation in an application that does not define

main()

and does not initialize the C library:'' confuses me. What do they mean by ''does not define

main()

and does not initialize the C library:'' ? And unfortunatly there is no example code. greetings
frankmeyer9
Associate II
Posted on April 18, 2013 at 11:31

I wouldn't care too much about that. It might be interesting and worth to keep in mind, but you will very likely have a main function, and the appropriate library init routines.

A bootloader application, for instance, might not use main, and not involve the usual startup glue code, which handles all the necessary things between MCU reset and main entry - the things an OS would do for you otherwise. Calling static constructors and (afterwards) destructors is one of those things.

I did not yet try a C++ project for an ARM Cortex M, and especially not with Keil, but be warned that you might be required to implement parts of the memory management, for instance the _sbrk() function.

I would suggest to try a very small test project in C++, just to check what will be required.

It will also tell you something about the Flash/RAM spent for library usage.

Andrew Neil
Evangelist
Posted on April 18, 2013 at 14:41

Now gone to the Keil forum:

http://www.keil.com/forum/22903/