473,544 Members | 1,957 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Information hiding

Hi everybody, I'm wondering how to realize a simple pattern in C:
information hiding, to hide detail implementations of a data structure.
How can I do that? I've also read that is preferred to use only struct
and avoid typedef struct. Can anyone help? Use as example:
struct list {
int my_data;
struct list *next;
}
Thanks,
Mattia
Apr 8 '08 #1
13 4424
On Apr 8, 8:35 am, mattia <ger...@gmail.c omwrote:
Hi everybody, I'm wondering how to realize a simple pattern in C:
information hiding, to hide detail implementations of a data structure.
How can I do that? I've also read that is preferred to use only struct
and avoid typedef struct. Can anyone help? Use as example:
struct list {
int my_data;
struct list *next;}

Thanks,
Mattia
In your header file, you include an incomplete definition of an ADT
and declare the functions that will create and modify objects of that
type:

/** list.h */

#ifndef LIST_H
#define LIST_H

struct list;
typedef struct list list;

extern list *newList(void);
extern void deleteList(list **theList);

extern void addValue(list *theList, int value);
extern int getValue(list *theList);
extern void removeValue(lis t *theList, int value);

extern list *getNext(list *theList);

/* return TRUE if no elements in list */
extern int listEmpty(list *theList);

/* return TRUE if at end of list */
extern int listEnd(list *theList);

#endif

Then your implementation file will complete the definition of list:

/** list.c */

#include <stdlib.h>
#include "list.h"

struct list
{
int my_data;
struct list *next;
}

list *newList(void)
{
list *p = malloc(sizeof *p);
if (p)
{
p->my_data = 0;
p->next = NULL;
}

return p;
}

etc., etc., etc.

You should be able to figure out the rest from here.
Apr 8 '08 #2
dan

On 08 Apr 2008 13:35:26 GMT, mattia <ge****@gmail.c omwrote:
>Hi everybody, I'm wondering how to realize a simple pattern in C:
information hiding, to hide detail implementations of a data structure.
How can I do that? I've also read that is preferred to use only struct
and avoid typedef struct. Can anyone help? Use as example:
struct list {
int my_data;
struct list *next;
}
Information hiding is the idea that someone has access to your struct,
but does not have access to some of the data contained inside the
struct without going through your interface.

You can accomplish this by writing the functions which implement the
linked list. Like:
list* Initialize(int my_data);
void AddItem(list *l, int my_data);
void PrintList(list l);

The difficulty of doing this in C is that when you give someone a
reference to an instance of your struct, that person now has access to
everything in the struct and there is no guarantee that the person
will use your functions.

One way around this is to use static global variables. A static
global is local to the file which defines it, which means that it is
hidden from everyone else. Of course this will have all of the
typical restrictions you encounter when using global variables.

Another (better) way to implement information hiding is through
misdirection. Write all of your linked list functions but instead of
having the functions take a reference to an instance of the linked
list, have them take an identifier (like an int). And then you
maintain the map which converts the identifier to the proper
reference.
Apr 8 '08 #3
Ok, thanks for the advice, all clear. For the same reason in my header
file I declare extern the functions that I let the user call. Then in my
implementation file I will have to implement the various functions,
without the extern special character, _but_ the static character for my
private functions, isn't it?
Apr 8 '08 #4
#ifndef LIST_H
#define LIST_H

typedef struct _list list;
struct _list {
int my_data;
list *next;
};

Will be the same?
Apr 8 '08 #5
mattia said:
Hi everybody, I'm wondering how to realize a simple pattern in C:
information hiding, to hide detail implementations of a data structure.
How can I do that?
John Bode has explained (correctly, as far as I can see) how to create
opaque types.
I've also read that is preferred to use only struct
and avoid typedef struct.
And I've read that Elvis is still alive.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Apr 8 '08 #6
mattia said:
#ifndef LIST_H
#define LIST_H

typedef struct _list list;
struct _list {
int my_data;
list *next;
};

Will be the same?
No, for two reasons. Firstly, you were just shown how to hide the
implementation details, and you have unhidden them again by waving them
around in the header instead of keeping them nicely hidden in the
implementation file. Secondly, you've invaded implementation namespace
with your _list identifier. Avoid leading underscores when naming
identifiers, to avoid clashing with implementations .

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Apr 8 '08 #7
Richard Heathfield <rj*@see.sig.in validwrites:
mattia said:
>Hi everybody, I'm wondering how to realize a simple pattern in C:
information hiding, to hide detail implementations of a data structure.
How can I do that?

John Bode has explained (correctly, as far as I can see) how to create
opaque types.
>I've also read that is preferred to use only struct
and avoid typedef struct.

And I've read that Elvis is still alive.
There's a school of thought that says it's a good idea to declare a
typedef for a struct type, so that you have a one-word name you can
use to refer to it. There's another school of thought that prefers
not to add the typedef, and to use the name "struct foo" directly
whenever referring to the type.

Note that this issue has very little to do with information hiding --
except that if you want a type that's implemented as a struct to be
opaque (i.e., code that uses it shouldn't depend on its being as
struct), typedef'ing it is probably a good idea.

