Author |
Topic: Loss of Array data (Read 684 times) |
|
Matt
Developer
member is offline


Gender: 
Posts: 210
|
 |
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
|
|
Logged
|
|
|
|
admin
Administrator
member is offline


Posts: 1145
|
 |
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 » |
Logged
|
|
|
|
Matt
Developer
member is offline


Gender: 
Posts: 210
|
 |
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
|
|
Logged
|
|
|
|
admin
Administrator
member is offline


Posts: 1145
|
 |
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.
|
|
Logged
|
|
|
|
|