Page 1 of 2
Clarification on Pointers...
Posted: Sun Jun 11, 2006 7:08 pm
by Kale
Just to get it straight in my head i need a little clarifcation on pointers. After reading this thread:
http://www.purebasic.fr/english/viewtop ... 8820#48820
Fred is basically saying that a pointer is a different type in itself. I had always thought pointers where nothing more than long type variables. I guess i was wrong.
So how do you define pointers? like this?:
i guess this would create one but notice there isn't a type declared. What if i created a pointer like this:
does this mean that i now have a long type variable with a fancy name instead of a dynamically resizing pointer?
What if i have this code:
Is this a proper pointer or another long type variable with a fancy name?
Posted: Sun Jun 11, 2006 7:36 pm
by Fred
A pointer isn't a always a 'long' because it needs to have the room store an absolute address. On 32 bits processors, the address space is limited to 32 bits, so a pointer takes 32bits (4 bytes, like a 'long') in memory. Now on 64 bits processors, it takes 64bits (8 bytes, like a 'quad') in memory, because an absolute address is on a 64bits range.
The type you affect to pointer is a different thing. For example: *Pointer.l, *Pointer.b or *Pointer.q in PB will makes no difference at all as the type l, b and q doesn't have any fields in them. Pointer makes sens mainly with a structure.
Posted: Sun Jun 11, 2006 7:43 pm
by Kale
Fred wrote:A pointer isn't a always a 'long' because it needs to have the room store an absolute address. On 32 bits processors, the address space is limited to 32 bits, so a pointer takes 32bits (4 bytes, like a 'long') in memory. Now on 64 bits processors, it takes 64bits (8 bytes, like a 'quad') in memory, because an absolute address is on a 64bits range.
The type you affect to pointer is a different thing. For example: *Pointer.l, *Pointer.b or *Pointer.q in PB will makes no difference at all as the type l, b and q doesn't have any fields in them. Pointer makes sens mainly with a structure.
So if i used code like this to create a pointer:
Would this give me a long type variable or a special resizing pointer that matches the address space?
Posted: Sun Jun 11, 2006 7:57 pm
by Konne
I guess it gives u a pointer to a long variable... Or it should be the same then *Pointer.Long . But I dunno I'm just guessing. And Maybe u could come to the Long by writetinh *Pointer\Long.... Maybe
Posted: Sun Jun 11, 2006 10:07 pm
by Nik
I think like fred says the .l doesn't change anything as normal types aren't the same as their structure fore e.g. *Pinter.LONG in Purebasic. So it's always just the same as a char* Pointer; in c/c++. If you use a structure thought it's a different thing eg *Pointer.MyStruct equals Mystruct* Pointer; in c/c++
Posted: Sun Jun 11, 2006 10:35 pm
by Fred
A pointer is always the same size (which is processor dependent), whatever the type (structure or not) you put after. A pointer is just an easy way to access a structured memory area. And yes, *Pointer.l and *Pointer.Long is the same, except than with the second one you can really read the long at the specified address, due to the 'l' structure field.
Posted: Sun Jun 11, 2006 10:54 pm
by Nik
That's what I meant sorry if it wasn't clear that I didn't mean the size but tthe usage.
Posted: Sun Jun 11, 2006 11:16 pm
by Kale
Fred wrote:A pointer is always the same size (which is processor dependent), whatever the type (structure or not) you put after. A pointer is just an easy way to access a structured memory area. And yes, *Pointer.l and *Pointer.Long is the same, except than with the second one you can really read the long at the specified address, due to the 'l' structure field.
Right i think i get it now!

