[fpc-pascal] pointer arithmetic help required

Martin lazarus at mfriebe.de
Thu Oct 15 15:03:39 CEST 2009


Graeme Geldenhuys wrote:
> _pContentsData = a generic Pointer type that points to a block of data
> (containing multiples of various structures). XXX bytes of
> information.
>
> pEntry = this is a pointer to a specific starting structure type and
> will increment in blocks of data to point to the various structures
> stored in _pContentsData. Each pEntry has a Length property that says
> how many bytes of data is used for all it's information.
>   
if pentry is
 TEntry = record .. end;
 pentry = ^ TEntry
> pEnd = the ending point of all the data held by _pContentsData. This
> variable is just a safety net, so pEntry doesn't move past the end of
> data block by accident.
>
> I have the following code...
>
> ------------------------
>   pEntry := _pContentsData;
>      // below toclen is in bytes
>   pEnd := _pContentsData + _pHeader^.toclen;
>   
is toclen, in bytes, or in units (eg number of records)

the above assumes bytes, if

_pContentsData = pointer; //not typed


> // START LOOP
>   [(a)...do various things with data pointed to by pEntry..]
>   ....
>
>   // now increment pEntry to point to next data structure
>   // below, .Length is in bytes
>   inc( pEntry, pEntry^.Length);
>   
inc(PEntry, 1)

since it increments by the size of the type

>   // now repeat from (a) above until all data is processed
> // END LOOP
> ------------------------
>
>
> Question:
> Am I incrementing the pEnd and pEntry in the correct way? They are
> pointers to structures, so I believe I don't need to dereference them,
> because I am working with the pointer at that stage and not the actual
> data.
> Example:
>
>   inc(pEntry, pEntry^.Length);
> vs
>   inc(pEntry, pEntry^.Length * sizeof(byte));
>
>
> Luckily the above method is not the only way to read the data I am
> interrested in. There is also a redundant array of file offsets
> pointing to each pEntry I am interrested in. Using the array works
> fine, but using the incrementing method shown below, I am having
> problems. :-(
> Purely for the purpose of learning I would like to be able to get both
> methods working - how else am I supposed to learn these things. :)
>
> [I think I need to dig up my old college handbooks and relearn the
> basics of pointers and record structures with binary file data]
>
>   




More information about the fpc-pascal mailing list