English Amiga Board


Go Back   English Amiga Board > Coders > Coders. Language > Coders. Blitz Basic

 
 
Thread Tools
Old 06 June 2020, 22:44   #1
peceha
Registered User

peceha's Avatar
 
Join Date: Dec 2017
Location: Poland
Age: 44
Posts: 223
[blitz] re-dim arrays inside statement/function

Hi,
I have an array which I declared on main program level.
Is there a way to redim such an array within a statement?
Whenever I try I get *unable to free memory* error and yellow guru shortly after.

Thanks
peceha is offline  
Old 08 June 2020, 09:50   #2
Daedalus
Registered User

Daedalus's Avatar
 
Join Date: Jun 2009
Location: Dublin, then Glasgow
Posts: 4,764
Blitz 2 doesn't support the re-declaring of arrays - that was added in AmiBlitz 3. Perhaps a List array might suit the situation?
Daedalus is online now  
Old 20 June 2020, 13:20   #3
peceha
Registered User

peceha's Avatar
 
Join Date: Dec 2017
Location: Poland
Age: 44
Posts: 223
Wow, I forgot about it, sorry
Anyway, I found a solution already
Thanks
peceha is offline  
Old 21 June 2020, 19:25   #4
peceha
Registered User

peceha's Avatar
 
Join Date: Dec 2017
Location: Poland
Age: 44
Posts: 223
Quote:
Originally Posted by Daedalus View Post
Blitz 2 doesn't support the re-declaring of arrays - that was added in AmiBlitz 3. Perhaps a List array might suit the situation?

Hmmm...I Just wrote that in ab3 and got en error ARRAY SUBSCRIPT OUT OF RANGE
Code:
Dim a.b(10)
a(9)=9

Dim a.b(20)
a(11)=11       - - - Here comes an error
But this works:
Code:
Dim a.b(10)
a(9)=9

Dim a.b(20)
a(10)=10
peceha is offline  
Old 22 June 2020, 04:40   #5
Nightshft
Registered User

Nightshft's Avatar
 
Join Date: Mar 2018
Location: Austria
Age: 45
Posts: 397
Just tried this too.
In AB3 the error "subscript out of range" is thrown by the debugger -> If you disable the debugger it seems to work.

But I dont know if
- memory is correctly allocated at the redim, and the debugger is working incorrectly (wouldn't be the first time), or
- redimming is not possible in a clean way, because there isn't more memory allocated which means you read/write out of bounds and the debugger error is "reasonable".

About BB2.1:
- Just tried it in BB2.1 and it seemed to work.
- For what its worth, I know that I saw a game written in bb2 that redimmed arrays and it worked (the game didnt crash).
Nightshft is offline  
Old 22 June 2020, 05:35   #6
peceha
Registered User

peceha's Avatar
 
Join Date: Dec 2017
Location: Poland
Age: 44
Posts: 223
Actually I re-dim arrays in BB2 and So far I have not experienced any problem with my programs.
My original question was about redimminng inside procedures since that gave me an error (in BB2) when trying to redim global array.

If this is AB3 debugger's problem (but as you said - hard to know) then it is already second one I came upon up to now. First was a crush when setting setint and entering blitz Mode after that.

Thanks
peceha is offline  
Old 22 June 2020, 18:35   #7
Daedalus
Registered User

Daedalus's Avatar
 
Join Date: Jun 2009
Location: Dublin, then Glasgow
Posts: 4,764
Okay, some clarifications: Redimming an array was always possible, in the way of deleting the entire array and creating a brand new one with the same name. What I meant was added in AB3 was redimming the array while keeping the contents, and I assumed that's what you wanted to do...

Anyway, I've checked this, and having never used it before, I can't actually get resizing arrays to work in AB3. The KEEP keyword isn't tokenized, and running the suggested code (checking register D0 after the redim attempt to check for success) gives a failure every time, even with small size changes and plenty of free RAM.

In this case, the debugger is correct. If you redim an array, while turning off the debugger might allow you to run with it, the memory isn't allocated so you're poking about with potentially dangerous memory locations. Try it: When you dim an array first, every element is initialised to 0. If you redim it, reading an index beyond the original bounds will give a random value, meaning it hasn't been initialised and is either random RAM contents or actual data belonging to another program. So it's not safe to use this method.
Daedalus is online now  
Old 24 June 2020, 13:10   #8
peceha
Registered User

peceha's Avatar
 
Join Date: Dec 2017
Location: Poland
Age: 44
Posts: 223
Quote:
Originally Posted by Daedalus View Post
... Try it: When you dim an array first, every element is initialised to 0. If you redim it, reading an index beyond the original bounds will give a random value, meaning it hasn't been initialised and is either random RAM contents or actual data belonging to another program. So it's not safe to use this method.
Why didn't I think of it? Now it's clear something is broken there, I will open a ticket on github page, let's see if this can be fixed...
peceha is offline  
 


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Similar Threads
Thread Thread Starter Forum Replies Last Post
Forward statement or function Nightshft Coders. Blitz Basic 5 17 April 2020 01:02
for..each statement in CLI pieter1976 support.Other 10 09 January 2020 22:15
Share NewType to Statement/Function earok Coders. Blitz Basic 21 30 May 2018 13:37
Blitz help with Dim gazj82 Coders. Blitz Basic 12 28 September 2017 17:47
A2000 with Picasso II, screen is dim LuigiThirty support.Hardware 1 16 August 2017 07:33

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 09:52.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2020, vBulletin Solutions Inc.
Page generated in 0.10032 seconds with 14 queries