opaque types without malloc?

Discussion in 'C Programming' started by Ark Khasin, Sep 14, 2007.

  1. Ark Khasin

    Ark Khasin Guest

    I'd like to have something functionally equivalent to, say,
    void make_it_happen(void)
    {
    struct T t;
    for(start(&t); !done(&t); doit(&t));
    }
    The function has no business knowing what's inside struct T; it just
    provides a "holder". Ideally, I'd say e.g.
    struct T;
    to pass an incomplete type, but that's not enough for instantiation of t.
    A dynamic-memory-allocation-inspired scheme like
    void make_it_happen(void)
    {
    struct T *p = start_newT();
    for(; !done(p); doit(p));
    free(p);
    }
    is not an option on a small (embedded) target, not to mention the overhead.

    Is there a clever way not to drag around the complete definition of
    struct T but still be able to create a holder and point to it as to
    struct T* ?

    Likewise, is there a clever compile-time constant equivalent to
    sizeof(struct T), all without exposing complete definition.

    [I suspect both are silly questions, but I'd take my chances.]

    Thank you,
    --
    Ark
     
    Ark Khasin, Sep 14, 2007
    #1
    1. Advertising

  2. Ark Khasin said:

    <snip>

    > Is there a clever way not to drag around the complete definition of
    > struct T but still be able to create a holder and point to it as to
    > struct T* ?


    Not as far as I'm aware, but there may be an easy way.

    Your problem seems to be that you can't afford malloc. Well, you *have*
    to be able to afford the object itself, so why not make it a static
    object in the module that knows about struct Ts:

    static struct T t;

    struct T *get_T(void)
    {
    return &t;
    }

    Or, if you feel so inclined:

    static struct T t[MAX_T];

    with all the gubbins involved in providing controlled access to the
    members of that array.

    > Likewise, is there a clever compile-time constant equivalent to
    > sizeof(struct T), all without exposing complete definition.


    Not as far as I know.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
     
    Richard Heathfield, Sep 14, 2007
    #2
    1. Advertising

  3. Ark Khasin

    CBFalconer Guest

    Ark Khasin wrote:
    >

    .... snip ...
    >
    > Is there a clever way not to drag around the complete definition
    > of struct T but still be able to create a holder and point to it
    > as to struct T* ?


    It's not especially clever, but it works. Use pointers, and let
    the module that knows about the struct T do the mallocing. For an
    example, see hashlib.zip, and the way of assigning the basic hash
    table in hshinit(), at <http://cbfalconer.home.att.net/download/>

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>



    --
    Posted via a free Usenet account from http://www.teranews.com
     
    CBFalconer, Sep 14, 2007
    #3
  4. In article <eRpGi.4368$Z33.1218@trndny08>,
    Ark Khasin <> wrote:

    >I'd like to have something functionally equivalent to, say,
    >void make_it_happen(void)
    >{
    > struct T t;
    > for(start(&t); !done(&t); doit(&t));
    >}
    >The function has no business knowing what's inside struct T; it just
    >provides a "holder". Ideally, I'd say e.g.
    > struct T;
    >to pass an incomplete type, but that's not enough for instantiation of t.


    It can't be an incomplete type, or you wouldn't be able to declare an
    instance of it. Perhaps we can arrange for a complete type that is
    nonetheless useless. So you need a way to declare something whose
    memory is suitable for storing a struct T in. It needs to be the
    right size, and have the right alignment.

    Just declaring a struct containing char dummy[N], where N is the size
    of the real struct, won't necessarily work because of the alignment.
    Is a structure guaranteed to have the alignment of its first member?
    (That's not a rhetorical question.) If so, you could declare the
    "opaque" type as having a first member the same type as the real one,
    and the right number of padding bytes after it.

    Of course you'll have to count the determine the size by hand for each
    platform if you don't want the "real" type to be visible, as it would
    have to be to use it with sizeof().

    -- Richard
    --
    "Consideration shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.
     
    Richard Tobin, Sep 14, 2007
    #4
    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. dna
    Replies:
    0
    Views:
    1,200
  2. Martin Meier

    opaque - Bedeutung ?

    Martin Meier, Sep 10, 2004, in forum: Java
    Replies:
    3
    Views:
    1,459
    Eric Sosman
    Sep 10, 2004
  3. Brian Tozer

    Semi-opaque colored background

    Brian Tozer, Feb 8, 2004, in forum: HTML
    Replies:
    11
    Views:
    4,797
    Steve Pugh
    Feb 9, 2004
  4. Beauregard T. Shagnasty

    IE hides inline images with opaque background

    Beauregard T. Shagnasty, Dec 31, 2004, in forum: HTML
    Replies:
    13
    Views:
    915
  5. Rajshekhar
    Replies:
    5
    Views:
    2,242
    Jonathan Bartlett
    Mar 29, 2005
Loading...

Share This Page