Re: Data Structure



-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

"Malcolm" <regniztar@xxxxxxxxxxxxxx> writes:

> "Roger Leigh" <${rleigh}@invalid.whinlatter.ukfsn.org.invalid> wrote
>>> typedef struct
>>> {
>>> char *Data;
>>> char *Parent;
>>> int Size;
>>> int_err (*Open) (struct_ObjectRecord *Self,
>>> const char *Filename);
>>>
>>> int_err (*close)(struct_ObjectRecord *Self);
>>>
>>> int_err (*destroy)(struct_ObjectRecord *Self);
>>>
>>> int_err (*YourFile)(struct_ObjectRecord *self,
>>> const char *filename,
>>> int *outFile);
>>>
>>> } struct_ObjectRecord;
>>
>> It's an attempt to fake objects with virtual functions in C. However,
>> it's grossly inefficient. The virtual functions should have been
>> placed in a separate "class" or "vtable" structure, because there's
>> only one instance required for each class. You have a pointer to the
>> vtable as part of each object struct.
>>
> It's not grossly inefficient. It avoid a layer of indirection for the cost
> of three pointers per structure. Unless the number of struct_ObjectRecords
> is very large this is not unreasonable.

It's still (IMO, of course) suboptimal. You waste the space taken up
by three pointers in every instance. Depending on the number of
objects you need to instantiate, this could be a very large amount of
waste. In this case it's 3/7 of the total size (assuming int has the
same size as a pointer).

More importantly, it prevents new member variables being added, so you
can't derive new instance types. If the instance and class structures
are separate, you can extend either at will.


Regards,
Roger

- --
Roger Leigh
Printing on GNU/Linux? http://gimp-print.sourceforge.net/
Debian GNU/Linux http://www.debian.org/
GPG Public Key: 0x25BFB848. Please sign and encrypt your mail.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.8 <http://mailcrypt.sourceforge.net/>

iD8DBQFCvZsoVcFcaSW/uEgRAtRcAJ9w37PX67B9Hl6Vt7SvzEFzsakB0gCg3W/X
KXfLLe8oDkl/mlw6iXFuZoA=
=n7Eb
-----END PGP SIGNATURE-----
.



Relevant Pages

  • Re: Another spinoza challenge
    ... You should test against the int type's limits: ... typedef struct complex ... a pointer to an integer ... A macro is preferable because it is replaced by inline code, ...
    (comp.lang.c)
  • Re: how to store list of varying types
    ... represent that with those in the struct definitions? ... pointer null, and the second one the CString object. ... then have to finish constructing the packet by copying the two data objects ... start with an upper-case letter (so, I would use DataElement ...
    (microsoft.public.vc.mfc)
  • Re: N1298 - try/finally for C
    ... Do you mean that "struct throwInfo" should be optional? ... that exception handling would be supported only on implementations ... If the standard mandates an integer as throw argument, ... should be an integer wide enough to encode a pointer if necessary. ...
    (comp.std.c)
  • Re: [RFC][PATCH 1/4] cgroup: support per cgroup subsys state ID (CSS ID)
    ... Patch for Per-CSS(Cgroup Subsys State) ID and private hierarchy code. ... This patch attaches unique ID to each css and provides following. ... returns pointer to struct cgroup_subysys_state of id. ... using a pointer introduces no such restriction. ...
    (Linux-Kernel)
  • Re: Memory Structure Pointer Problems
    ... typedef struct sta { ... char* name; ... int num_cmpnds; ... A pointer to a struct cmp is almost ...
    (comp.lang.c)