Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: Data hiding in C without using the heap ?

Reply
Thread Tools

Re: Data hiding in C without using the heap ?

 
 
Stefan Ram
Guest
Posts: n/a
 
      11-26-2009
Thomas Richter <(E-Mail Removed)-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; }

 
Reply With Quote
 
 
 
 
Chris M. Thomasson
Guest
Posts: n/a
 
      11-26-2009
"Stefan Ram" <(E-Mail Removed)-berlin.de> wrote in message
news:(E-Mail Removed)-berlin.de...
> Thomas Richter <(E-Mail Removed)-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?

;^/

 
Reply With Quote
 
 
 
 
Stefan Ram
Guest
Posts: n/a
 
      11-26-2009
"Chris M. Thomasson" <(E-Mail Removed)> 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.

 
Reply With Quote
 
Lew Pitcher
Guest
Posts: n/a
 
      11-27-2009
On Nov 26, 4:18*pm, Richard <(E-Mail Removed)> wrote:
> (E-Mail Removed)-berlin.de (Stefan Ram) writes:
> > Thomas Richter <(E-Mail Removed)-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.
------
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      12-08-2009
On 26 Nov 2009 20:14:38 GMT, http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de (Stefan Ram)
wrote:

>Thomas Richter <(E-Mail Removed)-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
 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      12-08-2009
On 26 Nov, 20:49, "Chris M. Thomasson" <(E-Mail Removed)> wrote:
> "Stefan Ram" <(E-Mail Removed)-berlin.de> wrote in message
> news:(E-Mail Removed)-berlin.de...
> > Thomas Richter <(E-Mail Removed)-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?
>
> ;^/


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How much heap can I reserve for JVM? Error when allocation very much heap Raymond Schanks Java 0 04-11-2010 04:25 PM
stl heap question: restoring heap validinty after changing 1 element viki C++ 6 06-28-2008 10:12 AM
Heap dump file size vs heap size Michal Slocinski Java 1 03-25-2008 12:54 PM
Script for Hiding/Un-Hiding Text On Click Ste Javascript 41 08-01-2007 02:35 PM
Hiding HTML elements without using Javascript hagarwal@tamaleresearch.com Javascript 2 01-18-2006 02:36 AM



Advertisments