Page 1 of 1

Datahiding in C

Posted: Wed Sep 29, 2004 6:12 am
by Guest
i would just like 2 know if it is possible to achicve data hiding & encapsulation in "C"????

if this question sounds crazy please 4give me, i'm a programmer who is in the process of getting born ;)

links or advice related to this doubt will be appriciated.

Re:Datahiding in C

Posted: Wed Sep 29, 2004 7:12 am
by bubach
hide from what? ???

Re:Datahiding in C

Posted: Thu Sep 30, 2004 12:51 am
by Schol-R-LEA
You can't really get data hiding in C, as any structure type you define is either visible, and all the fields accessible, or not. While you can *treat* it as if the fields were hidden, you would always be able to 'break paradigm' and work on the fields directly.

You can get a crude sort of encapsulation by creating a structure containing pointers to the functions which operate on the structure, but it's generally not worth the trouble.

Re:Datahiding in C

Posted: Thu Sep 30, 2004 1:21 am
by mystran
Actually there IS one fairly well-known encapsulation method (which is probably what you mean by datahiding): opaque pointers.

The idea is that you define a type as a pointer to a struct, but never define the struct in the headers. Then you define a set of functions which operate with those pointers. Obviously you need to define the struct for implementation of those functions, but you need not include that definition in the header files.

Especially when you only ship a binary version of some library, this is very effective form of data-hiding. Sure, someone can look though the pointer and see what's on the other side, but so can they dissassembler library binaries.. but for normal programming, the pointer is opaque: nothing is known of the struct it points to, except that certain functions expect such struct to do something.

Some people go so far as to XOR the pointers with some random value, sometimes even a different value from one run of the program to the next one. The idea is that the pointer doesn't leak even the address it points to. Whether this is good or bad is arguable, but I'd advice agaist such tricks: if someone tries to use a conservative garbage collector (which can be a Good Thing) such XOR'ed pointers will cause BIG trouble.

Anyway, googling for "opaque pointers" should give you more info.

As for partially hiding data, you could do something like define a struct, which contains all public stuff, then require the struct to be allocated with a special allocator, and allocate a bigger struct instead, which contains the requested struct as it's first member. This should work with most compiler. Similar techniques are often used to implement inheritance in C.

Re:Datahiding in C

Posted: Thu Sep 30, 2004 10:59 am
by Schol-R-LEA
I should have remembered that; I've seen the technique used before. My bad.

Re:Datahiding in C

Posted: Thu Oct 28, 2004 11:32 am
by rich_m
Actually there IS one fairly well-known encapsulation method (which is probably what you mean by datahiding): opaque pointers.

The idea is that you define a type as a pointer to a struct, but never define the struct in the headers.
won't that give an error as in 'C' before using any thing we should declare it ... ???

Re:Datahiding in C

Posted: Fri Oct 29, 2004 4:50 am
by Solar
No. You know there's a struct FILE, and you have a number of functions declaring FILE * as parameter or return value.

The actual definition of FILE is "hidden" in the file implementing the functions.

Re:Datahiding in C

Posted: Fri Oct 29, 2004 5:12 am
by mystran
If you simply state use "struct foo;" or "typedef struct foo bar;" or something like that, you tell the compiler that you have such a struct, and that it's ok to let people use pointers to it. They can't refer to actual fields, or create non-pointer variables of that type, but there's nothing wrong with having pointers..

Also allows you to do things like this:

Code: Select all

/* define types, this is also enough to declare the struct */
typedef struct S_foo foo;
typedef struct S_bar bar;

/* now we can defined the two structs if we want */
struct S_foo {
     bar * b;
};

struct S_bar {
     foo * f;
};

Same thing works in C++ for classes.