Clarification on Pointers...

Just starting out? Need help? Post your questions and find answers here.
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Clarification on Pointers...

Post 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?:

Code: Select all

*Pointer
i guess this would create one but notice there isn't a type declared. What if i created a pointer like this:

Code: Select all

*Pointer.l
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:

Code: Select all

Define.l
*Pointer
Is this a proper pointer or another long type variable with a fancy name?
--Kale

Image
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post 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.
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post 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:

Code: Select all

*Pointer.l
Would this give me a long type variable or a special resizing pointer that matches the address space?
--Kale

Image
Konne
Enthusiast
Enthusiast
Posts: 434
Joined: Thu May 12, 2005 9:15 pm

Post 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
Apart from that Mrs Lincoln, how was the show?
Nik
Addict
Addict
Posts: 1017
Joined: Fri May 13, 2005 11:45 pm
Location: Germany
Contact:

Post 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++
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post 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.
Nik
Addict
Addict
Posts: 1017
Joined: Fri May 13, 2005 11:45 pm
Location: Germany
Contact:

Post by Nik »

That's what I meant sorry if it wasn't clear that I didn't mean the size but tthe usage.
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post 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.
--Kale

Image
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

No, the pointer is still the same size: 4 bytes. Those four bytes contains the address of the string.
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post 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.

Code: Select all

String.s = "test"
is the same as:

Code: Select all

*String.s = "test"
:?:
--Kale

Image
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post 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.
BERESHEIT
User avatar
Andre
PureBasic Team
PureBasic Team
Posts: 2137
Joined: Fri Apr 25, 2003 6:14 pm
Location: Germany (Saxony, Deutscheinsiedel)
Contact:

Post 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.
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post 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?
BERESHEIT
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Post 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.
Konne
Enthusiast
Enthusiast
Posts: 434
Joined: Thu May 12, 2005 9:15 pm

Post by Konne »

How can I compile for 64 Bit?
Apart from that Mrs Lincoln, how was the show?
Post Reply