pointer to a string

Just starting out? Need help? Post your questions and find answers here.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Danilo.

I also thought *c.s is a pointer to a string, because '*' means
_always_ pointer in the PureBasic language.
In my opinion, the whole string stuff in PB isnt very good
and limited.

If every string would have its own memory address, the
strings would also be unlimited in size - like in all
other languages.

Internally, the string should be a pointer that holds
the address of the string.

So, for ' A$ = "Test" ' it allocates 5 Bytes of memory with
GlobalAlloc_(), copies "Test" + NULL to this memory and
writes down the address of the string/memory into the internal
variable v_string_A.

Now the coder wrote ' A$ + ": Hello World" '

Internally, the compiler should now get the current length of
the string with GlobalSize_(v_string_A) and save it in a temp
location (register or mem).
Now it makes GlobalReAlloc_() with the old size (4) + size of the
string to add (13) + 1 (ending NULL) and write the address
returned by GlobalReAlloc_() into v_string_A.
Done that, it can copy the new string to v_string_A pointer
+ oldsize and add a NULL at the end.

A pointer to that string, lets say ' *c = @A$ ' tells the
compiler if you use that pointer, it always loads the actual
address of the string from v_string_a internally.
To make a difference with other pointers, a string-pointer
type *c.s would be fine. So the compiler knows this is a
pointer to a string and generates code that doesnt write
to the internal variable pointer_c directly, but to the real
address of the string that is stored in v_string_A.
(So pointer_c contains always the address to v_string_A)

With this way strings could also be 10MB, not limited to 64k.

Its the same with Linked Lists / structures.
String.s in Structures holds always the Pointer returned by
Global(Re)Alloc_() and if you do a DeleteElement(), the
memory gets freed with GlobalFree_().

The size of the string: Len(A$) is internally a simple
GlobalSize_(v_string_A) - 1 (-1 because of the ending NULL).

Hope Fred can change all that for v4.0, so all the string
limits go away.
A good and complete string system isnt very easy to do,
but all other compilers can do that too - so...

At this time especially the 64kb limit can produce very
bad results, because your app just crashes when you make
a string longer than 64k.

Let us hope the year 2003 will change that... :)

cya,
...Danilo

(registered PureBasic user)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Tomio.

ok, I learned the simple assignment I was looking for, is not possible.

Danilo, thanks for the detailed explanation. I'll study it carefully.

And thanks to all contributers! I was not expecting a discussion like this!

Happy new 2003 ../tomio
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by El_Choni.

Danilo: yes, that seems more useful, and is different from just having a long value stored in *c. Now, what if you change *c.s value to point to another string? Would A$ change to that string, or the dependancy between A$ and *c.s would end that way?

El_Choni
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fred.


Ok, I will try to clarify the pointer things. First, pointers are reserved to advanced users, which already know how work the memory allocation/management. A pointer to a string is NOT like a variables, because it won't allocate or free the string. *c.s = @a$ just refer the a$ address. Actually, a pointer to a string with *c.s isn't very useful. But something like:

Code: Select all

Structure Byte
  b.b
EndStructure

*c.Byte = @a$
is much more interresting, as you can read the string char by char directly from the memory.

Fred - AlphaSND
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Danilo.

> yes, that seems more useful, and is different from just having a long value stored in *c.
> Now, what if you change *c.s value to point to another string?
> Would A$ change to that string, or the dependancy between A$ and *c.s would end that way?

Because *c.s would be a Pointer to a string, it would point
to the new string pointer.

*c.s = @A$ ; pointer_c points to v_string_A
*c = @B$ ; pointer_c points now to v_string_B

The strings A$ and B$ itself are not changed here,
only a pointer.

cya,
...Danilo
(registered PureBasic user)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Tomio.
Originally posted by fred


Ok, I will try to clarify the pointer things. First, pointers are reserved to advanced users, which already know how work the memory allocation/management. A pointer to a string is NOT like a variables, because it won't allocate or free the string. *c.s = @a$ just refer the a$ address. Actually, a pointer to a string with *c.s isn't very useful. But something like:

Code: Select all

Structure Byte
  b.b
EndStructure

*c.Byte = @a$
is much more interresting, as you can read the string char by char directly from the memory.

Fred - AlphaSND
*c.s would point to characters, so *c.s+5 would point the the 5. character.
*a.w would point to words, so *a.w+5 would point the the 5. word.
And things like FindString(*c.s,"b",1) would be possible in a natural way.
It would be similar the declaration in C: char *c; int *a

