if data hiding can be done in C? If yes..How??

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • brightk2010
    New Member
    • Jun 2010
    • 4

    #1

    if data hiding can be done in C? If yes..How??

    hi ppl
    In an interview i was asked if we can perform data hiding in c...i have been through a lot of books and sites but i dont quite understand the logic...can some1 plz give me a simple explanation and all the techniques that can be used...

    thanks in advance
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    First let's make sure we are all talking about the same thing. Please summarize for us what the phrase "data hiding" meant to you and the interviewer.

    Techniques I use to achieve my personal notion of "data hiding" include the following. I can elaborate on these once I know how closely our notions of "data hiding" coincide.
    • Manipulate data only through access functions rather than by direct memory access.
    • Pass void* "handles" between client and server.
    • Pass pointers to incomplete structures between client and server.
    • Whereever possible declare pointer parameters as const pointers.
    • Minimize the symbols (both data and functions) that have extern scope.


    Despite what the OOPers want you to believe, the architectural concepts of coupling and cohesion predate object-oriented programming. I suggest you look up those terms.

    A related interview question might be whether it is possible to totally subvert "data hiding" in C++.

    Comment

    • brightk2010
      New Member
      • Jun 2010
      • 4

      #3
      i think he was refering to the manipulation of data only through access functions...cud u plz elaborate on this...

      thanks

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        Access functions are pretty trivial in concept. You have a variable, but you want to hide it (that is, you don't want other parts of the program to access it directly). Consider a single source file that contains a static definition of the variable (static so that it is not available to any other source file), a function that writes to the variable, and a function that reads the variable.

        Here is a degenerate example:
        Code:
        foo.h:
           typedef int foo_t;
           void writeFoo(foo_t value);
           foo_t readFoo(void);
        
        foo.c:
           #include "foo.h"
           static foo_t foo = 0;
           void writeFoo(foo_t value) {
              foo = value;
              }
           foo_t readFoo(void) {
              return foo;
              }
        In real life this code would be more complicated.

        Are you more interested in how to write an access function or why doing so might be a good idea?

        Information hiding is a much bigger topic than access functions; and it is more fruitful than this trivial example would lead you to believe.

        Comment

        • brightk2010
          New Member
          • Jun 2010
          • 4

          #5
          Originally posted by donbock
          Access functions are pretty trivial in concept. You have a variable, but you want to hide it (that is, you don't want other parts of the program to access it directly). Consider a single source file that contains a static definition of the variable (static so that it is not available to any other source file), a function that writes to the variable, and a function that reads the variable.

          Here is a degenerate example:
          Code:
          foo.h:
             typedef int foo_t;
             void writeFoo(foo_t value);
             foo_t readFoo(void);
          
          foo.c:
             #include "foo.h"
             static foo_t foo = 0;
             void writeFoo(foo_t value) {
                foo = value;
                }
             foo_t readFoo(void) {
                return foo;
                }
          In real life this code would be more complicated.

          Are you more interested in how to write an access function or why doing so might be a good idea?

          Information hiding is a much bigger topic than access functions; and it is more fruitful than this trivial example would lead you to believe.
          hi
          i am interested to know as to y are v doing so....

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            This is an example of data hiding:

            Code:
            struct Data*  ptr)
            
            CreateData(ptr, 5,10, 25.5);

            From this you cannot deduce the names of the members of the struct. They are hidden inside CreateData().

            Now if the user is given a header file:

            Code:
            void CreateData(struct Data* arg, int val1, int val2,
            double vale);
            then you still can't know the names of the struct member.

            That means I am no free to re-design the struct and since no one knows the member names, then my re-design will not break existing code.

            The actual CreateData() function is provided as a shared object (dll) and a header with the function prototype.

            You use data hiding to defeat:

            Code:
            ptr->mema->speed->km = 25.5;
            This is called spaghetti code and it means that any change to any of the structs in the string will break this line of code. Code like this effectively freezes the program at the current rev level. Unless you have a computer in the basement and you control all of the terminals.

            Comment

            Working...