BUT when you say that 'A pointer is always the same size (which is processor dependent), whatever the type (structure or not) you put after'. can be proved with this piece of code:
Code: Select all
test.l = 10
*pointer.b = @test
Debug *pointer
As you can see the pointer is defined as a byte but in reality, it holds a bigger value than that type can hold, so yes it is a long internally.
But this also throws up an anomally, look at this:
Code: Select all
*pointer.s = "Testing"
Debug *pointer
Here the '.s' string type does make a difference.
I think a note should be added to the docs to clarify that the type doesn't make a difference in pointer size, apart from the string type which does.
Posted: Mon Jun 12, 2006 4:23 pm
by Trond
No, the pointer is still the same size: 4 bytes. Those four bytes contains the address of the string.
Posted: Mon Jun 12, 2006 8:00 pm
by Kale
Trond wrote:No, the pointer is still the same size: 4 bytes. Those four bytes contains the address of the string.
Yep, i get that but the asterisk doesn't actually make any difference to the way the string is handled internally does it? because all string handling is done via pointers.
is the same as:

Posted: Mon Jun 12, 2006 8:44 pm
by netmaestro
I think you'd use a string pointer along these lines:
Code: Select all
*ptr.string = AllocateMemory(6)
*ptr\s = "hello"
Debug *ptr\s
But I don't think you gain much with this code as the string structure only has one element.
Posted: Mon Jun 12, 2006 10:54 pm
by Andre
To improve the manual I've added the additional informations (based on Freds postings) to the pointer chapter of the PB manual:
Pointers
To use a pointer, put * before the variable name. A pointer is normally a long variable which stores an address. It is generally associated with a structured type. So, you can access the structure via the pointer.
Example:
*MyScreen.Screen = OpenScreen(0,320,200,8,0)
mouseX = *MyScreen\MouseX ; Assuming than the Screen structure contains a MouseX field
There are only three valid methods to set the value of a pointer:
- Get the result from a function (as shown in the above example)
- Copy the value from another pointer
- Find the address of a variable, procedure or label (as shown below)
Note: Other than in C/C++ in PureBasic the * is always part of the variable name. Therefore *ptr and ptr are two different variables.
Some technical background about pointers in PureBasic:
When you declare a variable with an '*', it's a pointer, which means it will be always the size of the CPU address mode (4 bytes on 32 bits CPU and 8 bytes on 64 bits one for example). When you attach a structure to a pointer (for example *MyPoint.Point) it allows to access any memory address in a structured way.
Example:
Point1.Point
Point2.Point
*CurrentPoint.Point = @Point1
*CurrentPoint\x = 10 ; 10 is affected to Point1
*CurrentPoint.Point = @Point2
*CurrentPoint\x = 10 ; 10 is affected to Point2
So a pointer isn't always a 'long' because it needs to have the room to store an absolute address. On 32 bits processors the address space is limited to 32 bits, so a pointer takes 32 bits (4 bytes, like a 'long') in memory. On newer 64 bits processors it takes 64 bits (8 bytes, like a 'quad') in memory, because the absolute address is on a 64 bits range.
The type you affect to a pointer is a different thing. For example: *Pointer.l, *Pointer.b or *Pointer.q will make no difference at all as the types l, b and q doesn't have any fields in them. Pointer makes sense mainly with a structure.
A pointer has always the same size (which is processor dependent), whatever type (structure or not) is put after. A pointer is just an easy way to access a structured memory area. Also *Pointer.l and *Pointer.Long are the same, except than with the second one you can really read the long at the specified address, due to the 'l' structure field.
Posted: Mon Jun 12, 2006 11:05 pm
by netmaestro
On newer 64 bits processors it takes 64 bits
Just one question, do you have to be running a 64-bit OS to get a memory pointer 8 bytes in size? Because on my AMD Athlon64 processor, with *ptr=Allocatememory(1024), SizeOf(*ptr) is reporting 4. Would it be 8 if I were running, say, 64-bit Vista?
Posted: Mon Jun 12, 2006 11:57 pm
by Rescator
Nope! Not unless the program was compiled as a 64bit application.
As long as it's compiled as a 32bit application the system will only provide the program with a 32bit address space.
Posted: Tue Jun 13, 2006 12:33 am
by Konne
How can I compile for 64 Bit?