Adding the ability to add functions into structures?

Discussion in 'C Programming' started by Albert, Dec 31, 2005.

  1. Albert

    Albert Guest

    So structures are useful to group variables, so you can to refer to a
    collection as a single entity. Wouldn't it be useful to also have the
    ability to collect variable and functions?

    Ask K&R say, C programs consist of variables to store the input and
    functions to manipulate them.

    This would make C object-oriented - how cool would that be?

    Are there any problems with adding the ability to have functions
    encapsulated in structures?
     
    Albert, Dec 31, 2005
    #1
    1. Advertising

  2. In article <>,
    Albert <> wrote:
    >So structures are useful to group variables, so you can to refer to a
    >collection as a single entity. Wouldn't it be useful to also have the
    >ability to collect variable and functions?


    >Are there any problems with adding the ability to have functions
    >encapsulated in structures?


    structures are data, functions are code. Having the ability to store
    functions (instead of function -pointers-) into structures would
    require mechanisms by which data become executable. Some
    architectures (e.g., the Harvard architecture) cannot do that.

    In architectures that do allow it, you would allow -all- of
    your data to be executable (in which case you have all of the
    classic stack overflow problems) or else you need a mechanism
    to -dynamically- indicate that a particular block of memory is
    executable; the existance of such a dynamic method is not at
    all certain on any particular system.
    --
    Is there any thing whereof it may be said, See, this is new? It hath
    been already of old time, which was before us. -- Ecclesiastes
     
    Walter Roberson, Dec 31, 2005
    #2
    1. Advertising

  3. Albert

    Chuck F. Guest

    Walter Roberson wrote:
    > Albert <> wrote:
    >
    >> So structures are useful to group variables, so you can to
    >> refer to a collection as a single entity. Wouldn't it be
    >> useful to also have the ability to collect variable and
    >> functions?

    >
    >> Are there any problems with adding the ability to have
    >> functions encapsulated in structures?

    >
    > structures are data, functions are code. Having the ability to
    > store functions (instead of function -pointers-) into structures
    > would require mechanisms by which data become executable. Some
    > architectures (e.g., the Harvard architecture) cannot do that.
    >
    > In architectures that do allow it, you would allow -all- of your
    > data to be executable (in which case you have all of the classic
    > stack overflow problems) or else you need a mechanism to
    > -dynamically- indicate that a particular block of memory is
    > executable; the existance of such a dynamic method is not at all
    > certain on any particular system.


    Methinks the OP wants lisp.

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
    More details at: <http://cfaj.freeshell.org/google/>
     
    Chuck F., Dec 31, 2005
    #3
  4. Albert

    Jack Klein Guest

    On 30 Dec 2005 17:12:33 -0800, "Albert"
    <> wrote in comp.lang.c:

    > So structures are useful to group variables, so you can to refer to a
    > collection as a single entity. Wouldn't it be useful to also have the
    > ability to collect variable and functions?
    >
    > Ask K&R say, C programs consist of variables to store the input and
    > functions to manipulate them.
    >
    > This would make C object-oriented - how cool would that be?
    >
    > Are there any problems with adding the ability to have functions
    > encapsulated in structures?


    It's already been done. They discuss it in comp.lang.c++, down the
    hall. And in comp.lang.java, and I'm sure also in whatever group it
    is on Microsoft's server that is devoted to C#.

    I know I'll get flamed for this, but with the exception of inheritance
    this is really nothing but syntactical sugar. You can write object
    oriented programs in C right now.

    A perfect example is the FILE data type, declared an <stdio.h>. It
    has a creator, fopen(), a destructor, fclose(), and all sorts of
    methods you can invoke on it via its pointer, such as fprintf(),
    fscanf(), fread(), fwrite(), between its successful creation and its
    destruction.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Dec 31, 2005
    #4
  5. Albert

    Artie Gold Guest

    Chuck F. wrote:
    > Walter Roberson wrote:
    >
    >> Albert <> wrote:
    >>
    >>> So structures are useful to group variables, so you can to
    >>> refer to a collection as a single entity. Wouldn't it be
    >>> useful to also have the ability to collect variable and
    >>> functions?

    >>
    >>
    >>> Are there any problems with adding the ability to have
    >>> functions encapsulated in structures?

    >>
    >>
    >> structures are data, functions are code. Having the ability to
    >> store functions (instead of function -pointers-) into structures
    >> would require mechanisms by which data become executable. Some
    >> architectures (e.g., the Harvard architecture) cannot do that.
    >>
    >> In architectures that do allow it, you would allow -all- of your
    >> data to be executable (in which case you have all of the classic
    >> stack overflow problems) or else you need a mechanism to
    >> -dynamically- indicate that a particular block of memory is
    >> executable; the existance of such a dynamic method is not at all
    >> certain on any particular system.

    >
    >
    > Methinks the OP wants lisp.
    >

    Methinks too much credit you offer to the OP.

    --ag

    --
    Artie Gold -- Austin, Texas
    http://goldsays.blogspot.com (new post 8/5)
    http://www.cafepress.com/goldsays
    "If you have nothing to hide, you're not trying!"
     
    Artie Gold, Dec 31, 2005
    #5
  6. Jack Klein said:

    > I know I'll get flamed for this,


    FLAME ON!!!

    > but with the exception of inheritance
    > this is really nothing but syntactical sugar. You can write object
    > oriented programs in C right now.


    Oh. Was that it? Sheesh. Flame off again. (sigh)

    > A perfect example is the FILE data type, declared an <stdio.h>. It
    > has a creator, fopen(), a destructor, fclose(), and all sorts of
    > methods you can invoke on it via its pointer, such as fprintf(),
    > fscanf(), fread(), fwrite(), between its successful creation and its
    > destruction.


    This is pretty much how I do it, yes. Just a minor nit - FILE isn't (quite)
    a perfect example, in at least two ways:

    1) FILE should, in my opinion, be a truly opaque type, with the structure
    definition hidden internally. typedef struct _iobuf FILE; is already more
    than we need to know. The whole schmeer is far, far more than we need to
    know.

    2) I wish I wish I wish that fclose took FILE ** rather than FILE *, don't
    you?

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, Dec 31, 2005
    #6
  7. Albert

    Chuck F. Guest

    Jack Klein wrote:
    >

    .... snip ...
    >
    > I know I'll get flamed for this, but with the exception of
    > inheritance this is really nothing but syntactical sugar. You
    > can write object oriented programs in C right now.
    >
    > A perfect example is the FILE data type, declared an <stdio.h>.
    > It has a creator, fopen(), a destructor, fclose(), and all sorts
    > of methods you can invoke on it via its pointer, such as
    > fprintf(), fscanf(), fread(), fwrite(), between its successful
    > creation and its destruction.


    I see no reason for any fires. The only problem with your example
    is that you can't write (in general) that type in C. As a further
    example I suggest my hashlib, which is written in standard C, is
    also very much object oriented, offers constructors, destructors,
    and methods.

    It offers the opaque incomplete object "hshtbl", whose details are
    completely hidden. This is unlike FILE*, which has to be
    incompletely hidden because of some standards constraints. Of
    course hashlib has the advantage of being designed some 30 plus
    years later than the FILE system.

    See: <http://cbfalconer.home.att.net/download/hashlib.zip>

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
    More details at: <http://cfaj.freeshell.org/google/>
     
    Chuck F., Dec 31, 2005
    #7
  8. Chuck F. said:

    > Jack Klein wrote:
    >>

    > ... snip ...
    >>
    >> I know I'll get flamed for this, but with the exception of
    >> inheritance this is really nothing but syntactical sugar. You
    >> can write object oriented programs in C right now.
    >>
    >> A perfect example is the FILE data type, declared an <stdio.h>.
    >> It has a creator, fopen(), a destructor, fclose(), and all sorts
    >> of methods you can invoke on it via its pointer, such as
    >> fprintf(), fscanf(), fread(), fwrite(), between its successful
    >> creation and its destruction.

    >
    > I see no reason for any fires. The only problem with your example
    > is that you can't write (in general) that type in C.


    I must have misunderstood what you mean, because I see no reason whatsoever
    why one could not write (in general) that type in C.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, Dec 31, 2005
    #8
  9. Albert

    Malcolm Guest

    "Jack Klein" <> wrote
    > I know I'll get flamed for this, but with the exception of inheritance

    ^^^^^^^^^^^^^^^^^^^^^^^^
    > this is really nothing but syntactical sugar. You can write object
    > oriented programs in C right now.
    >
    > A perfect example is the FILE data type, declared an <stdio.h>. It
    > has a creator, fopen(), a destructor, fclose(), and all sorts of
    > methods you can invoke on it via its pointer, such as fprintf(),
    > fscanf(), fread(), fwrite(), between its successful creation and its
    > destruction.
    >

    Inheritance is crucial.
    An object is any set of data items that are "part of the same thing". C
    structures are therefore objects. (The C standard further specifies that an
    object must be stored contigously in memory. This is a language issue and a
    fairly obvious thing to do, but not strictly necessary).

    A program becomes "object-oriented" not when it uses objects, but when the
    objects enter into relationships with each other. In C++ like most lanauges
    that support obect-orientation, this is achieved via inheirtance. However
    there are other ways, for example Microsoft Windows objects all respond to
    the same message system, Java interfaces specify common methods, text
    adventure objects have verb handlers.
     
    Malcolm, Dec 31, 2005
    #9
  10. Albert

    Chuck F. Guest

    Richard Heathfield wrote:
    > Chuck F. said:
    >> Jack Klein wrote:
    >>>

    >> ... snip ...
    >>>
    >>> I know I'll get flamed for this, but with the exception of
    >>> inheritance this is really nothing but syntactical sugar.
    >>> You can write object oriented programs in C right now.
    >>>
    >>> A perfect example is the FILE data type, declared an
    >>> <stdio.h>. It has a creator, fopen(), a destructor,
    >>> fclose(), and all sorts of methods you can invoke on it via
    >>> its pointer, such as fprintf(), fscanf(), fread(), fwrite(),
    >>> between its successful creation and its destruction.

    >>
    >> I see no reason for any fires. The only problem with your
    >> example is that you can't write (in general) that type in C.

    >
    > I must have misunderstood what you mean, because I see no reason
    > whatsoever why one could not write (in general) that type in C.


    I meant that to implement FILE you have to access such things as
    ports, disks, etc. that take you out of pure C.

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
    More details at: <http://cfaj.freeshell.org/google/>
     
    Chuck F., Dec 31, 2005
    #10
  11. Albert

    jacob navia Guest

    Albert a écrit :
    > So structures are useful to group variables, so you can to refer to a
    > collection as a single entity. Wouldn't it be useful to also have the
    > ability to collect variable and functions?
    >
    > Ask K&R say, C programs consist of variables to store the input and
    > functions to manipulate them.
    >
    > This would make C object-oriented - how cool would that be?
    >
    > Are there any problems with adding the ability to have functions
    > encapsulated in structures?
    >

    You can do this by defining an interface. I do this by using the
    following schema:

    The object has a pointer to the interface, called "lpVtbl", and other
    fields.

    The interface is a set of function pointers that defines the methods
    available for the object.

    For instance:

    1) I define a forward declaration to the object
    typedef struct _ArrayList ArrayList;

    2) I define the interface functions:
    typedef struct {
    // Returns the number of elements stored
    int (*GetCount)(ArrayList &AL);
    // Is this collection read only?
    int (*IsReadOnly)(ArrayList &AL);
    // Sets this collection read-only or unsets the read-only flag
    int (*SetReadOnly)(ArrayList &AL,int flag);
    ///SNIP /// SNIP /// SNIP
    // Pushes a string, using the collection as a stack
    int (*Push)(ArrayList &AL,void *str);
    } ArrayListInterface;

    3) I define the object
    struct _ArrayList {
    ArrayListInterface *lpVtbl; // The table of functions
    size_t count; /* number of elements in the array */
    void **contents; /* The contents of the collection */
    size_t capacity; /* allocated space in the contents vector */
    unsigned int flags; // Read-only or other flags
    size_t ElementSize; // Size of the elements stored in this array
    };

    The usage is now very simple: For instance to get the count of elements
    in an array list I do:

    ArrayList al;
    /// snip initialization, etc

    int count = al.lpVtbl->GetCount(&al);

    The lcc-win32 compiler uses this feature extensiveley to define
    arraylists and other data structures in C.

    jacob
     
    jacob navia, Dec 31, 2005
    #11
  12. Albert

    Eric Sosman Guest

    jacob navia wrote:
    > [...]
    > For instance:
    >
    > 1) I define a forward declaration to the object
    > typedef struct _ArrayList ArrayList;


    Have you forgotten that the identifier `_ArrayList'
    is off-limits?

    "All identifiers that begin with an underscore and
    either an uppercase letter or another underscore
    are always reserved for any use."
    -- ISO/IEC 9899:1999 section 7.1.3 paragraph 1

    --
    Eric Sosman
    lid
     
    Eric Sosman, Dec 31, 2005
    #12
  13. Chuck F. said:

    > Richard Heathfield wrote:
    >> Chuck F. said:
    >>> The only problem with your
    >>> example is that you can't write (in general) that type in C.

    >>
    >> I must have misunderstood what you mean, because I see no reason
    >> whatsoever why one could not write (in general) that type in C.

    >
    > I meant that to implement FILE you have to access such things as
    > ports, disks, etc. that take you out of pure C.


    Oh. I thought you meant opaque types were impossible, which they clearly
    aren't.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, Dec 31, 2005
    #13
  14. Albert

    Eric Sosman Guest

    jacob navia wrote:
    > [...]
    > 2) I define the interface functions:
    > typedef struct {
    > // Returns the number of elements stored
    > int (*GetCount)(ArrayList &AL);


    Whatever this language is, I doubt it's topical on
    comp.lang.c.

    --
    Eric Sosman
    lid
     
    Eric Sosman, Dec 31, 2005
    #14
  15. Albert

    Chuck F. Guest

    Eric Sosman wrote:
    > jacob navia wrote:
    >> [...]
    >> For instance:
    >>
    >> 1) I define a forward declaration to the object
    >> typedef struct _ArrayList ArrayList;

    >
    > Have you forgotten that the identifier `_ArrayList'
    > is off-limits?
    >
    > "All identifiers that begin with an underscore and
    > either an uppercase letter or another underscore
    > are always reserved for any use."
    > -- ISO/IEC 9899:1999 section 7.1.3 paragraph 1


    In this case I think you have forgotten that Jacob is the
    implementor, and thus correctly avoiding the users namespace.

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
    More details at: <http://cfaj.freeshell.org/google/>
     
    Chuck F., Dec 31, 2005
    #15
  16. Albert

    jacob navia Guest

    Eric Sosman a écrit :
    > jacob navia wrote:
    >
    >> [...]
    >> 2) I define the interface functions:
    >> typedef struct {
    >> // Returns the number of elements stored
    >> int (*GetCount)(ArrayList &AL);

    >
    >
    > Whatever this language is, I doubt it's topical on
    > comp.lang.c.
    >

    Yes, replace the "&" by "*".
    lcc-win32 supports "&" as in C++, but it can be
    replaced by "*". References are just pointers that
    can never be NULL. A pointer will do too.
     
    jacob navia, Dec 31, 2005
    #16
  17. Albert

    jacob navia Guest

    Eric Sosman a écrit :
    > jacob navia wrote:
    >
    >> [...]
    >> For instance:
    >>
    >> 1) I define a forward declaration to the object
    >> typedef struct _ArrayList ArrayList;

    >
    >
    > Have you forgotten that the identifier `_ArrayList'
    > is off-limits?
    >
    > "All identifiers that begin with an underscore and
    > either an uppercase letter or another underscore
    > are always reserved for any use."
    > -- ISO/IEC 9899:1999 section 7.1.3 paragraph 1
    >

    Yes, you are right. Just put
    typedef struct tagArrayList

    Sometimes I use _ because is shorter
     
    jacob navia, Dec 31, 2005
    #17
  18. On 2005-12-31 09:50:09 -0500, jacob navia <> said:

    > Eric Sosman a écrit :
    >> jacob navia wrote:
    >>
    >>> [...]
    >>> 2) I define the interface functions:
    >>> typedef struct {
    >>> // Returns the number of elements stored
    >>> int (*GetCount)(ArrayList &AL);

    >>
    >>
    >> Whatever this language is, I doubt it's topical on
    >> comp.lang.c.
    >>

    > Yes, replace the "&" by "*".
    > lcc-win32 supports "&" as in C++, but it can be
    > replaced by "*". References are just pointers that
    > can never be NULL. A pointer will do too.


    Why do you keep posting code that, while almost C, will only work on a
    single compiler, on a single platform?

    --
    Clark S. Cox, III
     
    Clark S. Cox III, Dec 31, 2005
    #18
  19. jacob navia <> writes:
    > Eric Sosman a écrit :
    >> jacob navia wrote:
    >>
    >>> [...]
    >>> For instance:
    >>>
    >>> 1) I define a forward declaration to the object
    >>> typedef struct _ArrayList ArrayList;

    >> Have you forgotten that the identifier `_ArrayList'
    >> is off-limits?
    >> "All identifiers that begin with an underscore and
    >> either an uppercase letter or another underscore
    >> are always reserved for any use."
    >> -- ISO/IEC 9899:1999 section 7.1.3 paragraph 1
    >>

    > Yes, you are right. Just put
    > typedef struct tagArrayList
    >
    > Sometimes I use _ because is shorter


    Why not just use

    struct ArrayList {
    ...
    };

    and then consistently refer to the type as "struct ArrayList"? What
    benefit does the typedef give you? (If you think not having to type
    the word "struct" is enough of a benefit, I disagree but I won't argue
    about it.)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Dec 31, 2005
    #19
  20. Albert

    jacob navia Guest

    Clark S. Cox III a écrit :
    > On 2005-12-31 09:50:09 -0500, jacob navia <> said:
    >
    >> Eric Sosman a écrit :
    >>
    >>> jacob navia wrote:
    >>>
    >>>> [...]
    >>>> 2) I define the interface functions:
    >>>> typedef struct {
    >>>> // Returns the number of elements stored
    >>>> int (*GetCount)(ArrayList &AL);
    >>>
    >>>
    >>>
    >>> Whatever this language is, I doubt it's topical on
    >>> comp.lang.c.
    >>>

    >> Yes, replace the "&" by "*".
    >> lcc-win32 supports "&" as in C++, but it can be
    >> replaced by "*". References are just pointers that
    >> can never be NULL. A pointer will do too.

    >
    >
    > Why do you keep posting code that, while almost C, will only work on a
    > single compiler, on a single platform?
    >


    Well, I forgot that. It is a single compiler yes, and I do not have
    the means of porting it. But what's your point?

    Changing "&" by "*" will make it work in any compiler in any platform.
     
    jacob navia, Dec 31, 2005
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Les Caudle
    Replies:
    0
    Views:
    614
    Les Caudle
    Mar 7, 2004
  2. Eric
    Replies:
    1
    Views:
    780
    Jim Higson
    Nov 14, 2005
  3. tweak
    Replies:
    14
    Views:
    2,789
    Eric Sosman
    Jun 11, 2004
  4. nholtz
    Replies:
    2
    Views:
    468
    nholtz
    May 13, 2006
  5. Alfonso Morra
    Replies:
    11
    Views:
    722
    Emmanuel Delahaye
    Sep 24, 2005
Loading...

Share This Page