BBC BASIC for Windows
« Loss of Array data »

Welcome Guest. Please Login or Register.
Apr 5th, 2018, 11:46pm



ATTENTION MEMBERS: Conforums will be closing it doors and discontinuing its service on April 15, 2018.
Ad-Free has been deactivated. Outstanding Ad-Free credits will be reimbursed to respective payment methods.

If you require a dump of the post on your message board, please come to the support board and request it.


Thank you Conforums members.

BBC BASIC for Windows Resources
Online BBC BASIC for Windows documentation
BBC BASIC for Windows Beginners' Tutorial
BBC BASIC Home Page
BBC BASIC on Rosetta Code
BBC BASIC discussion group
BBC BASIC for Windows Programmers' Reference

« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: Loss of Array data  (Read 684 times)
Matt
Developer

member is offline

Avatar




PM

Gender: Male
Posts: 210
xx Loss of Array data
« Thread started on: Jul 19th, 2013, 7:01pm »

Hi,

During the following code from a procedure, the array data disappears, but I cannot find the cause.

Index%() is the global index array. ndx%() is the temporary index array which is later SWAPped in the PROC with Index%(). The MaxIndexes% is set to 64K. IndexTop% at this time is only set to 9 for test purposes.

Code:
      LOCAL sort$(), ndx%()
      FOR I = 0 TO IndexTop% : PRINT Index%(I) : NEXT
      DIM sort$(4, IndexTop%), ndx%(MaxIndexes%)
      FOR I = 0 TO IndexTop% : PRINT Index%(I) : NEXT 
The first print loop shows the Index%() array intact. However, the second one, which should not have been affected, shows the Index%() array completely cleared. The Cross Reference shows nothing unnusual in this PROC, nor does the Memory Monitor - although I'm less certain of what I'm looking at here.

One of the curious things is that it only happens on the second pass through the PROC. The first pass works fine.

Any ideas what else I might try, or any other info that you might need to help me?

Matt
User IP Logged

admin
Administrator
ImageImageImageImageImage


member is offline

Avatar




PM


Posts: 1145
xx Re: Loss of Array data
« Reply #1 on: Jul 19th, 2013, 8:47pm »

on Jul 19th, 2013, 7:01pm, Matt wrote:
ndx%() is the temporary index array which is later SWAPped in the PROC with Index%().

You mustn't swap a local array with a global array. The BB4W docs call this out:

"LOCAL arrays are stored on the stack and special precautions are required as a result. Firstly, avoid swapping a local array with a global array (i.e. one stored on the heap).... You can safely copy the array, because the data is copied rather than the pointer":

http://www.bbcbasic.co.uk/bbcwin/manual/bbcwini.html#hint5

You can, quite safely, either swap the individual elements:

Code:
FOR I = 0 TO IndexTop% : SWAP Index%(I),ndx%(I) : NEXT 

or do the swap by copying:

Code:
tmp%() = Index%() : Index%() = ndx%() : ndx%() = tmp%() 

Richard.
« Last Edit: Jul 19th, 2013, 8:56pm by admin » User IP Logged

Matt
Developer

member is offline

Avatar




PM

Gender: Male
Posts: 210
xx Re: Loss of Array data
« Reply #2 on: Jul 21st, 2013, 05:21am »

Hi,

Just noticed my last reply to this seems to have been sucked up into the ether.

on Jul 19th, 2013, 8:47pm, Richard Russell wrote:
You mustn't swap a local array with a global array. The BB4W docs call this out:

"LOCAL arrays are stored on the stack and special precautions are required as a result. Firstly, avoid swapping a local array with a global array (i.e. one stored on the heap).... You can safely copy the array, because the data is copied rather than the pointer"

I hadn't realised this as I have only read the main help on it. But that's fine. I only need to copy the ndx%() to Index%() so I'll use the loop.

I assume, by trial and error, that Index%() = ndx%() only copies the pointers as well.

I would still be interested to know why the array was cleared before the DIM and then restored after with no apparent movemement of data or pointers, and in this case only during the second pass.

Matt
User IP Logged

admin
Administrator
ImageImageImageImageImage


member is offline

Avatar




PM


Posts: 1145
xx Re: Loss of Array data
« Reply #3 on: Jul 21st, 2013, 06:51am »

on Jul 21st, 2013, 05:21am, Matt wrote:
I assume, by trial and error, that Index%() = ndx%() only copies the pointers as well.

Huh? My previous reply (second example) shows that the opposite is the case, and the docs also state explicitly "You can safely copy the array, because the data is copied rather than the pointer". Note the comments in this code taken from the docs:

Code:
      SWAP globalarray(),localarray() : REM Don't do this!
      globalarray() = localarray() : REM This is OK
 

Anyway, think about it. Copying pointers wouldn't copy the array, it would create an alias of the array: modifying the contents of one would cause the contents of the other to change!

Quote:
I would still be interested to know why the array was cleared before the DIM and then restored after with no apparent movemement of data or pointers, and in this case only during the second pass.

Clearly what you describe, taken literally, is impossible. Trying to find out what actually did happen could be difficult and ultimately will only confirm that swapping a local and global array is a very bad idea!

Richard.
User IP Logged

Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

| |

This forum powered for FREE by Conforums ©
Terms of Service | Privacy Policy | Conforums Support | Parental Controls