GLBasic forum

Main forum => GLBasic - en => Topic started by: erico on 2015-Aug-10

Title: Static
Post by: erico on 2015-Aug-10
We have STATIC command inside GLB, I wonder out of curiosity if it can be used as this nice article states:
http://hackaday.com/2015/08/04/embed-with-elliot-the-static-keyword-you-dont-fully-understand/#more-164040
Title: Re: Static
Post by: MrTAToad on 2015-Aug-10
You mean this example :

Code (glbasic) Select
void loop()
{
    static int count;
    count = count + 1;
    if (count > 10) {
        Serial.println("Hello World");
    }
}
?

This is the GLBasic equivalent :

Code (glbasic) Select
WHILE TRUE
loop()
SHOWSCREEN
WEND


FUNCTION loop%:
STATIC count% = 0

INC count%
IF count%>10
DEBUG "Hello world"
ENDIF
ENDFUNCTION


I think you will find that it works exactly the same :)

As or the later examples, it wont work, most likely due to how C++ works (GLBasic wont allow STATIC variables in the global space).  However, that is where extended TYPES come in - you would have count% and the various functions in there, which would more or less that same way, without not much extra code.
Title: Re: Static
Post by: Hemlos on 2015-Aug-11
Use TYPE, oops did i say that again?!

lol

GLOBALS and STATIC are alike ONLY in the sense that thier values stay until you change them.
They are UNALIKE in the way of SCOPE....static is scoped into the code and limited to where its called.
GLOBALS are not scoped and they can corrupt multiple runs of a program.

Use TYPE....they are more like globals than STATIC is!

TYPE MyGlobals:
MyNameIs$
ENDTYPE

//Header:
LOCAL NAME AS MyGlobals

//Anywhere in your Program:
NAME.MyNameIs$ = "Neil"



Now you can use this anywhere in the program, and you can change it anywhere.



Title: Re: Static
Post by: erico on 2015-Aug-12
From the article and both your examples I get a glimpse on how this would work on GLB.
I understand the logic of a static variable within the global/local scope,
but I fail to actually picture a use of it in a game.

Either way, thanks for the info!
Title: Re: Static
Post by: MrTAToad on 2015-Aug-12
It would all depend on how you view globals really - there isn't really much of the difference.  Static does make things a bit tidier, but at the expense of trying to find variables :)
Title: Re: Static
Post by: Hemlos on 2015-Aug-13
There is a big difference in memory declarations in c++, which is what glbasic is made in righty?

GLOBAL  // can be accessed outside the program GLOBAL to the computer.
GLOBAL // permanent during duration of runtime

STATIC // can not be accessed outside the program
STATIC // permanent during duration of runtime, IN SCOPE ONLY.

LOCAL Permanent$ //this in your header is a global....same as static cant be externaly accessed.
LOCAL Temporary$ // elsewhere in a program, other than header, the memory goes away after the frame render, and is a scope.


G has recommended to me once upon a time, that using LOCAL in your header is the best way to go.
edit: i forgot this note...LOCAL in the header is global....in the main loop only.
eg. it wont scope into a function.
Title: Re: Static
Post by: Hemlos on 2015-Aug-13
Local is tricky..and interestingly enough..
If you declare a LOCAL TYPE in the header...it wont scope into a function either.
Title: Re: Static
Post by: kanonet on 2015-Aug-13
Quote from: HemlosLocal is tricky..and interestingly enough..
No, LOCAL is easy and the way to go, see below.

Quote from: Hemlosheader
Actually I wonder what you are talking about Hemlos, what is a header in GLBasic? GLBasic only has .gbas files, no differentiation between headers and implementation files.

Quote from: HemlosGLOBAL  // can be accessed outside the program GLOBAL to the computer.
This is just simply wrong. A GLOBAL can not be accessed outside of the program, all variables are just for internal use of the program. If you want something to be accessed from outside you could set a environment variable or write to a file or something... Generally this should be avoided of cause.

