+ 1
How to avoid global variables
Refer code below: How to complete this code without changing function signature and not using global variables? https://sololearn.com/compiler-playground/cMmIifMYJoFg/?ref=app P.s. I can use static in function and complete the code. Just checking for other alternative as static solution was ok but more option was asked
4 Antworten
+ 1
Besides global or static you could write it to some permanent storage such as a disk file or Windows registry.
And then there is my other trick of writing to other stack frames 😋
0
Thanks Brian
0
Ketan Lalcheta I hoped to see more responses with ideas for you. Here are a couple more that I thought of:
- Store the counter in a temp table of an in-memory db using SQLite.
- Store the counter in an environment variable. Here is an implementation:
https://sololearn.com/compiler-playground/c9qqY0pb5DX1/?ref=app
I even made sure the code would be portable.
0
Ketan Lalcheta you could declare a static local variable within myCycle().
That would preserve the integer value between successive calls, but is not a global variable declared outside the function.