The type FILE in <stdio.his a good example.

Note that FILE is typically implemented as a typedef for a struct.
The struct declaration is usually (perhaps always?) *not* hidden.
This isn't a problem in practice, because programmers typically don't
write code that depends on how FILE is implemented.

--
Keith Thompson (The_Other_Keit h) <ks***@mib.or g>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Apr 8 '08 #8
Keith Thompson said:
Richard Heathfield <rj*@see.sig.in validwrites:
>mattia said:
>>Hi everybody, I'm wondering how to realize a simple pattern in C:
information hiding, to hide detail implementations of a data structure.
How can I do that?

John Bode has explained (correctly, as far as I can see) how to create
opaque types.
>>I've also read that is preferred to use only struct
and avoid typedef struct.

And I've read that Elvis is still alive.

There's a school of thought that says it's a good idea to declare a
typedef for a struct type, so that you have a one-word name you can
use to refer to it.
Right, and...
There's another school of thought that prefers
not to add the typedef, and to use the name "struct foo" directly
whenever referring to the type.
....right again. The point I was making was not that it is wrong to reject
the typedef, but that it is wrong to claim that rejecting the typedef is
"preferred" in any universal or near-universal sense. That is, it is
reasonable to claim that opinion is divided, but unreasonable to claim
that either one method or the other "is preferred" as if it had some kind
of overwhelming majority mindshare.
Note that this issue has very little to do with information hiding --
except that if you want a type that's implemented as a struct to be
opaque (i.e., code that uses it shouldn't depend on its being as
struct), typedef'ing it is probably a good idea.
Right.
The type FILE in <stdio.his a good example.
Hmmm...
Note that FILE is typically implemented as a typedef for a struct.
The struct declaration is usually (perhaps always?) *not* hidden.
This isn't a problem in practice, because programmers typically don't
write code that depends on how FILE is implemented.
I'm not sure whether FILE's details are /allowed/ to be hidden - or rather,
I'm fairly sure they're not. That is, I believe the following program to
be strictly conforming:

#include <stdio.h>

int main(void)
{
printf("%lu\n", (unsigned long)sizeof(FIL E));
return 0;
}

....which won't compile if FILE is opaque.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Apr 8 '08 #9
On Tue, 08 Apr 2008 19:17:45 +0000, Richard Heathfield wrote:
I'm not sure whether FILE's details are /allowed/ to be hidden - or
rather, I'm fairly sure they're not. That is, I believe the following
program to be strictly conforming:

#include <stdio.h>

int main(void)
{
printf("%lu\n", (unsigned long)sizeof(FIL E)); return 0;
}

...which won't compile if FILE is opaque.
<nit>

That is not a strictly conforming program. The number it outputs is
unspecified, and I'm sure you didn't mean to suggest otherwise. It must
be accepted by any conforming hosted implementation, because it's a
_correct_ program, not a strictly conforming one.
Apr 8 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2170
by: Amir S. | last post by:
Hi, I'm a newbie to C++ (2 weeks into the course). We were given this assignment to write some code that reads a set of integers (grades) from a file (filename passed by console), outputs them in reverse order, calculates and prints their average. => We have been told that we need to make sure that our code meets "information hiding"...
9
4968
by: bob | last post by:
Hi, I know there exists a good reason why the designers of c++ decided that function hiding should exist. But I don't know why. Can anybody provide a good reason/example of a case where function hiding saves the day. I know there exists one, I'd just like to hear about it. thanks and have a nice day.
7
2022
by: Alexander Eisenhuth | last post by:
Hi all, I'm wodering how the information hiding in python is ment. As I understand there doesn't exist public / protected / private mechanism, but a '_' and '__' naming convention. As I figured out there is only public and private possible as speakin in "C++ manner". Are you all happy with it. What does "the zen of python" say to that...
1
1708
by: ctmashidayu | last post by:
how to code using C++ -security information hiding in an image? anybody know?
2
7614
by: subramanian100in | last post by:
Is my following understanding correct ? Data abstraction means providing the interface - that is, the set of functions that can be called by the user of a class. Information hiding means mentioning the class members(functions, typedefs, data) under the access control labels : public, protected, private. Encapsulation means providing...
2
1464
by: anupkumarkadam | last post by:
How to use servlet filter for hiding information given by URLs. URLs shud not reveal any info regarding page extention,parameters.
162
10096
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 prefix them with 2 underscores, but I hate prefixing my vars, I'd rather add a keyword before it. Python advertises himself as a full OOP language,...
27
2743
by: matt | last post by:
Hello group, I'm trying to become familiar with the information hiding design rules, and I have a lot (3) of questions for all you experts. AFAIK, a generic module has 2 files: ================ module.h ================ #ifndef __MODULE_HDR_INCLUDED__
12
11027
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms. Here is a newbie mistake that I found myself doing (as a newbie), and that even a master programmer, the guru of this forum, Jon Skeet, missed!...
0
7431
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7370
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7617
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7714
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
4924
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3424
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1848
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
992
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
674
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.