Qbasicnews.com
Why does this crash? - Printable Version

+- Qbasicnews.com (http://qbasicnews.com/newforum)
+-- Forum: Qbasic "like" compilers/interpreters (http://qbasicnews.com/newforum/forum-5.html)
+--- Forum: FB Discussion & Programming Help (http://qbasicnews.com/newforum/forum-15.html)
+--- Thread: Why does this crash? (/thread-6302.html)



Why does this crash? - keeling - 03-03-2005

Code:
redim OperatorStack () as Ubyte
  
   dim i as integer
  
   redim OperatorStack (10) as Ubyte

   for i = 0 to 10
      OperatorStack(i) = i
   next i

   sleep

   redim OperatorStack (20) as Ubyte
  
   for i = 0 to 19
      ? i, OperatorStack(i)
   next i

I was looking to see if redim in FB had a preserve nature to it. When I ran something similar to the above (and the above), it prints the numbers and then I get a lovely little error message on WinXP Pro. What gives?


Why does this crash? - Sterling Christensen - 03-03-2005

At least it doesn't crash on Linux, though it doesn't print anything.

I played around with the code a bit - I can't get it to work.


Why does this crash? - lillo - 03-03-2005

It works as it should, i.e. waits a keypress, then prints the entries. Linux, FB CVS version.

By default redim does not preserve the array contents when redimensioning; to get what you want, your redim line should be changed to:
Code:
redim preserve OperatorStack (20) as Ubyte



Why does this crash? - keeling - 03-03-2005

Code:
declare sub QuickCheck(dummy as string)
? "Start"

   QuickCheck "Dummy"

sleep
end

sub QuickCheck (dummy as string)
   redim OperatorStack () as Ubyte
    
   dim i as integer
    
   redim OperatorStack (10) as Ubyte

   for i = 0 to 10
      OperatorStack(i) = i
   next i

   sleep

   redim OperatorStack (20) as Ubyte
    
   for i = 0 to 19
      ? i, OperatorStack(i)
   next i
end sub

When I ran the code by itself it doesn't crash, BUT when I put it in a subroutine (like I had it before, I just didn't post that part) it does crash.


Why does this crash? - keeling - 03-03-2005

Quote:By default redim does not preserve the array contents when redimensioning; to get what you want, your redim line should be changed to:
Code:
redim preserve OperatorStack (20) as Ubyte

Yes, I am familiar with that format, I was just playing around with the REDIM statement when I came across the problem.


Why does this crash? - v3cz0r - 03-03-2005

Another one i missed..

DIM|REDIM array w/o explicit dimensions won't be allowed anymore inside procs, as the descriptor size is unknown, making it impossible (the way the compiler works) to reserve the right amount of space for each variable on stack.

I could just assume the array had the max number of dimensions allowed (16), but that would waste memory and "DIM|REDIM array()" is just "aesthetic", as it does nothing -- but it looks better when declaring dynamic shared arrays on modules, that used to be done with "REDIM array( 0 )" that will waste code as the module main-level is never called ("DIM array()" looks better :P).