BBC BASIC for Windows
« Schroedinger’s String »

Welcome Guest. Please Login or Register.
Apr 6th, 2018, 12:22am



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: Schroedinger’s String  (Read 766 times)
Andy Parkes
Developer

member is offline

Avatar




PM

Gender: Male
Posts: 25
xx Re: Schroedinger’s String
« Reply #7 on: Jan 8th, 2014, 10:42am »

Hi Richard,

I'm glad you've asked, because in answering, I've discovered that there is still something that I don't quite understand.

The program accessible from the Wiggio link below, reproduces the difficulty I was encountering and also demonstrates my solution. However, the fact that the same output is generated in BB4W_v6 tells me that this has nothing to do with LOCAL string management after all (although the insight I've gained into this was worth the extra confusion).

http://wiggio.com/yui/folder/stream_file.php?doc_key=QVteex27abiyemnhNemnSSNGUImoPAe4Y5ZCmINRI+I=

The issue occurs when I take a string variable (LOCAL or not), and indirectly set its header to point to a string at a known memory address. I can then, as I expected, use this string variable to directly access the string at the given memory address - OK! But for some reason that I don't yet understand, repeating this process for different memory address' does not appear to work in my example?

I understand the examples in your previous post, such that when the length of a string is changed, it may (or may not) be relocated on the heap to accommodate the new length of the string. But as I am not changing the length of any strings, I think there is something else at play here.

Andy
User IP Logged

admin
Administrator
ImageImageImageImageImage


member is offline

Avatar




PM


Posts: 1145
xx Re: Schroedinger’s String
« Reply #8 on: Jan 8th, 2014, 12:53pm »

on Jan 8th, 2014, 10:42am, Andy Parkes wrote:
The program accessible from the Wiggio link below, reproduces the difficulty I was encountering and also demonstrates my solution.

Your program appears to be implementing a linked list using structures. Have you seen my example of doing this on the Wiki:

http://bb4w.wikispaces.com/Linked+lists+using+structures

One thing you'll notice is that I never explicitly manipulate the 'format pointer' of the structure, I use only built-in language features to look after that; only the 'data pointer' is explicitly loaded. In my opinion that makes the code cleaner and easier to follow.

Another thing about your code that I'm not enthusiastic about is this way of discovering the size of a structure:

Code:
      pFrmt% = !^Node{}
      dataSize% = !(pFrmt%) 

It works, but using the built-in DIM() function is more straightforward:

Code:
      dataSize% = DIM(Node{}) 

Quote:
The issue occurs when I take a string variable (LOCAL or not), and indirectly set its header to point to a string at a known memory address.

Don't do it! BBC BASIC's string handling and garbage collection routines assume that the memory occupied by a string was allocated by the interpreter for that specific purpose. The sort of thing that can easily go wrong if you 'do it yourself' is that two different string variables can end up pointing to the same memory address, with potentially nasty consequences!

Quote:
But for some reason that I don't yet understand, repeating this process for different memory address' does not appear to work in my example?

It's pretty easy to see why FN_getMemberBAD is going to fail. You poke an address into the descriptor of LOCAL string variable name$, so when the function exits the interpreter will free the local variable in the usual way, hence 'freeing' memory that it didn't allocate in the first place (and which definitely isn't 'free')!

In over a decade of programming in BBC BASIC for Windows I have never been tempted to play tricks of the sort you are using. Partly that's because I know they won't work (reliably), but partly it's because I have never had cause to - there has always been a more 'legitimate' way of achieving the same result.

For example in the Wiki article to which I link above the equivalent routine to your FN_getMemberGOOD and FN_getMemberBAD is FNgetitem:

Code:
      DEF FNgetitem(RETURN n{}, n%)
      IF n% = 0 THEN = ""
      !(^n{}+4) = n%
      = n.item$ 

Richard.
User IP Logged

Andy Parkes
Developer

member is offline

Avatar




PM

Gender: Male
Posts: 25
xx Re: Schroedinger’s String
« Reply #9 on: Jan 8th, 2014, 7:27pm »

Hi Richard,

That's fantastic, thanks very much cheesy. I see that I've missed quite a few fundamental things, and while I apologise for not having first studied the linked list example on the wiki, I've learned more by doing it the hard way.

Quote:
when the function exits the interpreter will free the local variable in the usual way, hence 'freeing' memory that it didn't allocate in the first place (and which definitely isn't 'free')!


Thank you, I've finally made the connection! So that’s why it was writing the next string insertion over the top of the previous one, because the internal string management freed the memory occupied by the string, every time my function fetched it.

I see that the technique is never going to be a good idea. Even if I was to use a global string variable for the same purpose, its just creating an unnecessary risk. I recognise that its a hack, and the wrong way to go about solving the problem.

Right, well I have some reworking to do of the project which started this, but it will be so much the better for it.

Thank you very much for your help

Kind Regards

Andy
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