Data Structure

R

ranjeet.gupta

Dear All

Please go through the below data structure and please
let me know what it is exactly doing ? I am not able to understand
as there are the fuction pointers used inside it which is taking
the structure as the agrument, is this a sort of link list ?

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;

Regards
Ranjeet
 
R

Roger Leigh

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

Please go through the below data structure and please
let me know what it is exactly doing ?
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.

There are rather better ways to do this in C. See
http://www.le-hacker.org/papers/gobject/


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

iD8DBQFCvTsAVcFcaSW/uEgRAvtNAJ99LxLjjCndGWbigV8FwK5ALO4owACgwguA
soeNOjjhFba6RHT9nE7f8EM=
=Kys7
-----END PGP SIGNATURE-----
 
M

Malcolm

Roger Leigh said:
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.
 
H

He Shiming

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;

Well, I'm not sure if you know a bit about C++, this is a C++ class/object
definition equivalent in C. In the old days, we use this trick to define
objects and interfaces in C. The "weird stuff" you saw a just function
pointers. It creates a phantom of object-oriented programming. The
equivalent definition in C++ should go like this:

class struct_ObjectRecord
{
public:
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);
}

So that in the above declaration, you will be above to call a "method" by
syntax such as struct_ObjectRecord.Open(blah...);, it'll look like you are
calling an object local method, rather than a global function.

It's a fake object-oriented technique, because if you take a look at the
member functions, the first parameter is always the "this pointer" in C++.
This is required because C functions doesn't have a this pointer, they
function globally. When you initliaze this structure, you'll have to cast
the pointers to these global functions. After which, you can get them (local
functions) working like global functions, and therefore in the source code,
you'll see as if they were object-local methods.

Syntax like int (*Function)(parameter list) defines a pointer to the address
of a function, with "int" being the return type. The difference between this
syntax and other pointers like void* is that this syntax can be used to call
the pointee function, and void* can't.

Best regards,
He Shiming
 
R

Roger Leigh

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

Malcolm said:
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-----
 
C

Chris Torek

Roger Leigh said:
It's an attempt to fake objects with virtual functions in C. However,
it's grossly inefficient. ...

More importantly, it is just wong. It cannot compile as written,
because it suffers from typedef abuse. In particular, the alias
"struct_ObjectRecord" is defined at the end of the sequence (line
17), but is used earlier (lines 6, 9, 11, and 13), before it
exists. This is a syntax error; repairing it requires giving
the structure a "true name" (whether or not there is to be a
typedef-alias).

See <http://web.torek.net/torek/c/types2.html> for details.
 
B

Barry Schwarz

Dear All

Please go through the below data structure and please
let me know what it is exactly doing ? I am not able to understand
as there are the fuction pointers used inside it which is taking
the structure as the agrument, is this a sort of link list ?

None of the function pointers take the struct as an argument. They
all take pointers to struct. Once you correct the syntax errors Chris
pointed out this becomes acceptable because all pointers to struct are
required to have the same size and representation. Therefore, at the
time the function pointer members are being declared, the compiler
need to know only that struct_ObjectRecord is a struct. It does not
need to know any of the details, such as size or members, of the
struct. It works the same as if struct_ObjectRecord was an incomplete
struct at the time the members are processed.

It is not a linked list since the struct does not contain any pointer
to a struct of the same type.
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;

Regards
Ranjeet



<<Remove the del for email>>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,166
Messages
2,570,901
Members
47,442
Latest member
KevinLocki

Latest Threads

Top