Ontopic:
You can only create LOCAL variables inside the main area (in the main gbas file, above the 1st FUNCTION/SUB) or inside a function/sub. The scope of a LOCAL variable is always local to that area where it is defined, that means you can only use it from that area. If you call a function that contains a LOCAL variable the variable gets created and when the function is done and returns to its caller, the variable gets destroyed.

STATIC variables get created in a local scope to, just like a LOCAL variable. Only difference to LOCAL is, that a STATIC variable gets not destroyed when its containing function is done and return. So you can save its value between function calls. But you still can not access it from outside of the function. So STATICs are the way to go if you need to save data between function calls, that are only needed inside that function.

GLOBALs are defined globally (yes really!) that means you can read or write to them from everywhere inside your program. You can define a GLOBAl everywhere, even outside of any function or the main scope. You should never have a variable with the same name of a GLOBAl, because this shadowing can really confuse readers of your code and my be a huge source of bugs. So creating a GLOBAL somewhere actively limits the names of variables that you use anywhere in your project, so if you create one, give it a long and meaningful name. Also being able to access and change the value of a GLOBAL everywhere in your program can be a huge source of bugs, if anything changes that value and you can have a really painful time searching that bug. So generally advice is: avoid GLOBAL when ever possible, think twice or even 3-4 times if you can find any way to avoid a GLOBAL, replace it with a static or pack all your data that belongs together in the same type. Globals are evil most times.

-> LOCALs should be the tool that you use 99,99% of your time. Really dont use anything else, unless really needed. Then try to use static, or pack the data in a TYPE and declare this one LOCAL. BTW types... they are just normal variables. Soyoucan declare an instance of a type LOCAL, STATIC or GLOBAL as any otehr variable too. Of cause you should declare it LOCAL when ever possible.

BTW all this was about variable scope in GLBasic. In c++ its a bit different (but the ban on global vars is just as important too) and the keyword static has a totally different meaning! So a C++ article about static would not help you that much in GLBasic.
Title: Re: Static
Post by: MrPlow on 2015-Aug-13
I typically use a naming convention to avoid confusing locals and globals...

Globals are very handy for keeping state of timers and other key values...but are you saying that they should be replaced with Statics?!

Do Statics give a performance benefit? If so is significant?

Also using Type Endtype for all variable declares is a bit too verbose for me essentially 4 lines of code for each global val...No?!

I would think accessing via the type structure is probably slower than accessing via a global - but I dont know for sure.

