Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A critique - pointer arithmetic

Status
Not open for further replies.

Glenn9999

Programmer
Jun 19, 2004
2,311
US
Does anyone have any suggestions for this program?

It is meant to demonstrate:
1) Accessing an arbitrary block of storage.
2) Pointer arithmetic.

Code:
program sample10;
  { pointer arithmetic & arbitrary storage }
  var
    mem_ptr: pointer;
    mem_ptr_proc: pointer;
    array_size: word;
    mem_ptr_arith: ^longint;
    begin_of_mem, end_of_mem, index_counter: longint;
    proc_counter: integer;

  begin
    { Establish addressability for pointer arithmetic with our proc index }
    mem_ptr_arith := @mem_ptr_proc;
    { now ask for memory size and get it. }
    write('Enter size of integer array to be allocated: ');
    readln(array_size);
    getmem(mem_ptr, array_size*sizeof(integer));
    { set our pointers to access memory based on getmem }
    mem_ptr_proc := mem_ptr;
    begin_of_mem := mem_ptr_arith^;
    end_of_mem := mem_ptr_arith^ + array_size * sizeof(integer);
    writeln('Memory address for arithmetic is: ', begin_of_mem);
    writeln('End of memory is: ', end_of_mem);
    { process against this memory - from 1 to array_size }
    index_counter := begin_of_mem;
    proc_counter := 1;
    while (index_counter <= end_of_mem) do
      begin
        mem_ptr_arith^ := index_counter;
        move(proc_counter, mem_ptr_proc^, sizeof(proc_counter));
        inc(index_counter, sizeof(integer));
        inc(proc_counter, 1);
      end;
    { now write the memory back }
    index_counter := begin_of_mem;
    while (index_counter < end_of_mem) do
      begin
        mem_ptr_arith^ := index_counter;
        writeln(integer(mem_ptr_proc^));
        inc(index_counter, sizeof(integer));
      end;
    { midpoint of memory -100 is entered, 50 is returned }
    index_counter := end_of_mem - begin_of_mem;
    index_counter := (index_counter div 2) - sizeof(integer);
    index_counter := begin_of_mem + index_counter;
    mem_ptr_arith^ := index_counter;
    writeln('Midpoint is: ', integer(mem_ptr_proc^));
    { release the memory }
    freemem(mem_ptr, array_size * sizeof(integer));
  end.
 
(1) mem_ptr_arith is a pointer pointing to another pointer, but thinking it points to a long int. You could (if you want) just typecast the original pointer when you want to do arithmetic on it.
example:
getmem(aptr, somesize);
bptr := aptr;
inc(word(bptr), ThingSize);

I find the typecast approach clearer and more economical on variables, but it's a matter of taste. If you do this, the only down-side is you have to be careful about distinguishing
inc(word(bptr), ThingSize); {move pointer}
from
inc(word(bptr^), Something); {process word-size data addressed by bptr}

(2) Are you in 16-bit pascal? I'm assuming so from the general appearance of the code. If so, you probably shouldn't treat pointers as longints (which are 4-bytes). It won't usually matter, but an overflow from the low word to the high word will be incorrect, because the high word is counting in units of 16, not units of $FFFF. In your case, it means there isn't much meaning to the absolute values of begin and end of memory, but they will differ by the correct amount.
If you're in 32bit world, please ignore me.

(3) OK, OK, it's a test program, so I won't moan about lack of error checking on your readln!

I didn't read in detail... Have fun!

 
Suggestions noted...and yes this is 16-bit Pascal. I would have rather had a Dword type to manipulate, but Turbo Pascal doesn't offer such a creature...

The basic point of the program was to try and prove the two points I had listed above...my main concern of it is whether I did sufficiently, and in a correct way (it's part of a bigger project).
 
I'm basically done with the work that I had this program in. Does anyone know of where something like this could see the light of day? Not necessarily in a forum but something more enduring?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top