Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why does this crash?
#1
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?
Reply
#2
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.
Reply
#3
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
ngelo Mottola - EC++
Reply
#4
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.
Reply
#5
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.
Reply
#6
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).
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)