Can I change default initial values?

New to FreeBASIC? Post your questions here.
Post Reply
tick_da
Posts: 10
Joined: May 11, 2015 16:40

Can I change default initial values?

Post by tick_da »

Sometimes I forget to initialize a variable. And it is not always possible to find such bug by testing. Sometimes the bug can be noticed only when I copy that code to a different context. That's because in a specific context the default initial values (zero for integers and empty string for strings) can give the correct result although the code itself is wrong.

A few days ago I discussed a different problem: mistyping a variable name in '-lang qb' mode. Some people suggested me to use '-lang fb' mode instead.

Unfortunately '-lang fb' mode cannot help me when I forget to initialize a variable. Take a look at the following code

DIM I AS INTEGER
PRINT I

Obviously it is a programming error. In FreeBASIC this error is not as nasty as in C. But still it would be nice to have a solution.

I want to discuss a possible solution (to the initialization problem). What if I could change the default initial values for testing purposes? In this case the correct program should give me exactly the same results. This feature does not require any run-time checks and I guess it is relatively easy to implement.

So do you think it is a good idea? Or maybe I have missed something?
vdecampo
Posts: 2992
Joined: Aug 07, 2007 23:20
Location: Maryland, USA
Contact:

Re: Can I change default initial values?

Post by vdecampo »

What type of initialization do you want. FB initializes all new numeric variables to zero.

Optionally you can do...

DIM AS INTEGER I = 100
PRINT I

Not sure what you're looking for.

-Vince
tick_da
Posts: 10
Joined: May 11, 2015 16:40

Re: Can I change default initial values?

Post by tick_da »

vdecampo, I am talking about default initial values not about optional initial values. I want to have ability to change these defaults to something else (to make sure I did not forget to initialize a variable).

Trying to change optional initial values for testing purposes is too hard and error prone. Because then I will have to change manually the optional initial value of every variable in my sources. This will add even more errors to my sources instead of fixing them.

Besides, I just noticed that my topics are in 'Beginners'. This is wrong. They should be in 'Community Discussion'. Because I try to discuss the FreeBASIC project itself and the features that should be in the FreeBASIC. I think it is a good idea to discuss a possible feature before posting a feature request.
grindstone
Posts: 862
Joined: May 05, 2015 5:35
Location: Germany

Re: Can I change default initial values?

Post by grindstone »

In my opinion such a feature would cause more problems than it solves.

But if you really want to utilize it, you could write a code manipulator which scans the source code for DIM - statements and modyfies them by inserting an initial value of your choice to every variable. Maybe you could even pipe its output directly to the fbc - compiler, so the code file hasn't to be changed.

Greetings
grindstone
marcov
Posts: 3462
Joined: Jun 16, 2005 9:45
Location: Netherlands
Contact:

Re: Can I change default initial values?

Post by marcov »

