Re: Data hiding in C without using the heap ?

Discussion in 'C Programming' started by Stefan Ram, Nov 26, 2009.

  1. Stefan Ram

    Stefan Ram Guest

    Thomas Richter <-berlin.de> writes:
    >RamRaider wrote:
    >>Is there some way to achieve data hiding without using the heap, ie
    >>static or local variables only ?

    >There is no problem creating objects on the stack, and using their


    In C, we do not have »heap« nor »stack«.

    We have »allocated storage duration« and
    »automatic storage duration«.

    Below, »a« is hidden, when the memory is
    represented as »data«, but available via
    »p« - if this is what the OP asked for.

    No allocated storage involved.

    int main( void )
    { struct private { int a; };
    struct public { char buff[ sizeof( struct private )]; } data;
    struct private * p =( void * )&data;
    p->a = 5; }
    Stefan Ram, Nov 26, 2009
    #1
    1. Advertising

  2. "Stefan Ram" <-berlin.de> wrote in message
    news:-berlin.de...
    > Thomas Richter <-berlin.de> writes:
    >>RamRaider wrote:
    >>>Is there some way to achieve data hiding without using the heap, ie
    >>>static or local variables only ?

    >>There is no problem creating objects on the stack, and using their


    I am not exactly sure what the OP means here...


    Humm, I guess you might be able to hack something together like:


    <pseudo-code typed in news reader!>
    _____________________________________________________________________

    /* foo.h */

    #define FOO_SIZE 32


    struct foo
    {
    double buf[(FOO_SIZE / sizeof(double)) + 1];
    };


    void foo_init(struct foo*);
    void foo_deinit(struct foo*);




    /* foo.c */

    #include "foo.h"


    struct foo_impl
    {
    int a;
    int b;
    int c;
    };


    typedef char static_assert
    [
    sizeof(struct foo_impl) <= FOO_SIZE ? 1 : -1
    ];


    void
    foo_init(struct foo* f)
    {
    struct foo_impl* impl = (struct foo_impl*)f;

    /* free to use `impl' */
    }


    void
    foo_deinit(struct foo* f)
    {
    struct foo_impl* impl = (struct foo_impl*)f;

    /* free to use `impl' */
    }




    /* main.c */
    #include <foo.h>


    int
    main(void)
    {
    struct foo f;

    foo_init(&f);

    foo_deinit(&f);

    return 0;
    }
    _____________________________________________________________________




    Is that crappy shi% something like the OP is writing about?

    ;^/
    Chris M. Thomasson, Nov 26, 2009
    #2
    1. Advertising

  3. Stefan Ram

    Stefan Ram Guest

    "Chris M. Thomasson" <> writes:
    >I am not exactly sure what the OP means here...


    The OPs were published in comp.lang.c.moderated.

    My replies were not published after 32 hours,
    I was a little impatient and posted them here.
    Stefan Ram, Nov 26, 2009
    #3
  4. Stefan Ram

    Lew Pitcher Guest

    On Nov 26, 4:18 pm, Richard <> wrote:
    > -berlin.de (Stefan Ram) writes:
    > > Thomas Richter <-berlin.de> writes:
    > >>RamRaider wrote:
    > >>>Is there some way to achieve data hiding without using the heap, ie
    > >>>static or local variables only ?
    > >>There is no problem creating objects on the stack, and using their

    >
    > >   In C, we do not have »heap« nor »stack«.

    >
    > Don't be ridiculous. Just about every C out there has a stack.


    Now, let's not start that argument again.

    [snip]
    --
    Lew Pitcher
    Master Codewright & JOAT-in-training | Registered Linux User #112576
    Me: http://pitcher.digitalfreehold.ca/ | Just Linux: http://justlinux.ca/
    ---------- Slackware - Because I know what I'm doing.
    ------
    Lew Pitcher, Nov 27, 2009
    #4
  5. On 26 Nov 2009 20:14:38 GMT, -berlin.de (Stefan Ram)
    wrote:

    >Thomas Richter <-berlin.de> writes:
    >>RamRaider wrote:
    >>>Is there some way to achieve data hiding without using the heap, ie
    >>>static or local variables only ?

    >>There is no problem creating objects on the stack, and using their

    >
    > In C, we do not have »heap« nor »stack«.
    >
    > We have »allocated storage duration« and
    > »automatic storage duration«.
    >
    > Below, »a« is hidden, when the memory is
    > represented as »data«, but available via
    > »p« - if this is what the OP asked for.
    >
    > No allocated storage involved.
    >
    >int main( void )
    >{ struct private { int a; };
    > struct public { char buff[ sizeof( struct private )]; } data;
    > struct private * p =( void * )&data;
    > p->a = 5; }


    Except that struct private may have a more stringent alignment
    requirement (frequently a multiple of 4) than struct public
    (frequently unrestricted). If data is not aligned properly for a
    struct private, then both assignment statements invoke undefined
    behavior.

    --
    Remove del for email
    Barry Schwarz, Dec 8, 2009
    #5
  6. On 26 Nov, 20:49, "Chris M. Thomasson" <> wrote:
    > "Stefan Ram" <-berlin.de> wrote in message
    > news:-berlin.de...
    > > Thomas Richter <-berlin.de> writes:
    > >>RamRaider wrote:


    > >>>Is there some way to achieve data hiding without using the heap, ie
    > >>>static or local variables only ?


    yes. Data hiding and storage allocation are orthogonal problems. You
    effectivly write your own allocator. This might be quite sane if
    malloc is expensive/fragments too much

    code fragments

    /* zork.h */

    typedef void* Zork;

    Zork create_zork (int param);
    int zork_action (Zork z, int param);
    int destry_zork (Zork z)


    /* zork.c */

    typedef struct
    {
    int able;
    int baker;
    int dog;
    } Zork_internal;

    define MAX_ZORK 100

    static int next_zork = 0;
    static Zork_internal zork[MAX_ZORK];

    Zork create_zork (int param)
    {
    if (next_zork == MAX_ZORK)
    return NULL;

    able = param;
    return &zork [next_zork++];
    }

    or use a free flag in the internal structure

    > >>There is no problem creating objects on the stack, and using their


    I'm not sure what happened here. But I think, yes, there well may be a
    problem in using stack (auto) variables. I'd need to knwo more.


    > I am not exactly sure what the OP means here...
    >
    > Humm, I guess you might be able to hack something together like:
    >
    > <pseudo-code typed in news reader!>
    > _____________________________________________________________________
    >
    > /* foo.h */
    >
    > #define FOO_SIZE 32
    >
    > struct foo
    > {
    >     double buf[(FOO_SIZE / sizeof(double)) + 1];
    >
    > };
    >
    > void foo_init(struct foo*);
    > void foo_deinit(struct foo*);
    >
    > /* foo.c */
    >
    > #include "foo.h"
    >
    > struct foo_impl
    > {
    >     int a;
    >     int b;
    >     int c;
    >
    > };
    >
    > typedef char static_assert
    > [
    >     sizeof(struct foo_impl) <= FOO_SIZE ? 1 : -1
    > ];
    >
    > void
    > foo_init(struct foo* f)
    > {
    >     struct foo_impl* impl = (struct foo_impl*)f;
    >
    >     /* free to use `impl' */
    >
    > }
    >
    > void
    > foo_deinit(struct foo* f)
    > {
    >     struct foo_impl* impl = (struct foo_impl*)f;
    >
    >     /* free to use `impl' */
    >
    > }
    >
    > /* main.c */
    > #include <foo.h>
    >
    > int
    > main(void)
    > {
    >     struct foo f;
    >
    >     foo_init(&f);
    >
    >     foo_deinit(&f);
    >
    >     return 0;}
    >
    > _____________________________________________________________________
    >
    > Is that crappy shi% something like the OP is writing about?
    >
    > ;^/
    Nick Keighley, Dec 8, 2009
    #6
    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. Michal Slocinski

    Heap dump file size vs heap size

    Michal Slocinski, Mar 25, 2008, in forum: Java
    Replies:
    1
    Views:
    718
    GArlington
    Mar 25, 2008
  2. viki
    Replies:
    6
    Views:
    554
    Erik Wikström
    Jun 28, 2008
  3. Raymond Schanks
    Replies:
    0
    Views:
    500
    Raymond Schanks
    Apr 11, 2010
  4. Replies:
    2
    Views:
    81
    Dag Sunde
    Jan 18, 2006
  5. Ste
    Replies:
    41
    Views:
    785
    Thomas 'PointedEars' Lahn
    Aug 1, 2007
Loading...

Share This Page