[YLUG] Pointer help correction

Roger Leigh rleigh at whinlatter.ukfsn.org
Sun Jan 13 13:49:21 GMT 2008


"Dominic Hibbs" <dominic.hibbs at gmail.com> writes:

> POINTER HELP REQUESTED
>
> My first posting had some errors - I am still not sure of the exact
> structure I will be using.  I hope this is correct.

What you haven't told us is if your C++ program structures need to
*exactly* match the assembler structures (e.g. for reading a binary
format), or not.  You might need this for backward compatibility, or
you might have much more flexibility.

> I am converting a program, written in assembler to C/C++ and I am
> stuck over pointers.  This data structure is a linked list of
> variables.
>
> A block of memory consists of records made up of
>
> Byte No. Content
> (0..3)     a 32 bit pointer to
>                 a long int (8 Byte)
>                 or a double
>         or (32 bit pointer and two 16 bit integers)
> (4..11) followed by a long int or Double
>         or (32 bit pointer and two 16 bit integers)
> (12..15)     followed by one byte made up of
>         2 bits of flags, 4 bits of a number and two more bit flag
>         followed by an ASCIZ string of 4 n + 3 bytes long (n = 0..30)

So, actual data in the structure could be represented by something
like:

struct s
{
  void*   a,
  int16_t b,
  int16_t c
};

struct record
{
  union u1
  {
    int64_t* a,
    double*  b,
    s*       c
  };

  union u2
  {
    uint64_t  a,
    s         b
  };

  ...
};

> Bytes 12..15  are anded with  & 0x00 00 00 3c and then added to the address of
> this record to get the next record.

While this is compact and efficient, it's also a horror from a C++
programmer's perspective.  C++ provides mechanisms in the Standard
Library to do lists, e.g. std::list and std::vector.  These templated
containers should, with compiler optimisations turned on, be just as
time- and space-efficient as your assembly.

Consider, C++ lets you code from a higher level perspective, so rather
than show us the exact structure, tell us what you want the code to
/do/, and what information needs to be represented.  There's sure to
be a clean and efficient way of representing it.

Also note: pointers are /not/ 32 bits.  They might be on i386, but
they are not on amd64, and on other architectures, even 32-bit ones,
they may be greater than 32 bits (e.g. function pointers).  A pointer
is a pointer, and has an architecture-specific size which might even
change depending on what you are pointing to; you can't assume it's 32
bits, ever.  A C or C++ compiler will always handle this for you.

A union (storing different types in a common memory location) is
almost always a bad idea.  If you have one structure doing several
things, then you should consider if inheritance or templates are a
cleaner approach.  C++ has features such as typeof and dynamic_cast to
help here.


Regards,
Roger

-- 
  .''`.  Roger Leigh
 : :' :  Debian GNU/Linux             http://people.debian.org/~rleigh/
 `. `'   Printing on GNU/Linux?       http://gutenprint.sourceforge.net/
   `-    GPG Public Key: 0x25BFB848   Please GPG sign your mail.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 188 bytes
Desc: not available
Url : http://mailman.lug.org.uk/pipermail/york/attachments/20080113/143c2a15/attachment.bin


More information about the York mailing list