Qbasicnews.com

Full Version: fb newbie having problems...
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm pretty new to basic's but have been programming c/c++/java several years. I thought to try out FB just for fun.

First thing I did was to try out ptr_test.bas. I changed MAXNODES to 1000 and the program simply exited with output "result:"

I tried several values and found out that with MAXNODES=715 it ran just fine but with anything more it either generated windows errors or exited.

Seckond thing I tried out was dynamic memory allocation. I wanted to have a function that takes a int parameter and return a float array with given length. I tried several things but nothing worked. Probably because I had no documentation and compiler didn't generate any meaningful compile errors/warnings, also during runtime it exited with no error messages several times.

In conclusion I have two questions:

1)Why does the ptr_test.bas crash when bigger list is created?
2)How to make a function that returns a dynamically created variable?
The problem with #2 is, any data allocated for arrays is local in scope, meaning it's automatically deallocated for you when the function exits. The closest you could get to what you want is reallocating a global array, or a parameter array, to the requested size.

Untested example:
Code:
declare sub resize(array() as single, newSize as integer)

dim myArray(1 to 10) as single
resize myArray(), 20
end

sub resize(array() as single, newSize as integer)

redim array(1 to newSize)

end sub
Tere Ho Ho, tere tulemast foorumisse :wink:
Crash happened coz PRINT was limited to 80x25 chars at time, i was too lazy to correct that (didn't think ppl would find that so soon ;).. fixed, size is unlimited now.. WRITE still has that limit tho.

You can't return dynamic vars unless you malloc 'em and return a ptr like:

Code:
'$include: 'crtdll.bi'

function intfunc as integer ptr
  
  intfunc = malloc( len( integer ) )

end function


   dim p as integer ptr

   p = intfunc  

   *p = 1234

(can't do function dereferencing yet, no *intfunc() allowed)

ALLOCATE() and DELETE() intrinsic functions are planned, they would be mapped to malloc() and free() anyway.