Beginner needs some pointers with pointers! 
Author Message
 Beginner needs some pointers with pointers!

How exactly do pointers work? I have a very basic understanding of what
they do, by I'm not sure how to properly use them.

  When do you use "new()" ?
  When do you use "getmem()"

  What's the difference between

P : ^TWhaterver;
  and
P : Pointer;

What happens if you have a pointer pointing to another pointer?

I'd really appreciate some help, or somewhere where I can find help?
Thanks



Wed, 18 Jun 1902 08:00:00 GMT  
 Beginner needs some pointers with pointers!

Quote:

> How exactly do pointers work? I have a very basic understanding of what
> they do, by I'm not sure how to properly use them.

>   When do you use "new()" ?
>   When do you use "getmem()"

>   What's the difference between

> P : ^TWhaterver;
>   and
> P : Pointer;

> What happens if you have a pointer pointing to another pointer?
> I'd really appreciate some help, or somewhere where I can find help?

http://www.geocities.com/SiliconValley/2926/tpf.html
page "FAQ", article "Turbo Pascal memory considerations, Pointer primer"
:-) Franz Glaser


Wed, 18 Jun 1902 08:00:00 GMT  
 Beginner needs some pointers with pointers!
Quote:
> How exactly do pointers work? I have a very basic understanding of what
> they do, by I'm not sure how to properly use them.
>   When do you use "new()" ?
>   When do you use "getmem()"
>   What's the difference between
> P : ^TWhaterver;

   Use New(P) for this, because the amount needed is known by the
compiler.

Quote:
>   and
> P : Pointer;

   Use GetMem(P,bytes_desired) for this, since the compiler hasn't been
told how much data it points to.

Quote:
> What happens if you have a pointer pointing to another pointer?
> I'd really appreciate some help, or somewhere where I can find help?

   See if this helps (it won't deal with your last question, though):

   Pointers are variables within a (Pascal) program which reference
(point to) data on the Heap.  The Heap is a (usually) large area of
computer memory which is not part of the program,
_but_can_be_used_by_the_program_.  Therefore, the amount of data your
program can use is more than is initially stated (declared) when the
program is built.  This is very useful in applications where the amount
of needed data is not known when the program is created, or for those
which require a dynamic amount of data.
   There are many ways pointers are used: (1) to create large variables
or structures; (2) to allocate/use a variable amount of data; (3) for
allocating/using more data than the program allows via directives; (4)
assigning large buffers; and many others.  The techniques for using
Heap (pointer-referenced) data are numerous: (1) individual data
pointers; (2) linked lists (single or double); (3) pointer arrays
(arrays of pointers).
   Note that a given technique (e.g. linked lists) is not always the
best way to use pointers - some techniques are more versatile than
others, while other are more dynamic.  For example, linked lists are
excellent for really dynamic data, but are very difficult to maneuver
(sort or reorder) beyond initial allocation.  Conversely, pointer arrays
must be of a fixed maximum size, but sorting and manipulation is quite
easy.  Single pointers cannot reference data variables/structures which
exceed certain implementation limits, which for Turbo Pascal (Borland)
is 64K bytes.
   Linked lists are often used for storing and accessing data which is
extremely dynamic to the program: data whose amounts or limits are
unknown until the program executes.  L/Ls are created in several forms:
linear; hierarchial; trees; etc.  The application best defines which
type is best.  The logic to insert/delete or reorder L/Ls is well known,
but it is rather complex.
   Pointer arrays are very useful for applications where a maximum
amount of data can be expressed, and normally all of available Heap
memory can be expressed for P/As, whether it's actually used is
application and execution/data dependent.  In TP/BP, it's possible to
P/As of up to 16,384 items, or even P/As of other 16K P/As - there
really is no limit other than available Heap limits.
   Some examples:

Type BUFF = array[0..8191] of char;              { large buffer type }
Var  B    : ^BUFF;                  { a pointer to large buffer type }
     F    : Text;
  Assign (F,'filename.dat'); New(B); SetTextBuf (F,B^); Reset (F);

   This example shows use of an 8K (large) Text file buffer.  The buffer
must be defined, and the pointer to it must be declared (the Var) and
allocated (the New(B)).  It is referenced by the SetTextBuf procedure.

Type REC = record                          { sample record structure }
             I : integer;                             { uses 2 bytes }
             S : string[120];                       { uses 121 bytes }
             R : real                                 { uses 4 bytes }
           end;
Var  PA1 : array[1..10000] of ^REC;  { pointer array for the records }
     J,K : Word;
  for J := 1 to 10000 do PA1[J] := Nil;   { initialize P/A to "null" }
  J := 0;                                     { initialize P/A index }
  while not EOF (F) do                    { load Heap with file data }
    begin      { will load up to 10,000 records, if Heap accommodates }
      Inc (J); New (PA1[I]);    { allocate Heap for next record data }
      with PA1[J]^ do readln (F,I,R,S)
    end;
  Close (F);                                       { close file file }
  for K := 1 to J do                   { display part of each record }
    with PA1[K]^ do writeln (I:8,R:9:2);
  for K := 1 to J do writeln (PA[K]^.S)             { another way... }

   Some things to understand about the above P/A code:
   - 10,000 stored records would consume 127,000 bytes of Heap storage,
which is much more than TP/BP would support as normal Var data.
   - the 10,000 pointers would use 40,000 bytes of the Data Segment, but
that can be reduced to 4 by making another type of the pointer array and
having a pointer to _it_.
   - combined, the pointers shown and the maximum data would consume
167,000 bytes, but the Data segment still supports about 25,000 bytes of
other data.  With a pointer type, 166,996 bytes would be on the Heap and
4 would be on the Data Segment.  This is where the trade offs start to
manifest themselves, as the 64K Data Segment limit can be overcome in a
variety of ways, through effect use of pointers.

   Linked Lists require more code and logic than I can show here (but I
assume someone will do so...).  I really prefer P/As for doing most of
my dynamic memory work - because it's easier than L/Ls and is much more
versatile.  Your mileage may vary, of course...  8<}}



