473,804 Members | 2,201 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 4457
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
2194
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" guidelines. Could anyone help me get started with this (any ideas?)
9
4987
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
2034
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 design? (protected is useless?)
1
1718
by: ctmashidayu | last post by:
how to code using C++ -security information hiding in an image? anybody know?
2
7645
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 the implementation of class member
2
1475
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
10327
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, but why does it miss one of the basic principles of OOP? Will it ever be added to python?
27
2790
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
11119
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! (He knows this I'm sure, but just didn't think this was my problem; LOL, I am needling him) If...
0
9595
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10600
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10352
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10097
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9175
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7642
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3835
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3002
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.