We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Hi, The test code is listed as below,
#pragma OT(0) void main (void) { DATA_MSG *b=NULL; DATA_MSG *a=NULL; b=(DATA_MSG *)malloc(sizeof(DATA_MSG)); b->sMsgHeader.MessageType=E_Battery_Status; b->sMsgHeader.Extended=1; //Plug_Status=0; //Varible_Init(); a=(DATA_MSG *)malloc(sizeof(DATA_MSG)); a->sMsgHeader.MessageType=E_Get_Country_Info; a->sMsgHeader.Extended=0; if(b->sMsgHeader.Extended || a->sMsgHeader.Extended) EX3=1; else EX3=0;
I use the keil-C simulator to debug it. I found the pointer a and b point to the same memory location. It means that the a overwrites b, I don't know why? Anyone knows it? Thanks a lot.
Dynamic memory allocation is generally avoided in small embedded systems - like 8051.
Why do you feel the need for Dynamic memory allocation here?
You do not check that malloc() succeeded before using the pointer - this is a recipe for disaster!
Have you properly configured your Heap ?
BTW
Having selected 'Cx51, 8051, or MCS51' as your Architecture/Product, there's no need to repeat that in your title
the '51 is NOT a PC dynamic memory allocation should be avoided like the plague (MISRA does not allow it)
I only see mallocs etc when the coder is PC trained and believes the '51 is "just another processor"
are you the ghost of Erik Malund ... ?!
Why do you feel the need for Dynamic memory allocation here? Ans: Because the size of data in my code is not a fixed value and I could not know the excat size in advance. Also I only have 1KB external RAM.
You do not check that malloc() succeeded before using the pointer - this is a recipe for disaster! Ans: I understand it. Thanks for your suggestion.
Have you properly configured your Heap ? Ans:Do you mean memory model? Current my memory mode is Large:variables in XDATA ,code Rom : Large 64K program
Then your really, seriously do not want to be using dynamic allocation!!
:0
Using dynamic allocation will not magically create extra memory from nowhere - it all still has to come out of your 1K of RAM.
And using dynamic allocation involves overheads - which are probably quite significant relative to that 1K !!
No, configuring the Heap has nothing to do with your Memory Model.
If you need to ask, then it is almost certainly not correctly configured - so your use of malloc is almost certainly bound to fail.
You can google how Dynamic Allocation with malloc & the Heap work; it is standard 'C' stuff - not specific to Keil.
You have to configure the Heap as part of your project setup - so it really doesn't help you with the problem of knowing your data sizes in advance!
I know the root cause http://www.keil.com/support/man/docs/c51/c51_init_mempool.htm
Thanks.
Yes, that's it.
Now you see that you have to define the size of the memory pool (aka "Heap") in advance.
So it really doesn't help you - does it?
You might just as well use the memory directly - and then you save the overheads!
And you avoid all the other risks associated with Dynamic Allocation!
RE: the '51 is NOT a PC are you the ghost of Erik Malund ... ?!
no, could not be, ghosts do not make sense