Author Topic: Local initalization  (Read 2476 times)

0 Members and 1 Guest are viewing this topic.

JRS

  • Guest
Local initalization
« on: January 20, 2013, 10:50:37 PM »
Quote from: Frederick Harris - Jose Roca Forum
I know in my PowerBASIC coding I frequently rely on the compiler initializing a local to zero.  Only by looking close at a number of programming statements in the body of the procedure on a case by case basis would it be possible to determine whether it was/is necessary or not.

Code: [Select]
SUB test
LOCAL a
PRINT a,"\n"
END SUB

test

jrs@laptop:~/sb/test$ scriba iszero.sb
undef
jrs@laptop:~/sb/test$


@Charles - How does OxygenBasic handle local variable initialization. Is PowerBASIC unique in how it handles local variables?


Aurel

  • Guest
Re: Local initalization
« Reply #1 on: January 20, 2013, 10:55:54 PM »
This is the way how works..easy..right?
Code: [Select]
SUB test
INT a
a=5
PRINT str(a)
END SUB

test()

Charles Pegge

  • Guest
Re: Local initalization
« Reply #2 on: January 21, 2013, 03:42:04 AM »
In the Basic tradition, Oxygen initialises all variables (including arrays) to null.
This can be overridden for local variables in a procedure but it is seldom needed.

Oxygen also delivers clean memory blocks, even at low level:

sys a=getmemory 0x1000

a now points to a block of 4096 null bytes.
« Last Edit: January 21, 2013, 03:53:03 AM by Charles Pegge »

JRS

  • Guest
Re: Local initalization
« Reply #3 on: January 22, 2013, 06:57:38 AM »
Quote from: JCFuller - BASIC programming forum
Ed,
  You did have the right idea but instead of b= 0 if I use braces as in int b={0}; I can use it for all data types. I implemented it but need to do a lot more testing. Thanks for the idea.

James

I agree with how Charles is initializing data and structures. What is wrong with using NULL? bc9 is a C translator and that is how C initialize its data and structures from what I understand.

Code: [Select]
a = NULL
a$ = NULL
a[NULL]
a = MyFunction(arg1,arg2,NULL)

In ScriptBasic the undef serves the same purpose as NULL.


« Last Edit: January 22, 2013, 07:03:14 AM by JRS »

Charles Pegge

  • Guest
Re: Local initalization
« Reply #4 on: January 23, 2013, 02:25:30 AM »
In Oxygen, initialisation of local variables to null is the normal rule but it is possible to override it.

function f()
#noinit
sys a
print a
end function

f  'result: '1572416 etc


However this will never work for strings. They are always set to null


Another way to bypass null initialisation is using implicit dim, (a bit like scriptbasic):

va=123   'va is automatically created as sys
vb=42.5  'vb is automatically created as double float


Charles

JRS

  • Guest
Re: Local initalization
« Reply #5 on: January 23, 2013, 09:21:52 AM »
Quote
Another way to bypass null initialisation is using implicit dim, (a bit like scriptbasic):

implicit dim  - I like that term better then smart vars.  :D