But to say it clear: this is not my wishlist! It was only to understand PB's behavior as stated in my mail at the beginning.

../tomio
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Danilo.

>And things like FindString(*c.s,"b",1) would be possible in a natural way.

Code: Select all

*c.s = "Hello World"x = FindString(*c.s,"lo",1)
If x
  MessageRequester("INFO","Found string 'lo' at position "+StrU(x,2),0)
EndIf
Seems already possible.

cya,
...Danilo

(registered PureBasic user)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Tomio.
Originally posted by Danilo

>And things like FindString(*c.s,"b",1) would be possible in a natural way.

Code: Select all

*c.s = "Hello World"x = FindString(*c.s,"lo",1)
If x
  MessageRequester("INFO","Found string 'lo' at position "+StrU(x,2),0)
EndIf
Seems already possible.

cya,
...Danilo

(registered PureBasic user)
..yes, but it's like doing: FindString("Hello World","lo",1)

because as said above:

*c.s=@a$
...
a$="Hello World"
...
FindString(*c.s,"lo",1)

...does not as you would expect.
.../tomio
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Danilo.

Happy new year :)

...Dan

(registered PureBasic user)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by El_Choni.

Yes, happy new year. And sorry for all the confusion I may have created (I don't understand pointers any more either :wink:

El_Choni
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by horst.

I think there is a big confusion about the asterisk (*)
As far as I can see, this has absolutely no functionality.

It is only meant for you personal use to remind you that your
variable is used as a pointer.

Instead of *blah you can also use blah_pointer or anything else.

Since pointers are always 32bit, it is confusing to have a
variable *BLAH.s, because this means:

*BLAH is a string, but remember: it is a 32 bit pointer.

That's nonsence, because a variable is either a string or
a 32bit value, not both at the same time.

A pointer is produced by a function (where "@variable"
can be regarded as a function), and not by the variable
name that receives the result.
Examples:

buffer_pointer = AllocateMemory(0,1024,0)
title_pointer = @title
element_pointer = @Mylist()

To store the result in a variable with an asterisk is
optional and just a matter of taste. Only make sure that the
pointer variable is of type long (32bit).

Note that a 32bit variable can be used either as signed integer,
as absolute integer (like memory addresses), or as bit string.
It all depends what operations you do with the variable.

BTW: The internal storage of strings ia a different issue.



Horst
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Pupil.

Sorry about this Horst, but you've got it all wrong the '*' character before pointers is NOT optional but a MUST if you want proper pointers, otherwise you've only got address locations. Test the following code and see yourself:

Code: Select all

Structure CharType
  Char.b
EndStructure

string.s = "Hello world"

a.CharType = @string
*b.CharType = @string

Debug Chr(a\Char)
Debug Chr(*b\Char)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fred.


You don't get it, if I remove the * from purebasic, you can no more use pointer associated with structure.

a.POINT

is not the same at all than:

*a.POINT

Happy new year !

Fred - AlphaSND
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Tomio.

Horst,

sorry, but your point of view of pointers is wrong.

> As far as I can see, this has absolutely no functionality.

Concerning BASIC you are right, because (original) BASIC doesn't know about pointers. But it's a basic building block of C and other modern languages.
Using pointers you can do programming you could carry out only very circuitously else.

> *BLAH is a string, but remember...

No, *BLAH is not a string but a variable, may be the first element of a string(variable).

If a is avariable, &a is the pointer to that variable a.
If b holds a valid address (of addressspace) than *b is the variable at that address.
While pointer @a is an addressconstant and cannot be modified, b may run through the complete memory (print *b: b+1) (for demonstration only).

There are many more things to know about pointers but I only wanted to oppose the statement "... has absolutely no functionality"

../tomio
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by horst.

Pupil, Fred, Tomio:

I am begining to understand:
There is a special feature used to put a structure
over any data (with a given address).

Code: Select all

Structure blah
  char.b[10]
EndStructure

string.s = "Hello world"
*b.blah = @string

For i = 0 To 10
  Debug Chr(*b\char[i])
Next 
*b.blah is a pointer (32bit) that points to the string data,
but it is associated to the blah structure, so you can use it
like the name of a structured variable (variable.bla).

Maybe we should point out that this is a special structure mapping
feature using pointers. And this requires the asterisk.

If you don't use a pointer for this purpose, the asterisk
is not required. Is that right???

Code: Select all

string.s = "Hello world"
pointer = @string

For i = 0 To 10
  Debug Chr(PeekB(pointer+i))
Next 

Horst
Post Reply