Wed, 18 Jun 1902 08:00:00 GMT  
 Beginner needs some pointers with pointers!

Quote:

>How exactly do pointers work? I have a very basic understanding of what
>they do, by I'm not sure how to properly use them.

>  When do you use "new()" ?
>  When do you use "getmem()"

Use New() when you know the size of the allocation at compile time. That
is you allocate a specific target variable. Use getmem() when you know
it only on run time. Consider the following ways to allocate a pointer
to string and assign the target:

var p:^string;
    st:string;

new(p);
p^:=st;

getmem(p,length(st)+1);
p^:=st;

The first always allocates 256 bytes no matter how short the string is.
The second allocates only as much as is needed for the specific string.
That is with getmem() one can make the programs smarter.

Then thee is the stupid way to use getmem: getmem(p,sizeof(p^)); this
does same as new(p); but is much uglier.

Quote:

>  What's the difference between

>P : ^TWhaterver;
>  and
>P : Pointer;

The first p points to a variable of specific type and you can do
p^:=<variable of type Twhatever>. The latter is just an abstract pointer
that does not point to anything specific. For example if you write a
memory allocation function:

function alloc(x:word):pointer;
var p:pointer;
begin
  getmem(p,x);
  alloc:=p;
End;

You would not have to care for the specific type of the target variable
and you could assign the result to any pointer variable.

Quote:
>What happens if you have a pointer pointing to another pointer?

Nothing special. It just points to another pointer.

Quote:
>I'd really appreciate some help, or somewhere where I can find help?
>Thanks

Osmo


Wed, 18 Jun 1902 08:00:00 GMT  
 Beginner needs some pointers with pointers!

Quote:

>> How exactly do pointers work? I have a very basic understanding of what
>> they do, by I'm not sure how to properly use them.
>>   When do you use "new()" ?
>>   When do you use "getmem()"
>>   What's the difference between
>> P : ^TWhaterver;
>   Use New(P) for this, because the amount needed is known by the
>compiler.

Things are not that simple. Read my previous example of the string
allocation.

Quote:

>   Pointers are variables within a (Pascal) program which reference
>(point to) data on the Heap.

Or anywhere else in the memory. For those who are low level oriented,
pointer holds the address (segment:offset) of the variable that it
points.

Quote:
>   Note that a given technique (e.g. linked lists) is not always the
>best way to use pointers - some techniques are more versatile than
>others, while other are more dynamic.  For example, linked lists are
>excellent for really dynamic data, but are very difficult to maneuver
>(sort or reorder) beyond initial allocation.

Pardon? Linked lists are very easy to sort with mergesort:

Procedure SortList(var p:pnode);
var mp:pnode;
Begin
   if (p=nil) or (p^.next=nil) then exit;

   find the middle point and put it in mp, cut the list in
   half at the middle point;

   sortlist(p);
   sortlist(mp);

   Merge lists p and mp and put the result in p;
End;

If one passes also the length then it comes somewhat simpler and faster
to cut the list. When one uses lists one must think abut lists ands not
try to force ways learned from arrays to lists.

Osmo



Wed, 18 Jun 1902 08:00:00 GMT  
 Beginner needs some pointers with pointers!

Quote:
> >   Note that a given technique (e.g. linked lists) is not always the
> >best way to use pointers - some techniques are more versatile than
> >others, while other are more dynamic.  For example, linked lists are
> >excellent for really dynamic data, but are very difficult to maneuver
> >(sort or reorder) beyond initial allocation.

> Pardon? Linked lists are very easy to sort with mergesort:

> Procedure SortList(var p:pnode);
> var mp:pnode;
> Begin
>    if (p=nil) or (p^.next=nil) then exit;

>    find the middle point and put it in mp, cut the list in
>    half at the middle point;

>    sortlist(p);
>    sortlist(mp);

>    Merge lists p and mp and put the result in p;
> End;

> If one passes also the length then it comes somewhat simpler and faster
> to cut the list. When one uses lists one must think abut lists ands not
> try to force ways learned from arrays to lists.

   I consider your example above to be a _lot_ more work (code and
execution) than that required to sort array data - pointer array or
direct data.  So, we disagree.  I know which method I'd choose...


Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. pointers, pointers, pointers...

2. Pointers Pointers Pointers

3. Alias generation: Any pointers for a beginner ?

4. Pointers to pointers

5. POINTERS POINTERS...

6. need pointer to information please

7. need help on pointers

8. Need help, i want send pointer(^) to a unit

9. Need an idea for a Pointer Exercise

10. Needed: Code for array of pointers to procedures

11. need help on pointers

12. Need help with pointers...

 

 
Powered by phpBB® Forum Software