+ 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

13th Feb 2025, 5:43 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
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 😋
13th Feb 2025, 8:59 AM
Brian
Brian - avatar
0
Thanks Brian
13th Feb 2025, 11:08 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
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.
13th Feb 2025, 8:42 PM
Brian
Brian - avatar
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.
13th Feb 2025, 10:59 PM
Shardis Wolfe