• pointer...

    From Scott Adams@1:112/91 to All on Monday, January 26, 2004 00:35:22
    Here is a quickie demo of Pointers. I did it in a few minutes.
    So its not very elaborate. Keep in mind for much more detailed
    examples SWAG has 43 example codes and demo programs just to
    be examined. For SWAG a link can be found on my programming
    page links section @ http://users.cybermax.net/~longshot


    Program PTDem;
    Uses Crt, Dos;

    type StrArray = Array[1..5000] of String[70];


    A basic, simple program. Let's try to create a large string array.
    In this case however, the above will not work. It clearly
    is too large (5000x70 = 350000 bytes when borland Dos IDE only
    supports 64k for its Dos Platform programs). So this will not
    compile. We have to correct the size to a more manageable size.

    Program PTDem;
    Uses Crt, Dos;

    type StrArray = Array[1..800] of String[70];

    Var ICnt : Integer;
    SAR : StrArray;

    For ICnt := 1 to 800 do
    SAR[ICnt] := Chr(Random(65)+1);

    The StrArray is now 800 elements but still length of 70. This
    is much smaller but compiles to 56K of data. I create a simple
    counter variable and then a new variable SAR. This StringArrayRecord
    variable is of same type as Strarray. This is so we can access and
    store/use the data. But if you compile the basic Type is only 688
    bytes but with this new SAR variable the data size goes to 57490
    which is still in the 64k limit but huge!

    The For loop simply stores a random character in the SAR record
    array (all 800 items). Then writes it to screen. Now this
    will work but its still alot of space taken up.

    BTW, I forgot we should put a RANDOMIZE; at the top before the
    loop to make it truely random (I forgot).


    Now comes the magic of pointers.

    Program PTDem;
    Uses Crt, Dos;

    type StrArray = Array[1..800] of String[70];
    Var Str_Pt : ^StrArray;
    ICnt : Integer;
    { SAR : StrArray;}

    For ICnt := 1 to 800 do
    Str_Pt^[ICnt] := Chr(Random(65)+1);

    1. Notice I commented out the SAR variable freeing up 57k of data.
    2. The new part is Str_Pt which is a variable of pointer type.
    This Str_Pt POINTS to StrArray. The Use of the ^ shows it
    where its pointing to. Thus an arrow..pointer..get it?
    3. When you compile this it saves and goes down to 694 bytes of data
    space a BIG difference.
    4. A new command New(Str_Pt); Is a constructor in OOP programming.
    It Creates and reserves space for the pointer on the heap.
    5. Same for Dispose at the bottom. This is a Destructor in OOP
    jargon and clears the reservation of that spot so other programs
    can use that memory space.
    6. Now to use the pointers:
    Str_Pt^[ICnt] := Chr(Random(65)+1);

    The only difference here is we added a ^ to the end of the
    variable. This means we are Storing data using the pointer
    Str_PT. Then we read the same data using the ^ at the end as well
    meaning we are retrieving info from the pointer.

    It may be confusing as to where to put the ^ at the end or start.
    Just remember for data access, writes, reads use it at the end.
    For making varibles use it at the start so its pointing to some
    variable. This is much clearer to read than it is in C/C++ language
    and if you get in in pascal it helps alot.

    So what does the above really do? Well it frees up that huge chunk
    of ACTIVE memory to be stored Dynamically. This means the memory
    for the data is stored in Ram be it EMS, XMS or the space between
    the 64k limit and 1 meg of memory some old computers had :)

    This is houw the 64k memory barrier is broken. But this doesn't
    mean you have infinite space for data. It just meams you can
    store as much data as you have in memory. Thus a good system
    with 256 Megs of Ram could store a great, great deal of data.
    But the larger you get the more management you have to do on the

    Management of the data is done through commands like GetMem (a
    popular command used in simple graphics like PutImage for
    example) which simply gets the amount of space needed for some
    data object. FreeMem, AllocMem and other Heap commands also
    become critical for large programs.

    Pointers also speed up processing since its in memory rather
    than on a physical data space which may take time. This
    is another advantage of pointers.

    The Heap is basically that area of memory that is used by
    programs to Stack data (thus the Stack Size you see in your
    compile screens).

    Each function or procedure you do has a limited Stack space
    of data they can substore data in. But all this can be
    managed via pascal commands or the IDE itself. You can
    set lower or max heap and stack sizes to use.

    It is CRITICAL that you use a 1:1 association of New/Dispose.
    If you have 8 Pointers you should have 8 News and 8 Disposes.
    If not this will cause memory leaks. Thus when you exit
    your program the data of the pointer will still be reserved
    and not released so new programs you run can't use it. This
    we have all seen sometimes through the evil Windows Blue
    screen of death in some errors.

    Does this help or confuse folks? I could explain more if needed.
    Gotta run for now..

    --- Fringe BBS
    * Origin: EWOG II - The Fringe - 904-733-1721 (1:112/91)