Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

LPC21xx Memory Management (stack, heap, dynamic memory allocation etc) using Keil

Status
Not open for further replies.

nigh

Newbie level 6
Newbie level 6
Joined
Aug 24, 2011
Messages
13
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,379
Hi,
I am a beginner level developer and until now I have been working on PIC18 & PIC16 using MikroC compiler. But now I am working on a project with LPC2138, an ARM7 based MCU from NXP, using Keil uVision.
The project includes GSM module interfacing, EEPROM, SD card, different digital (with I2C and SPI interface) and analog sensors etc. This may later include USB and Ethernet interfacing too (for which I intend to shift to LPC2378).
Well ... In case of MikroC, the compiler allocates all required memory ONLY at compile-time (a negative aspect of which is that the functions are non-reentrant) so I never had to worry about the concepts like dynamic memory allocation and heap etc.

Now with Keil, I started developing without giving much thought to memory management. Once I created an array of 4000 elements inside a function (local variable) and after compilation there was no significant change in used RAM space. This made me start wondering about compiler's memory management.
According to my current knowledge about PC high-level programming:
1. local variables: values are stored on stack and references are stored on heap
2. dynamically allocated variables (using malloc etc) are stored on heap
if above two points are correct and these concepts apply to MCU's as well, then my array would be created on stack and if there's not enough stack, there would be stack-overflow. Am I correct?
I used this syntax to create my array:

Code C - [expand]
1
char arr[4000] = "";


my second question is:
Who Pushes and Pops the data to and from stack as there is no OS in my case. Does compiler include some house-keeping code during compilation/linking?

P.S. I would also like to discuss the concepts like function reentrancy and dynamic memory allocation in some detail but first let's clear my concepts about above two questions.
P.P.S. if you think that this question is possible duplicate, please overlook my laziness although I did try (a bit) to find my answers through google. please provide me some pointers where i could have learned these things.

Thanks in advance. I'd really appreciate your response.
 

if above two points are correct and these concepts apply to MCU's as well, then my array would be created on stack and if there's not enough stack, there would be stack-overflow. Am I correct?
Yes you are correct. You will have to take care of stack size yourself. Compiler does not show any error in this case but your program will probably malfunction on run time.
And it's a good practice not to make such large variables inside the function (as local variable).

Who Pushes and Pops the data to and from stack as there is no OS in my case. Does compiler include some house-keeping code during compilation/linking?
You don't need to worry about this, compiler will handle this.

P.S. I would also like to discuss the concepts like function reentrancy and dynamic memory allocation in some detail but first let's clear my concepts about above two questions.
I suggest you should read Keil's Arm compiler reference manual, it has all the details. (just open Keil uVision and goto Help).
 

yes, as salmanliaquat mentioned, you are correct about stack and heap.
There are two more areas in RAM. Data segment and BSS segment called RW-data and ZI-data (zero initialized) respectively in Keil.
Data segment is for initialized Statically allocated (globals & static) variables and BSS segment is for un-initialized Statically allocated variables.

if you understand the above concepts properly, understanding reentrancy should be pretty straight forward.
 

Hi,
salmanliaquat Thank you for your response. I'll keep in mind your advice.

if you understand the above concepts properly, understanding reentrancy should be pretty straight forward.

shihab_leo Can you please explain a little more
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top