473,322 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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
Jun 29 '10 #1
5 3941
donbock
2,426 Expert 2GB
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++.
Jun 29 '10 #2
i think he was refering to the manipulation of data only through access functions...cud u plz elaborate on this...

thanks
Jun 30 '10 #3
donbock
2,426 Expert 2GB
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:
Expand|Select|Wrap|Line Numbers
  1. foo.h:
  2.    typedef int foo_t;
  3.    void writeFoo(foo_t value);
  4.    foo_t readFoo(void);
  5.  
  6. foo.c:
  7.    #include "foo.h"
  8.    static foo_t foo = 0;
  9.    void writeFoo(foo_t value) {
  10.       foo = value;
  11.       }
  12.    foo_t readFoo(void) {
  13.       return foo;
  14.       }
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.
Jun 30 '10 #4
@donbock
hi
i am interested to know as to y are v doing so....
Jun 30 '10 #5
weaknessforcats
9,208 Expert Mod 8TB
This is an example of data hiding:

Expand|Select|Wrap|Line Numbers
  1. struct Data*  ptr)
  2.  
  3. 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:

Expand|Select|Wrap|Line Numbers
  1. void CreateData(struct Data* arg, int val1, int val2,
  2. 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:

Expand|Select|Wrap|Line Numbers
  1. 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.
Jun 30 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Lorenzo Villari | last post by:
I premise I don't know C++ well but... I wondered what is this data hiding thing... I mean, if I can look at the header (and i need it beacuse of the class), then what's hidden? Can someone give...
2
by: coolwarrior | last post by:
Hi, 1_I want to know the difference between "data hiding" , "steganography" ,"watermarking" ,"capsulation" related to DSP. 2_There r plenty of informaion about data hiding for images on the web...
12
by: Alex Hunsley | last post by:
There's no really specific questions in this post, but I'm looking for people's thought on the issues within... The two main versions I've encountered for data pseudo-hiding (encapsulation) in...
11
by: sofeng | last post by:
I'm not sure if "data hiding" is the correct term, but I'm trying to emulate this object-oriented technique. I know C++ probably provides much more than my example, but I'd just like some feedback...
162
by: Sh4wn | last post by:
Hi, first, python is one of my fav languages, and i'll definitely keep developing with it. But, there's 1 one thing what I -really- miss: data hiding. I know member vars are private when you...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.