Data Structure

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ranjeet.gupta@gmail.com

    #1

    Data Structure

    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_ObjectR ecord *Self,
    const char *Filename);

    int_err (*close)(struct _ObjectRecord *Self);

    int_err (*destroy)(stru ct_ObjectRecord *Self);

    int_err (*YourFile)(str uct_ObjectRecor d *self,
    const char *filename,
    int *outFile);

    } struct_ObjectRe cord;

    Regards
    Ranjeet

  • Roger Leigh

    #2
    Re: Data Structure

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

    ranjeet.gupta@g mail.com writes:
    [color=blue]
    > Please go through the below data structure and please
    > let me know what it is exactly doing ?[/color]
    [color=blue]
    > typedef struct
    > {
    > char *Data;
    > char *Parent;
    > int Size;
    > int_err (*Open) (struct_ObjectR ecord *Self,
    > const char *Filename);
    >
    > int_err (*close)(struct _ObjectRecord *Self);
    >
    > int_err (*destroy)(stru ct_ObjectRecord *Self);
    >
    > int_err (*YourFile)(str uct_ObjectRecor d *self,
    > const char *filename,
    > int *outFile);
    >
    > } struct_ObjectRe cord;[/color]

    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
    Find information, resources and relevant links for le-hacker.org. This domain may be for sale.



    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.sourc eforge.net/>

    iD8DBQFCvTsAVcF caSW/uEgRAvtNAJ99LxL jjCndGWbigV8FwK 5ALO4owACgwguA
    soeNOjjhFba6RHT 9nE7f8EM=
    =Kys7
    -----END PGP SIGNATURE-----

    Comment

    • Malcolm

      #3
      Re: Data Structure


      "Roger Leigh" <${rleigh}@inva lid.whinlatter. ukfsn.org.inval id> wrote[color=blue][color=green]
      >> typedef struct
      >> {
      >> char *Data;
      >> char *Parent;
      >> int Size;
      >> int_err (*Open) (struct_ObjectR ecord *Self,
      >> const char *Filename);
      >>
      >> int_err (*close)(struct _ObjectRecord *Self);
      >>
      >> int_err (*destroy)(stru ct_ObjectRecord *Self);
      >>
      >> int_err (*YourFile)(str uct_ObjectRecor d *self,
      >> const char *filename,
      >> int *outFile);
      >>
      >> } struct_ObjectRe cord;[/color]
      >
      > 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.
      >[/color]
      It's not grossly inefficient. It avoid a layer of indirection for the cost
      of three pointers per structure. Unless the number of struct_ObjectRe cords
      is very large this is not unreasonable.


      Comment

      • He Shiming

        #4
        Re: Data Structure

        > typedef struct[color=blue]
        > {
        > char *Data;
        > char *Parent;
        > int Size;
        > int_err (*Open) (struct_ObjectR ecord *Self,
        > const char *Filename);
        >
        > int_err (*close)(struct _ObjectRecord *Self);
        >
        > int_err (*destroy)(stru ct_ObjectRecord *Self);
        >
        > int_err (*YourFile)(str uct_ObjectRecor d *self,
        > const char *filename,
        > int *outFile);
        >
        > } struct_ObjectRe cord;
        >[/color]

        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_ObjectRe cord
        {
        public:
        char *Data;
        char *parent;
        int Size;
        int_err Open(struct_Obj ectRecord *Self, const char *Filename);
        int_err close(struct_Ob jectRecord *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_ObjectRe cord.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)(par ameter 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


        Comment

        • Roger Leigh

          #5
          Re: Data Structure

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

          "Malcolm" <regniztar@btin ternet.com> writes:
          [color=blue]
          > "Roger Leigh" <${rleigh}@inva lid.whinlatter. ukfsn.org.inval id> wrote[color=green][color=darkred]
          >>> typedef struct
          >>> {
          >>> char *Data;
          >>> char *Parent;
          >>> int Size;
          >>> int_err (*Open) (struct_ObjectR ecord *Self,
          >>> const char *Filename);
          >>>
          >>> int_err (*close)(struct _ObjectRecord *Self);
          >>>
          >>> int_err (*destroy)(stru ct_ObjectRecord *Self);
          >>>
          >>> int_err (*YourFile)(str uct_ObjectRecor d *self,
          >>> const char *filename,
          >>> int *outFile);
          >>>
          >>> } struct_ObjectRe cord;[/color]
          >>
          >> 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.
          >>[/color]
          > It's not grossly inefficient. It avoid a layer of indirection for the cost
          > of three pointers per structure. Unless the number of struct_ObjectRe cords
          > is very large this is not unreasonable.[/color]

          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.sourc eforge.net/>

          iD8DBQFCvZsoVcF caSW/uEgRAtRcAJ9w37P X67B9Hl6Vt7SvzE FzsakB0gCg3W/X
          KXfLLe8oDkl/mlw6iXFuZoA=
          =n7Eb
          -----END PGP SIGNATURE-----

          Comment

          • Chris Torek

            #6
            Re: Data Structure

            In article <871x6qk83h.fsf @hardknott.home .whinlatter.ukf sn.org>[color=blue]
            >ranjeet.gupta@ gmail.com writes:[color=green]
            >> typedef struct
            >> {
            >> char *Data;
            >> char *Parent;
            >> int Size;
            >> int_err (*Open) (struct_ObjectR ecord *Self,
            >> const char *Filename);
            >>
            >> int_err (*close)(struct _ObjectRecord *Self);
            >>
            >> int_err (*destroy)(stru ct_ObjectRecord *Self);
            >>
            >> int_err (*YourFile)(str uct_ObjectRecor d *self,
            >> const char *filename,
            >> int *outFile);
            >>
            >> } struct_ObjectRe cord;[/color][/color]

            Roger Leigh <${rleigh}@inva lid.whinlatter. ukfsn.org.inval id> wrote:[color=blue]
            >It's an attempt to fake objects with virtual functions in C. However,
            >it's grossly inefficient. ...[/color]

            More importantly, it is just wong. It cannot compile as written,
            because it suffers from typedef abuse. In particular, the alias
            "struct_ObjectR ecord" 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.
            --
            In-Real-Life: Chris Torek, Wind River Systems
            Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
            email: forget about it http://web.torek.net/torek/index.html
            Reading email is like searching for food in the garbage, thanks to spammers.

            Comment

            • Barry Schwarz

              #7
              Re: Data Structure

              On 25 Jun 2005 03:55:37 -0700, ranjeet.gupta@g mail.com wrote:
              [color=blue]
              >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 ?[/color]

              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_ObjectRe cord 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_ObjectRe cord 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.
              [color=blue]
              >
              >typedef struct
              >{
              > char *Data;
              > char *Parent;
              > int Size;
              > int_err (*Open) (struct_ObjectR ecord *Self,
              > const char *Filename);
              >
              > int_err (*close)(struct _ObjectRecord *Self);
              >
              > int_err (*destroy)(stru ct_ObjectRecord *Self);
              >
              > int_err (*YourFile)(str uct_ObjectRecor d *self,
              > const char *filename,
              > int *outFile);
              >
              >} struct_ObjectRe cord;
              >
              >Regards
              >Ranjeet[/color]



              <<Remove the del for email>>

              Comment

              Working...