Recent

Author Topic: Can someone please explain pointers..  (Read 34276 times)

captian jaster

  • Guest
Can someone please explain pointers..
« on: May 03, 2010, 08:49:24 pm »
Do they hold data stored in another variable or what? cuz i dont get it.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9791
  • Debugger - SynEdit - and more
    • wiki
Re: Can someone please explain pointers..
« Reply #1 on: May 03, 2010, 09:25:24 pm »
All you variables must be stored somewhere in the memory of your computer.

From the point of your application, that can be  for example on the stack, or the heap (but both are part of the overall memory of your computer)

Each place in the memory has an address. So each variable you have is located at some address.

A Pointer stores the address of another variable. It does not store the data of this variable, only where in the memory it can be found.

var
  a: Integer;
  pa: ^Integer; //Pinteger / pointer to integer

pa := @a;

pa contains the address of "a" in memory.

But if "a" moves to another address, or is entirely removed (yes it can happen), then pa doesn't know that. pa still has the address where "a" used to be.
if you do access this address, you may cause an error. (an immediate error, or an error at any later time)

As long as "a" is still in place:
pa^

returns the data which is stored in "a"

"^" is the dereference operator. It means you are not interested in the address stored in pa, but in the content of the memory pointed to by pa.

var
  a,b: Integer;
  pa, pb: ^Integer; //Pinteger / pointer to integer

a:= b;
pa := @a;
pb := @b;

if a=b then foo();
if pa=pb then bar();

foo will be executed, because a = b.

but bar will not be executed, because the addresses in the pointers are different (address of a of equal to address of b)

mas steindorff

  • Hero Member
  • *****
  • Posts: 532
Re: Can someone please explain pointers..
« Reply #2 on: May 03, 2010, 09:38:47 pm »
A pointer is just points to the data.  A pointer to a byte and a pointer to complex data record are the same size. there are some (OS and platform) rules as to what you can point to kinda like what phone number you can have.

Just like phone numbers, the end result is not the number but who is on the other end.

The size of a pointer depends on the OS.
When I was young, all I needed to call my friend was the last 4 digit in my small town (in USA). I had to use 7 to call another town in the same state and 10 numbers (+1) to call out of state.  Today, the population is so large and my calling location (area code) can change that I always use the the 10 digit even if the 7 digit option is possible.  The same thing has happened to pointers so they can reach the any location without special handling.
some times it is the special handing that confuses people, kinda like when you call out of country but that sort handling is another topic  >:D

for more see wikipe..

http://en.wikipedia.org/wiki/Pointers
windows 10 &11, Ubuntu 21+ - fpc 3.0.4, IDE 2.0 general releases

anna

  • Sr. Member
  • ****
  • Posts: 426
Re: Can someone please explain pointers..
« Reply #3 on: May 04, 2010, 12:13:36 pm »
......
var
  a: Integer;
  pa: ^Integer; //Pinteger / pointer to integer

pa := @a;

pa contains the address of "a" in memory.

....


Wait for minute. pa variable contains pointer, not address. Address is got by calling Ofs(a);

I agree that Pointers are very stupid device. For example I take 64 bytes of memory by
Code: [Select]
GetMem( pData , 64 );Now pData is a pointer on first byte of 64 . How to write some data in e.g. 32nd byte? Mystery, huh?
« Last Edit: May 04, 2010, 12:15:19 pm by anna »
WinXP SP3 Pro Russian 32-bit (5.1.2600)

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Can someone please explain pointers..
« Reply #4 on: May 04, 2010, 01:09:02 pm »
This is how i do it:
Code: [Select]
var
  data: PByte; // Pointer to dynamic byte-array
  i: integer;
begin
  data:=AllocMem(64); // reserve 64 bytes
  for i:=0 to 63 do
    data[i] := i; // Set data in dynamic array
  writeln(inttostr(data[32])); // prints out "32"
  FreeMem(data); // Free all (64) bytes
end;

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9791
  • Debugger - SynEdit - and more
    • wiki
Re: Can someone please explain pointers..
« Reply #5 on: May 04, 2010, 01:16:02 pm »
pa := @a;

pa contains the address of "a" in memory.
Wait for minute. pa variable contains pointer, not address. Address is got by calling Ofs(a);
"@" returns the address of a variable.

so pa contains the address. all pointers contain an address (or nil)


I agree that Pointers are very stupid device. For example I take 64 bytes of memory by
Code: [Select]
GetMem( pData , 64 );Now pData is a pointer on first byte of 64 . How to write some data in e.g. 32nd byte? Mystery, huh?


Ok I don't have a full tutorial for pointers at hand, I am sure somewhere there will be one.

the answer is
- for untyped pointer
(PByte(pdata) + 32)^

- for typed pointer (pointer to byte)
(pdata + 32)^

Which makes sense, if you accept that pdata contains the address of the first byte in the memory block.