Title: Re: Static
Post by: erico on 2015-Aug-13
In my case my code is usually 95% globals... :(
Title: Re: Static
Post by: MrTAToad on 2015-Aug-13
When I did my speed tests several versions ago, there were speed advantages for each call to a function in extended types.

There should be no speed loss for STATIC variables.
Title: Re: Static
Post by: Slydog on 2015-Aug-13
@MrPlow:
QuoteAlso using Type Endtype for all variable declares is a bit too verbose for me essentially 4 lines of code for each global val...No?!

You could use just one TYPE for ALL globals.  Extending Hemlos's code:

Code (glbasic) Select
TYPE TGlobals:
  PlayerTimer%
  EnemyTimer%
  PlayerScore%
  TimeRemaining%
ENDTYPE
LOCAL Globals AS TGlobals

Globals.PlayerTimer = 1000
Globals.PlayerScore = 2000

Title: Re: Static
Post by: Paul Smith on 2015-Aug-13
Quote from: erico on 2015-Aug-13
In my case my code is usually 95% globals... :(

Me to :)

You should see the mess of my Latest app. not even sure what half of them do any more =D
Title: Re: Static
Post by: Hemlos on 2015-Aug-13
@ kano:
by header i mean the top of your program above the main loop where you define most of your globals.

I did research before sharing this K.
Globals are "extern" accessable....so you can use C++ tools to view data and memory leaks etc.
Static are not accessable this way, even though they are a global.
When you declare a GLOBAL, it gets pushed into a volatile area of your ram, thats how extern can access it....its GLOBAL for the whole computer, not just your program.....that said, this data is not secure.

I highly recommend everyone look into and study this a lil more, your code will be more reliable, and potentially safer for your computer, and your customers personal info better secured.
Title: Re: Static
Post by: kanonet on 2015-Aug-14
Quoteby header i mean the top of your program above the main loop where you define most of your globals.
This is just part of what i called the main area, in C++ it will be in the main function. That you did not define a main loop yet (you dont need one) does not change anything. Its simply the main function. And variables inside main function have same scope as variables inside every function, LOCALs/STATICs from main loop are not accessible inside any other function. You can define a GLOBAL anywhere in your program, it does not need to be at top of your main file.

QuoteI did research before sharing this K.
Globals are "extern" accessable....so you can use C++ tools to view data and memory leaks etc.
Static are not accessable this way, even though they are a global.
When you declare a GLOBAL, it gets pushed into a volatile area of your ram, thats how extern can access it....its GLOBAL for the whole computer, not just your program.....that said, this data is not secure.
If you use debugging/hacking/analysing tools you can access any variable in a program, no matter if it was a global, static or local. All are in stored in ram and ram can be read out completely by programs with high enough privileges. But this has nothing to do with globals, like I said you can also read out statics/locals. But without such special tools no variable is readable from outside your program, even not globals. If you still say otherwise I demand a prove/link.
Title: Re: Static
Post by: nabz32 on 2015-Aug-14
If you are doing object oriented coding in C++ globals are the devil,
they can easily be avoided.

When you declare a static each instance of your object ( or function ) will use
the same memory address for this static.

I would only recommend using statics inside types,
In types it can be very usefull to keep track of how many instances you have generated from this type,
increase it in the constructor, decrease it in the deconstructor.

If you really want to eliminate all GLOBALs in your code, you need strict discipline and a good sketch of your types before.

@Hemlos:
In C++ a memory space is generated for a STATIC, when you create the first instance of the object class or declare it the first time.
It is possible to declare it before you even have created an instance of the object.
Like in GLBasic each instance of a class shares the same memory area for the static variable.
PRIVATE means you can´t access the data directly when working with an instance of the class.

iE. instead of using today.day = x ; today.month = x ; today.year = x ( or today->day etc. )
it is far better to make those variables PRIVATE and declare an overloaded constructor ( today = new Date(10,8,2000) ) for the class.
So that you can build a check inside the constructor that keeps the date from beeing set to some invalid values.

It is all a thing of enabling users to work better as a team ( failproof shairing of libraries )

@kanonet
Yes you can read out everything in the RAM with the right tools,
there is no doubt about it.
Title: Re: Static
Post by: Hemlos on 2015-Aug-15
Quote from: nabz32 on 2015-Aug-14
If you really want to eliminate all GLOBALs in your code, you need strict discipline and a good sketch of your types before.

Indeed!

Quote
@Hemlos:
In C++ a memory space is generated for a STATIC, when you create the first instance of the object class or declare it the first time.
It is possible to declare it before you even have created an instance of the object.
Like in GLBasic each instance of a class shares the same memory area for the static variable.
PRIVATE means you can´t access the data directly when working with an instance of the class.

what he said.
But i read on the internet somewhere, that c++ static is not readable extern_ , i havent tested it.
TBH, i cant explain to you how it even is logically possible to not be able to read them with the extern tools.
Its an internal rules in c++ for security reasons?
But how else can it be hidden? (i think we are in disagreement here, but im always willing to learn.)
Have you even tested the fact of reading a static global externally?
From a different OS maybe?

Quote
It is all a thing of enabling users to work better as a team ( failproof shairing of libraries )
It sure it a standard around here, and thanks for some input!
Title: Re: Static
Post by: kanonet on 2015-Aug-15
I think you simply missunderstood that text. When it did say extern it did not mean extern of of its program, but extern of its scope. In C++ static has several meanings:
Remember, that in GLBasic only the 1st meaning of static exist, but not the other two.

Again all variable are for intern use of the program only and can only be accessed with special tools that use some kind of hacky way to break out of its own memory and read other programs memory. But that has nothing todo with the way variable scopes are designed. So I think you just got your text wrong.