Local variable overlaps part of global variable space

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Local variable overlaps part of global variable space

Jump to solution
791 Views
louislee
Contributor I

Hi,

 

It's first time I contact S08AW32 w/ codewarrior 10.6. The development tool is Multilink Universal. I create several files and many function calls.

 

In file Main.c:

void main(void)

{

     Record_AddUser();

}

 

In file Record.c:

typedef struct {

     byte Template[498];

     // other members

} tUserData;

 

void Record_AddUser(void)

{

     tUserData sUserData;

 

     // do something...

    GetTemplate(gbyTemplate);

     memcpy(sUserData.Template, gbyTemplate, 498);     // <------ overlapping !!!!

}

 

In file FP.c: (provided by h/w module vendor)

byte gbyTemplate[498];     // global variable

 

void GetTemplate(byte* pBuf)

{

     byte Buf[498];

 

     // do somethins to get Template, and put it into Buf.

     memcpy(pBuf, Buf, 498);

}

 

 

After calling function GetTemplate(gbyTemplate), value of many other variables are changed. And then, I find something wrong. The structure sUserData is allocated at 0x0133, and gbyTemplate is allocated at 0x00A5. Can anyone help? Thank you!!

Labels (1)
0 Kudos
Reply
1 Solution
568 Views
kef2
Senior Contributor V

Hi,

function scope non-static variables are allocated on stack. Default stack size in CW generated S08 projects is about 0x80 bytes. This is much less than your Buf size plus sUserData size. Stack size is specified in linker *.prm file. Don't forget though that you have only 2k of RAM for all purposes.

Edward

View solution in original post

0 Kudos
Reply
2 Replies
569 Views
kef2
Senior Contributor V

Hi,

function scope non-static variables are allocated on stack. Default stack size in CW generated S08 projects is about 0x80 bytes. This is much less than your Buf size plus sUserData size. Stack size is specified in linker *.prm file. Don't forget though that you have only 2k of RAM for all purposes.

Edward

0 Kudos
Reply
568 Views
louislee
Contributor I

Hi Edward,

Thank you for reply soon.

Yesterday I had reallocated the stack size several times, but unfortunatelly , nothing changes. The library from module vendor is dedicated for Kinetis. Maybe I'll look for another mcu since I shouldn't modify the library for some reason. In fact, I have these code work fine in Kinetis M0+ and M4 w/ "large" RAM size.

Anyway, you are right. Thx angin!!

0 Kudos
Reply