Info Hiding in C

Ugo Cei newsuser at oliver.SUBLINK.ORG
Fri Apr 19 06:18:22 AEST 1991


unicorn at uxh.cso.uiuc.edu (Harry E Miller) writes:

>I wish to include optional information hiding within this library,
>so that the programer can only get string information from a
>function (not a macro!).
[...]
>#ifdef INFO_HIDING

>/* programmer can hide the structure String so that he has to
>   use functions to access the data
>*/
[...]
>typedef char String[6];   /* Hidden string */

>#else

>	                  /* ------------------------------------- */
>typedef struct _string {  /* 0x00 | string structure               */
>        char *str;        /* 0x02 | pointer to the char array      */
>        unsigned length;  /* 0x04 | current length of str          */
>        unsigned alloc;   /* 0x06 | amount allocated to str        */
>} _String;                /* 0x07 | end of _string structure       */
>	                  /* ------------------------------------- */

>typedef _String String;

As someone else pointed out, declaring an array of six chars for your
"opaque" string type puts on your code an ugly and unnecessary
dependence on the particular machine and compiler. What I have been
doing recently in cases like this (and I would very much like comments
on it) is to use incomplete types in a public header file, and their
complete definition in a private header. The modules implementing my
objects include the private header, while "client" programs will
include just the public header, like this:

-----

/*
 * file: String.h - public definitions for String class
 */

#ifndef _STRING_
#define _STRING_

typedef struct _String String;
  
...

/* Prototypes */

String * CreateString( ... );
...

#endif

-----

/*
 * file: StringP.h - private definitions for String class
 */

#ifndef _STRINGP_
#define _STRINGP_
#include "String.h"

struct 
  {
    ...
  }
_String;
  
#endif

-----

/*
 * file: String.c - implementation of the String class
 */

#include "StringP.h"

String *
CreateString( ... )
{
  ...
}

... other function to manipulate String's and access their fields

-----

/*
 * file: main.c - demonstration of usage of String class
 */

#include <String.h>

int
main(void)
{
  String * str;

  str = CreateString(...);
  ...
}

-----

Then, if you want to disable information hiding, just include
"StringP.h" instead of "String.h".

>  if (amount < len)        /* will allocate at least enough */
             ^^^
>     amount = len+1;       /* for the string to fit into    */

No, it won't, if amount == len. Your test should look like

   if(amount <= len)

Besides, I don't really grasp the need for the "amount" field in your
structure. Can't you just live with "len" ?

Cheers.
-- 
**************** | Ugo Cei            | home:    newsuser at oliver.sublink.org
*    OLIVER    * | Via Colombo 7      | office:  cei at ipvvis.unipv.it
**************** | 27100 Pavia ITALY  |       "Real Programs Dump Core"



More information about the Comp.lang.c mailing list