Global Variables changed in local context

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

Global Variables changed in local context

跳至解决方案
2,036 次查看
rdazcal
Contributor III

Greetings,

 

If this question doesn't belong in here, please let me know where I should have posted it, instead.

 

I was debugging a program I've made for the MC9S08LC60, and I noticed something that was very strange to me.

 

I noticed that when the program enters some functions, it saves the local variables in the same physical address where some global variables reside, thus changing them!

 

Does that make any sense to any of you?

 

is it possible the compiler knows (or thinks it know) in advance which global variables it may change without affecting the program's logic?

 

I thank you in advance.

标签 (1)
标记 (1)
0 项奖励
回复
1 解答
758 次查看
bigmac
Specialist III

Hello,

 

The PRM file for the project determines how the linker allocates memory.  The default PRM file contains a STACKSIZE entry, typically 0x50 (80 decimal) bytes.  This occupies the RAM space immediately above the global variables.  This parameter should be increased to a suitable value.  Are you using floating point operations within your code?  This will massively increase the stack size requirement.

 

My personal preference for limited RAM devices, is to use an alternate STACKTOP entry so that the start of the stack can be placed at the maximum RAM address, so as to maximize the distance between the stack and the global variables.

 

Regards,

Mac

 

在原帖中查看解决方案

0 项奖励
回复
6 回复数
758 次查看
Lundin
Senior Contributor IV

The 3 most common causes for such bugs are: stack overflow, stack overflow and stack overflow.

 

:smileyhappy:

0 项奖励
回复
758 次查看
rdazcal
Contributor III

Lundin,

 

You were right on spot!

 

The SP is pointing to the same addresses as the Global variables, which means it overflowed...

 

is there a way in C to reserve more space for the stack, or I have to change "manually" the SP at the program initialization?

0 项奖励
回复
759 次查看
bigmac
Specialist III

Hello,

 

The PRM file for the project determines how the linker allocates memory.  The default PRM file contains a STACKSIZE entry, typically 0x50 (80 decimal) bytes.  This occupies the RAM space immediately above the global variables.  This parameter should be increased to a suitable value.  Are you using floating point operations within your code?  This will massively increase the stack size requirement.

 

My personal preference for limited RAM devices, is to use an alternate STACKTOP entry so that the start of the stack can be placed at the maximum RAM address, so as to maximize the distance between the stack and the global variables.

 

Regards,

Mac

 

0 项奖励
回复
758 次查看
rdazcal
Contributor III

Thank you bigmac, it worked beautifully.

 

Thank you all for the help!

0 项奖励
回复
758 次查看
rdazcal
Contributor III

Thank you J2ME and Lundin!

 

I am analizing the code to see if the problems I'm having are caused by Stack Overflow or code optimization.

 

I'll post soon what I discover

0 项奖励
回复
758 次查看
J2MEJediMaster
Specialist I

Usually the optimizer can track whether varaibles change or are accessed, and it uses this information to remove redundant operations. For example, if a certain variable in a function does not change, it can be eliminated from the code for the lifetime of the function, or treated as a constant which can affect the instructions that generate accesses to it. This is a great oversimplification of what goes on, of course. It can be stated concisely that when the optimizations are on, the generated assembly code may not match the intent or locations of the source code. You will have to look carefully at an assembly listing to see if this is case.

 

---Tom

0 项奖励
回复