This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

[51] simulation in KeilC question

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.

Parents
  • 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

Reply
  • 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

Children