Jump to content

Strange Behavior With Inline Functions And Static Variables


Recommended Posts

After spending some time trying to figure out why function-static variables were not being reinitialized on a processor reset, I realized that the function they were in was inline. That may be a bit dubious in nature, but it seems like this should really either be supported correctly (whatever that means) or a warning/error is generated. I don't know what the standard says about this issue. In fact I think only C99 has an inline keyword anyway. Here is sample code of what I'm talking about though:

 

#include <system.h>

//Set clock frequency
#pragma CLOCK_FREQ	4000000

inline void func_a(void)
{
static unsigned char count = 0;

++count;
}

void interrupt( void )
{
}

void main( void )
{
for(;;) {
	func_a();
	asm {
		reset
	}
}
}

 

After the reset, the value of count in func_a would still be 1. Once I made the function a regular, non-inline function, the static variable was initialized correctly on reset.

 

I just tested this with the Boost IDE as well. It appears to work in there. I'm guessing this is because all resets force a memory clear in Boost IDE. As soon as the reset instruction was executed the memory view showed the byte was cleared.

Link to post
Share on other sites
...After the reset, the value of count in func_a would still be 1...

 

Not really. After reset all variables become uninitialised what means that they can be of any values. Than start up code initialises globals and by the time 'main' starts all global variables that are initialised within their declaration are initialised. However because 'count' in your code is local to 'func_a' it won't get initialised till 'func_a' is called.

 

Regards,

Pavel

Link to post
Share on other sites
...After the reset, the value of count in func_a would still be 1...

 

Not really. After reset all variables become uninitialised what means that they can be of any values. Than start up code initialises globals and by the time 'main' starts all global variables that are initialised within their declaration are initialised. However because 'count' in your code is local to 'func_a' it won't get initialised till 'func_a' is called.

 

Regards,

Pavel

 

But that is not happening. When I was checking the value of that variable, it was inside the function. The effect of running that snippet of code I posted is that the variable will immediately increment to the value 2 instead of 1 after RESET, because the value remains at 1 after entering the function. In looking at the disassembly of the inline function, the code to reset the variable is not present there. I did find the code that is supposed to clear it another address outside of the function, and could not locate any call to that address.

 

In the case of the non-inline function, the code to reset the variable is present and works appropriately.

Link to post
Share on other sites
I don't think modifiable static variables are allowed in inline functions (C99).

 

Cheers

 

Reynard

 

I found some document written about GNU gcc that says the same thing about C99. So my preference would really be an error for this, but if it can be made to work in some defined way that is good too. As it stands now, I don't believe the variable is ever getting initialized and was only working for a while because of the implicit memory clear when the simulation starts.

Link to post
Share on other sites

I checked the code under debugger and the 'count' var is not initialised to zero when the function 'func_a' is entered the first time. We'll investigate further and fix this one way or another.

 

Regards,

Pavel

Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...