as for pointer arithmetic, it depends on the type of pointer:
 (somepointer + 32)
does not mean 32 BYTES after somepointer. But it does mean 32 times the size of the element identified by the type of the pointer.

  

Troodon

  • Sr. Member
  • ****
  • Posts: 484
Re: Can someone please explain pointers..
« Reply #6 on: May 04, 2010, 01:40:55 pm »
Pointers and recursion are the greatest features in Pascal. Without pointers there would be no graphs, the most complex data structures. Without recursion some algorithms would be impossible to code. I could manage without OOP but not without those two features... Just kidding, OOP is awesome.

A pointer is a memory address. A pointer variable of type ^MyType is the beginning address of the memory segment where a value of type MyType is stored.

Using pointer variables ("pointers", for short) is simple:

- declare a pointer variable IntPtr that stores an Integer value:

Code: [Select]
type
  PInteger = ^Integer;

var
  IntPtr: PInteger;  // at this stage it is only an address


- create it:

Code: [Select]
IntPtr := New(PInteger);  // allocate memory for it
- assign a value to it:

Code: [Select]
IntPtr^ := 100;
- retrieve its value:

Code: [Select]
N := IntPtr^;
- destroy it:

Code: [Select]
Dispose(IntPtr);  // release memory allocated to it
Extras (you will seldom need these):

Code: [Select]
addr := @(IntPtr);  // memory address where it begins
size := SizeOf(Integer);  // size of allocated memory segment
N := Integer(IntPtr);  // value retrieved by type casting

To make things more interesting you could choose a pointer to a record type, ^MyRecord, instead of ^Integer. And obviously, MyRecord can include additional pointer variable fields along with static variable fields. That is how linked lists (stacks, queues), trees, and graphs are generated.

In particular, an object is an instance of a pointer variable. IMO, one of the confusing changes in Delphi (as compared to TP) was not making the pointer nature of the stock objects more explicit.
« Last Edit: May 04, 2010, 02:57:27 pm by Troodon »
Lazarus/FPC on Linux

anna

  • Sr. Member
  • ****
  • Posts: 426
Re: Can someone please explain pointers..
« Reply #7 on: May 04, 2010, 03:14:39 pm »

the answer is
- for untyped pointer
(PByte(pdata) + 32)^

- for typed pointer (pointer to byte)
(pdata + 32)^

Which makes sense, if you accept that pdata contains the address of the first byte in the memory block.

as for pointer arithmetic, it depends on the type of pointer:
 (somepointer + 32)
does not mean 32 BYTES after somepointer. But it does mean 32 times the size of the element identified by the type of the pointer.

 


Over the pointers are not defined any operations other than testing equality and inequality.
Adding the integer will cause error of incompatibility types.

Pointers are not addresses because RAM address is dword value from $00000000 to $FFFFFFFF.

Pointers are pointers. It's individual type. Pascal doesn't have any other mechanism to write data to memory except through using pointers. I think would be better if pointer was integer value.
« Last Edit: May 04, 2010, 03:20:57 pm by anna »
WinXP SP3 Pro Russian 32-bit (5.1.2600)

captian jaster

  • Guest
Re: Can someone please explain pointers..
« Reply #8 on: May 04, 2010, 03:40:11 pm »
OH.....OK thats cool. ima try to find some examples...
This is how i do it:
Code: [Select]
var
  data: PByte; // Pointer to dynamic byte-array
  i: integer;
begin
  data:=AllocMem(64); // reserve 64 bytes
  for i:=0 to 63 do
    data[i] := i; // Set data in dynamic array
  writeln(inttostr(data[32])); // prints out "32"
  FreeMem(data); // Free all (64) bytes
end;
Thats cool. i didnt know i could reserve memory.. were did you learn that?

Thanks guys!

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9791
  • Debugger - SynEdit - and more
    • wiki
Re: Can someone please explain pointers..
« Reply #9 on: May 04, 2010, 03:41:23 pm »
- for untyped pointer
(PByte(pdata) + 32)^

- for typed pointer (pointer to byte)
(pdata + 32)^

Which makes sense, if you accept that pdata contains the address of the first byte in the memory block.

Over the pointers are not defined any operations other than testing equality and inequality.
Adding the integer will cause error of incompatibility types.

Pointers are not addresses because RAM address is dword value from $00000000 to $FFFFFFFF.

Pointer arithmetics are definitely defined and allowed.
You can add to a *typed* pointer (or an untyped pointer after casting it to a typed pointer)

The fact that memory on a modern PC is mapped, and each program is made to see it's own address-space does not oppose the fact that the data of the program is located at addresses (in this address space). So I stand by it: A pointer contains an address.

Troodon

  • Sr. Member
  • ****
  • Posts: 484
Re: Can someone please explain pointers..
« Reply #10 on: May 04, 2010, 05:56:28 pm »
Pointers are not addresses

You are technically right, anna. And it's like Martin said. Therefore, I reword:

"A pointer is a memory address." -> "A pointer holds the address at which data of the type type_identifier is situated." (cf. GNU Pascal docs)

Is there a difference?
« Last Edit: May 04, 2010, 10:18:54 pm by Troodon »
Lazarus/FPC on Linux

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4459
  • I like bugs.
Re: Can someone please explain pointers..
« Reply #11 on: May 04, 2010, 10:05:25 pm »
Pointers are not addresses because RAM address is dword value from $00000000 to $FFFFFFFF.

What does that mean? Pointers are very much addresses! Yes.
I think every programmer should learn some assembly, or at least C, to understand what is actually going on.
This is a big problem with modern languages which have more and more abstraction from the actual processor level. If a programmer doesn't know how the code translates into low-level binary then he/she can make inefficient programs.

Juha
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

Troodon

  • Sr. Member
  • ****
  • Posts: 484
Re: Can someone please explain pointers..
« Reply #12 on: May 04, 2010, 10:24:02 pm »
This is a big problem with modern languages which have more and more abstraction from the actual processor level.

IMO that is actually a good thing ;) Another problem is, sometimes beginners try to edit memory directly instead of assigning values to variables. It's a bit like a child tinkering with the motherboard jumpers to learn computer programming.
Lazarus/FPC on Linux

Troodon

  • Sr. Member
  • ****
  • Posts: 484
Re: Can someone please explain pointers..
« Reply #13 on: June 11, 2010, 09:54:40 pm »
I have decided to learn C# just for fun and to see what all the fuss is about. Jeez, can they beat around the bush when trying to explain the difference between value and reference, or between class and struct! And to make things more complicated explanations also include the vague concepts of instance and object, while desperately trying to avoid any... reference to pointers. It's actually quite simple:

ObjectPascal    C#    comment
------------    ---    ---------
class    class*    object type    
object/instance    object/instance    instance of class =  the actual dynamic variable
value    value    content of a variable
pointer   reference    (variable containing) the address of a value

*To maintain an even stress on the developer, the C# type struct is similar but not identical to type class, it has methods but the constructor cannot be inherited, and it's a type for static not dynamic variables.

In C# the following types are reference types:

    - arrays
    - class
    - delegates
    - interfaces

And the following types are value types:

    - enum
    - struct

Obviously, "reference type" means dynamic variable and "value type" means static variable, in ObjectPascal. But adding the word "type" after "value" only creates confusion as "type" is an abstract concept while "value" involves an instance.

A few more reasons to like ObjectPascal ;)
« Last Edit: June 11, 2010, 10:04:13 pm by Troodon »
Lazarus/FPC on Linux

anna

  • Sr. Member
  • ****
  • Posts: 426
Re: Can someone please explain pointers..
« Reply #14 on: November 09, 2011, 04:08:42 am »
Hello again.
I have some errors with SHFileOperation windows function.
Code: [Select]
uses windows;
var
    SH: TSHFileOpStruct;
    shpFrom_C67,shpTo_C67:pointer;
    path0,path1:string;

begin
      sh.hwnd:=0;
      sh.wFunc:=2;
      GetMem( sh.pFrom, $200  );FillChar(sh.pFrom^, $200, 0);
      GetMem( sh.pTo  , $200  );FillChar(sh.pTo^  , $200, 0);
      sh.pFrom:=pchar(Utf8ToAnsi(path0));
      sh.pTo:=pchar(Utf8ToAnsi(path1));
      sh.fFlags:=$251;
      sh.fAnyOperationsAborted:=longbool(0);
      sh.hNameMappings:=nil;
      sh.lpszProgressTitle:=nil;
      SHFileOperation(sh);
      system.assign(f2,Utf8ToAnsi(path0));
    end;           
Problem is that I must make pFrom & pToare  double-null terminated. How to do that? When I debugging I see that
before line      sh.pFrom:=pchar(Utf8ToAnsi(path0));
pFrom has an one address, and afrer line      sh.pFrom:=pchar(Utf8ToAnsi(path0));
pFrom has another memory address (and of course new-address^-data is NOT double-null terminated). Why? Is there a way to write some data into sh.pFrom^ whithout for-loop and whithout address changing.

I need that pFrom's GetMem address will be kept because I have $200 null-characters on that address. So even if first bytes will be some path, path automatically will be double(actually more then double, but [$200-length(path)>=$100]) null-terminated. Such solution as
Code: [Select]
for i:=1 to length(Utf8ToAnsi(path0)) do
 sh.pFrom^[i-1]:=Utf8ToAnsi(path0)[i];
Such solution as above is stupid!!!


I got added evidence that I was right, it's much better  if there would be some easy mechanism convert ''pointer-->address'' and ''pointer<--address''.
« Last Edit: November 09, 2011, 04:32:07 am by anna »
WinXP SP3 Pro Russian 32-bit (5.1.2600)

 

TinyPortal © 2005-2018