[fpc-pascal] pointer arithmetic help required

Graeme Geldenhuys graemeg.lists at gmail.com
Thu Oct 15 15:32:02 CEST 2009


By the way, I am reading in the Table of Contents data for an OS/2 INF file.


Martin wrote:
>>   
> if pentry is
>  TEntry = record .. end;
>  pentry = ^ TEntry


Yes, pEntry is a pointer to a structured type.
  pEntry: pTTOCEntryStart;

The actual structure is as follows...
------------------------
Type
  TTOCEntryStart = packed record
    length: uint8;// length in bytes including this byte
    flags: uint8; // flag byte, description folows (MSB first)
                  // bit1 haschildren;
                  // bit1 hidden;
                  // bit1 extended;     // extended entry format
                  // bit1 stuff;        // ??
                  // int4 level;        // nesting level
    numSlots: uint8; // number of "slots" occupied by the text for
                     // this toc entry
  end;
  pTTOCEntryStart = ^TTOCEntryStart;
------------------------


>> ------------------------
>>   pEntry := _pContentsData;
>>      // below toclen is in bytes
>>   pEnd := _pContentsData + _pHeader^.toclen;
>>   
> is toclen, in bytes, or in units (eg number of records)

_pHeader^.toclen is in number of bytes of data that pContentData is
pointing at.


> the above assumes bytes, if
> _pContentsData = pointer; //not typed

Yes, pContentsData is not typed because it contains various structures,
NOT just a repeat of one structure type.

So I assume I am incrementing pEnd correctly at that point.

>>   // 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

No, I don't think that is correct. The data pContentData is pointing at,
holds various structures of differing sizes. pEntry^.Length says that
all data relating to one TOC entry is XXX bytes in length. That means
the TTOCEntryStart structure itself (only 3 bytes) and various other
structures making up the difference.

eg: In the file I am trying to read, the first pEntry^.Length is 17
bytes. Meaning 3 bytes for TTOCEntryStart itself, and 14 bytes of other
information.

Here is a graphical but very simplified representation.

In this example, pContentsData will point to the beginning of this data
block, encapsulating all the information shown below. Each TOC entry
(complete data) is grouped together. As you can see, no set length, each
entry can have 0 or 1 "extended data" info and 1 or more "data slots"
and the title.

      pContentsData----v
                       -----------------------
                       TOCEntryStart
pEntry^.Length = 17    TOCExtendedInfo
                       Data Slot
                       Title
                       -----------------------
                       TOCEntryStart
pEntry^.Length = 10    Data Slot
                       Title
                       -----------------------
                       TOCEntryStart
pEntry^.Length = 30    TOCExtendedInfo
                       Data Slot
                       Data Slot
                       Data Slot
                       Title
                       -----------------------
           pEnd--------^

Base of the above information: _pHeader^.toclen should equal 57.




Regards,
  - Graeme -




More information about the fpc-pascal mailing list