Go Down

Topic: Solved: Why does it have to be static? (Read 804 times) previous topic - next topic


Sep 16, 2012, 09:05 pm Last Edit: Sep 17, 2012, 10:41 am by Msquare Reason: 1
My main routine
Code: [Select]
static int P ;
function F ( &P ) ;

The function
Code: [Select]
void function F ( int *P) { P = 42 ; }
It fails mysteriously if the P is NOT declared static. It works for char data type, but not int or float.

(I was just browsing the GRBL software package where they have the same construction, and it works, presumably. Some time ago I did above and it failed, as I describe. A weak theory is that GRBL presumes a different toolchain to compile... but it still is the same compiler, right?)


F is a macro used to keep string constants from taking up SRAM. Try a different name.


I just wrote this in hand to illustrae the essence. The actual code does use a different name. The actual code is http://arduino.cc/forum/index.php/topic,115174.0.html


Code: [Select]

void function F ( int *P) { P = 42 ; }

That will take the integer pointer "P" and set it to be at address 42.  Is that what you really want?

Surely you mean:

Code: [Select]

void function F ( int *P) { *P = 42 ; }

which will assign the value 42 to the variable pointed to by "P".


Damn. It just goes to show, when writing new and shorter code in the post, instead of doing a cut-n-paste from the real code, one introduces new errors. And there is one more error <sigh-  :smiley-roll-blue: > in my initial post. I was just gettng very very frustrated. Sorry. If you look at the real code (link in above post), you'll see that I do know the difference.

Still the problem is there.

When I write
Code: [Select]
static int P ;
  F ( &P ) ;
void function F ( int *P) { *P = 42 ; }

it works fine

BUT when I use
Code: [Select]
/*dynamic*/ int P ;
F ( &P ) ;
void function F ( int *P) { *P = 42 ; }

I get "random" results


Can you create a working example that demonstrates the problem? A piece of code with a setup() and a loop() that compiles and prints out the incorrect value? It's unclear which part of the thread you linked to has the problem, and there's no way the snippets you posted here are correct with "void function F" and the random colon.

Nick Gammon

Yep, more details needed. Code that compiles. What happens. What you expect to happen.
Please post technical questions on the forum, not by personal message. Thanks!

More info:


I really love the "Send us the code, before and after" "explain what you expect". 

I do the same. Hey, I even demand the same. (Years ago I was head of  support/QA in a company, the official bug report forms I designed had fields for this, with great success) I thought I was above such details this time. <beep> Wrong!

There is this great saying, that I use with friends/colleagues, when we hit an "impossible" problem/bug/behaviour in the systems we work with:

"Explain it to the teddy bear"

Usually, when explaining a problem in excruiating detail to the teddy bear (or a colleague  ;) ) you suddenly see the problem from a new angle or discover a presumption you made that was never made explicitly clear.

And thus the error is discovereed, the problem solved. Humbly I realized my hidden assumption in this case, too.

So - the example in this thread will work.

My code (see the link) left the calling function (loop) and in again and the called function assumed(!) that the parameter it had gotten was unchanged by the caller. Which it wasn't as it is un-static.

I conclude it is very bad coding style for a function to assume that it can use the by reference parameters as (static) storage, between invokations of being called. Too aggressive in saving precious memory bytes. My excuse is that the function was always inline with global variable. As the linked thread says - I converted it to library, and this is where all probelms began. Before the variables were global but you cant do that with a library. I'll fix the code, and "waste" 3 bytes.

Thank you for reading. Now move along, there is nothing more to see here. :)

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131