Datahiding in C

Programming, for all ages and all languages.
Post Reply
Guest

Datahiding in C

Post 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.
User avatar
bubach
Member
Member
Posts: 1223
Joined: Sat Oct 23, 2004 11:00 pm
Location: Sweden
Contact:

Re:Datahiding in C

Post by bubach »

hide from what? ???
"Simplicity is the ultimate sophistication."
http://bos.asmhackers.net/ - GitHub
Schol-R-LEA

Re:Datahiding in C

Post 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.
mystran

Re:Datahiding in C

Post 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.
Schol-R-LEA

Re:Datahiding in C

Post by Schol-R-LEA »

I should have remembered that; I've seen the technique used before. My bad.
rich_m

Re:Datahiding in C

Post 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 ... ???
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:Datahiding in C

Post 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.
Every good solution is obvious once you've found it.
mystran

Re:Datahiding in C

Post 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.
Post Reply