Do pointers exist in basic4ppc?

rmschreuder

Member
Licensed User
In C++ there are variables that can be declared pointers, so one can address more objects of the same type by setting a pointer to point to one of them.
This is very usefull, does that exist in b4ppc?

Mike

:confused:
 

agraham

Expert
Licensed User
Longtime User
No. They are considered dangerous due to type-safety and buffer overrun considerations and are discouraged in .NET based languages. C# for example offers reference parameters and delegates, which are type-safe function pointers, to avoid the need for the explicit use of pointers.

Also most objects in .NET are adressed by reference so setting a variable to an instance of a reference type is actually setting a pointer to it. For instance a Basic4ppc array is a reference type so if you assign the same arrays to two different array variables they will all actually be be the same array.

Dim A(10)
Dim B(0)
Dim C(0)
...
B() = A()
C() = A()
A(0) = "fred"
msgbox(B(0) & " " & C(0)) ' shows "fred fred"
 
Last edited:

taximania

Well-Known Member
Licensed User
Longtime User
B4X:
B() = A()
C() = A()
A(0) = "fred"
msgbox(B(0) & " " & C(0)) ' shows "fred fred"

Sorry for me being thick AGraham. Are you saying,

if I'm using an array a()
and want a copy of the data currently in the array, b()=a()
then alter the data in a()
when I go back to check my stored data in b(), it will have changed.

This sounds totally dangerous :sign0137:
Or have I totally misread or understood your post.
 

agraham

Expert
Licensed User
Longtime User
B4X:
when I go back to check my stored data in b(), it will have changed.[/QUOTE]That is correct, try it. :)
[code]
Sub Globals
   'Declare the global variables here.
   Dim A(1)
   Dim B(0)
   Dim C(0)
End Sub

Sub App_Start
   B() = A()
   C() = A()
   A(0) = "fred"
   Msgbox(B(0) & " " & C(0))
End Sub
It's not really dangerous as long as you know what is happening. This is the way .NET reference types work, and just about everything apart from numbers are reference types in .NET. In Basic4ppc you would use ArrayCopy if you wanted to make sure the original array was unchanged.
 

specci48

Well-Known Member
Licensed User
Longtime User
B4X:
This sounds totally dangerous :sign0137:[/QUOTE]
References are not dangerous, but it's dangerous not to know if you work with a reference or not. :)

If you need a real copy of an array you have to use [B]ArrayCopy[/B].

[CODE]Sub Globals
   'Declare the global variables here.
    Dim A(10)
    Dim B(0)
   Dim C(0)

   Dim AA(10)
    Dim BB(10)
   Dim CC(10)
End Sub

Sub App_Start
   Form1.Show
   
   A(0) = "taxi"
   B() = A()
   C() = A()
   A(0) = "fred"
   Msgbox(B(0) & " " & C(0)) ' shows "fred fred" 

   AA(0) = "taxi"   
   ArrayCopy(AA(), 0, ArrayLen (AA()), BB(), 0)
   ArrayCopy(AA(), 0, ArrayLen (AA()), CC(), 0)
   AA(0) = "fred"
   Msgbox(BB(0) & " " & CC(0)) ' shows "taxi taxi" 

End Sub

specci48


Edit: sorry agraham, 4 minutes late....
 

taximania

Well-Known Member
Licensed User
Longtime User
Many thanks Agraham and Specci48.
This was something I was totally unaware of.

I knew ArrayCopy existed, but never knew why.
a()=b() Sorted :sign0060: NOT !!

I'm glad I asked the question, It might be of help to others.
I've probably spent hours in the past trying to figure out why arrays don't
contain the data I thought they should do.

copy array :purelly written for the forum search

:) Ta guys
 
Top