Content originally posted in LPCWare by PhilYoung on Wed Jun 20 12:59:16 MST 2012
The default keil setup code needs to be configured to allocate sufficient stack and heap space, this can be done with the config wizard.
There are a number of things to consider when choosing the heap size, for example if you are writing C++ and instantiate static objects where a destructor is declared then the startup code will allocate memory to build a table for callng the destructors on exit, which uses malloc and therefore needs a heap defining.
Similarly if you use semi-hosting then it also requires heap space, if you are using neither of these and don't explicitly call malloc then you probably don't need a heap ( unless you handle exceptions ).
Otherwise, for all statically allocated objects the linker will allocate space for them in data, and all dynamic objects ( variables in functions etc ) they will be on the stack.
initialized variables are set up by the startup code and placed in RW Data, zero'd variables are placed in ZI data, constants are placed in RO Data, code is placed in code.
initialized data is set up by the startup code, there is not a literal copy of it in the code since the Keil tools use compression, so the startup code decompresses this before calling main.
you can of course define whatever sections you want and place anything in them using a custom linker script, but by default Keil will handle everything for you, just ensure that the stack and heap are large enough, do this by opening the startup.s file in the project and switching to the config screen where you can use a menu to change the sizes.
to find out how much stack you need, you can compile with all options selected on the linker listing section in the listing tab, then it produces an HTML file listing the functions and there callers / callees and required stack size ( static call graph ), this lists the maximum stack size that it can determine.
regards
Phil.