June 17, 2024, 04:48:21 AM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Declaring variables

Started by Brian, February 09, 2014, 06:01:51 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Brian

Hi,

I've often wondered about declaring variables: To declare them at the start of a program, or declare within a sub?

Is there a performance hit (obviously going to be minimal), to, say, declare STRING myString at the start of a
program, or STRING myString within a SUB? Does the STRING inside the SUB re-initialise every time that SUB is used?

If so, I would have thought it would be best to pre-declare it

Just a random thought . . .

Brian

LarryMc

I guess the answer is it depends.
If you have a small program then it's probably okay to make your variable source file global.

If you have a large program and use a lot of indexes you can wind up having trouble keeping track of what each index does.

If a variable is declared globally then if you change the value anywhere in your program it changes it everywhere; both inside and outside subroutines.

If you declare a variable locally(inside a subroutine) then it doesn't change outside the sub.
that way you can use def x:int in every sub you have and one sub will never interfere with the x in another sub.
Also, each time you enter those subs the x is reset unless you use the STATIC keyword when you define it.

Where you can really confuse yourself is declaring the same variable, say x, both globally and locally.
The local variable overrides(ignores) the value in the global variable and vice versa.
So you can wind up thinking "well this isn't changing when I expect it to; why"

It's handy to put all your variables in the beginning but it can cause you headaches.

My rule of thumb is if it is only used in a sub then declare it in the sub.

LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

AdrianFox

That's a very interesting question that has also had me wondering about whether I am using up more memory than needed in some of my programs.

Being a 'literary' ex English teacher type of programmer, I often use extremely long ISTRINGS which I define in an include file as part of my program.  I only call these at a particular point in the program from a menu item (for example, to display an example text in an edit box,  consisting of 3 or 4000 characters or more.)   Is each of these strings stored in memory when the program is compiled and run, or would it be better and more efficient to only define the string in the sub which I call when I want to display the text contained in this particular Istring?  Does it make any difference?  I read what Larry says but still not sure if it makes any difference to the amount of memory used when the program is run.

Adrian Fox

Sam

Variables defined outside of a subroutine are global. That means that space has been allocated for them and will remain allocated for the life of the program. Variable defined inside a sub have space allocated and de allocated with each call to the sub - unless the variable is declared static.
So, it's a trade off of memory used vs. speed. In most cases, with todays computers, the difference is insignificant.
In Brian's case, unless you are calling the sub extremely frequently, I would definitely go with Larry's advice and declare inside the sub.
In Adrian's case, if you have a bunch of really long strings, you may want to consider storing then in a file and read them in when needed. You could store then in a string table resource as well. That still uses memory and I'm not sure if it compacts them. But you would be able to retrieve them faster.

aurelCB

I am not sure but i think that when you create pointer as local variable then is visible
as global but maybe i am wrong.
When i use EB i create all my big strings as global and when i don't need them just delete
or use global pointer and cast as string for string operations then delete pointer and release memory.
But this was a long time ago... :-\

Sam

February 10, 2014, 10:39:27 AM #5 Last Edit: February 10, 2014, 10:52:24 AM by tennisbum
Quote from: aurelCB on February 10, 2014, 04:34:26 AM
I am not sure but i think that when you create pointer as local variable then is visible
as global but maybe i am wrong.

Attempting to reference any variable (pointer or otherwise) that was created inside a sub, outside of that sub, will result in compiler failure - undefined variable.

Of course you can make that variable available to the rest of the program either by setting it as the subs return value or by passing a parameter by reference and setting that parameter equal to the variable.