Such behaviour is usually reserved for the heap manager (malloc/free). They usually initialize the BSS segment memory on startup (if the OS doesn't guaranteedly it). So writing your own heap management might be useful, and its howto would be worth documenting.

Note that a default value other than zero set in such way may cause trouble with automated types whose helpers might rely on being initialized to zero.
RockTheSchock
Posts: 252
Joined: Mar 12, 2006 16:25

Re: Can I change default initial values?

Post by RockTheSchock »

Maybe you can use a variant type and overwrite the let operator. So it s possible to catch initialisation. Or you can modify the compiler let operator to issue a warning.
integer
Posts: 408
Joined: Feb 01, 2007 16:54
Location: usa

Re: Can I change default initial values?

Post by integer »

tick_da wrote:...I am talking about default initial values not about optional initial values. I want to have ability to change these defaults to something else (to make sure I did not forget to initialize a variable).

Trying to change optional initial values for testing purposes is too hard and error prone. Because then I will have to change manually the optional initial value of every variable in my sources. This will add even more errors to my sources instead of fixing them.
I'm trying to understand how I could use this:

Code: Select all

#DefaultInitialization INTEGERS -7
#DefaultInitialization UINTEGERS +755
#DefaultInitialization BYTE 21
#DefaultInitialization UBYTE 17
#DefaultInitialization LONGINT -4252527
#DefaultInitialization ULONGINT 1
#DefaultInitialization SINGLE nan
#DefaultInitialization DOUBLE nan
#DefaultInitialization STRING "undefined"
#DefaultInitialization SHORT -75
#DefaultInitialization USHORT 75
#DefaultInitialization LONG 125
#DefaultInitialization ULONG 175
#DefaultInitialization ZSTRING "ZUDEF"
#DefaultInitialization WSTRING "WUDEF"
#DefaultInitialization Pointer -1
#DefaultInitialization ptr -1 
Except for the floating points being 'nan' (not a number), it appears as if I would still have to test
the variable for the default value.

That means that I would have to establish a normally not accessible value in the program.
It makes me uneasy to accept: I am modifying some could you default initialized and I get
weird results. Where do I start to find the adjustment?

Just wondering.
It appears (to me) that this would create an instability.
marcov
Posts: 3462
Joined: Jun 16, 2005 9:45
Location: Netherlands
Contact:

Re: Can I change default initial values?

Post by marcov »

Well, default values for types are not something new. Being able to do default(t) is great for generics (to initialize a yet unknown type).

OTOH, the user doesn't have much control over what the exact value is, that is compiler builtin
tick_da
Posts: 10
Joined: May 11, 2015 16:40

Re: Can I change default initial values?

Post by tick_da »

grindstone wrote:you could write a code manipulator which scans the source code for DIM-statements
The feature is supposed to be used in '-lang qb' mode. Some variables will have no DIM-statements.
integer wrote:to establish a normally not accessible value
Of cause I don't want to touch any non-accessible values. To be useful the feature should change only those default values that first appear in my source code, i.e. only those variables that I forgot to initialize. If I didn't forget to initialize everything (and didn't introduce a new variable accidentally by mistyping a variable name) then my program will give the same results even if the default values are different because in this case my program does not depend on the default values.

The idea is to have only one simple compiler switch for changing defaults for all numeric and string types from zero values to default maximum possible values. This switch will be useful only for testing (to make sure that my program does not depend on the default values). But this switch should have no influence on external libraries that I use in my program.

Well, as I see no one is really interested in this feature except me. So there is no point in writing a feature request.
RockTheSchock
Posts: 252
Joined: Mar 12, 2006 16:25

Re: Can I change default initial values?

Post by RockTheSchock »

Wouldn't it be enough to just issue a warning if a variable gets assigned a constant value more than once?

@dkl or any other fbc developer. (I did take a brief look into fbc 1.0 sources, i am not an expert!):
Just add a CONST_ASIGNMENT flag to "type ASTNODE" which is checked/set if an FBVALUE is assigned.


Oh i just realized it's exactly the opposite of what was asked for.
There you need to add a counter and just traverse the AST in the end and issue a warning if the counter is smaller than 2.
tick_da
Posts: 10
Joined: May 11, 2015 16:40

Re: Can I change default initial values?

Post by tick_da »

RockTheSchock wrote:issue a warning if a variable gets assigned a constant value more than once?
Actually "less than once" (or "less than twice" if we also count assignments made by the compiler itself) is a better description. But I don't care if this value is constant or not. Using INPUT as a source for values is also acceptable for me.
vdecampo
Posts: 2992
Joined: Aug 07, 2007 23:20
Location: Maryland, USA
Contact:

Re: Can I change default initial values?

Post by vdecampo »

Are you describing an 'Immutable' attribute? You want a variable that can only be assigned once. Correct?

-Vince
tick_da
Posts: 10
Joined: May 11, 2015 16:40

Re: Can I change default initial values?

Post by tick_da »

vdecampo wrote:You want a variable that can only be assigned once. Correct?
No, I want that all variables be assigned at least once (not including auto-assignment of zero value or empty string by the compiler itself).
Post Reply