srod wrote:To my mind, PB has it right in that pointers, in terms of de-referencing operations at least, are intended really for structures. It keeps things very 'clean' and simple.
My opinion is that pointers are really useful and indispensable. I'm only wondering about the need for a *stringpointer.s="pppppp" that acts differently from all other pointers, acts differently if used in a procedure parameter or in a standalone assignment, and may also lead to errors because it doesn't handle memory allocation (or something like that).
Incidentally, the following is a kind of workaround in that you don't need to define a structure of type STRING in order to call the procedure and avoids Peek's and Poke's!
Code: Select all
Procedure ModString(a)
*x.string=@a
*x\s="2434567892434567892434" ; this screws things up no longer!
EndProcedure
x.s
x="ab"
ModString(@x)
Debug x
Unfortunately the above procedure doesn't work too.
You can see it if you add another variable. Try this way:
Code: Select all
Procedure ModString(a)
*x.string=@a
*x\s="2434567892434567892434767767677676" ; this screws things up
EndProcedure
x.s
y.s
x="ab"
y="cd"
ModString(@x)
Debug x
I think that this is happening because what you're doing here is a sort of manually recreating a structure containing a s.s, using a pointer to pointer. But probably the compiler doesn't see it as a real structure, so it doesn't handle memory properly.
In the end, it would seem that the only way for passing a string to a procedure and being able to modify it freely from inside the procedure, was utilizing a structure of type .string instead of a simple string, and passing a pointer to that structure like in the first code snippet of this thread. ( not considering inline assembly and/or manual memory allocation/garbage collection)
edit:
Here we can see how memory is handled properly, with a reallocation of the x\s buffer.
Code: Select all
Procedure ModString(*a.string)
*a\s="2434567892434567892434767767677676"
EndProcedure
x.string
y.string
x\s="ab"
y\s="cd"
Debug @x\s
ModString(@x)
Debug x\s
Debug @x\s ; here we can see that the buffer of x\s has been reallocated since its address has changed.