Info Hiding in C

Harry E Miller unicorn at uxh.cso.uiuc.edu
Wed Apr 17 10:40:44 AEST 1991


Hi there,

I started learning C++ and one of the most interesting things about
it is information hiding.

For my project at work, I am developing a dynamically allocated
string library, that will eventually incorporate expanded memory.
(I'm working with Turbo C++ in C mode.)

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!).

I've tested the following three files, and they seem to work.
They compile without complaint with all warning flags on.

I would like any comments (flames if this is just crap) on
this way of (optionally) hiding the string structure.

Also why won't this work?

typedef void String[6];

The compiler gives me a "Not an allowed type" error.
 

I just made a fool of myself debugging a FORTRAN program (uch!!!) with
a distinguished colleague (sp?), so go ahead and flame away if you wish!

Please e-mail this to me.

Harry Miller aka "The Four-Fingered Bandit"
unicorn at uxh.cso.uiuc.edu  - lots of u's running around these hills

You can tell how far we have to go, when FORTRAN is the
    language of supercomputers.
         -- Steven Feiner


/* ------------------- cut here for file unicstr.h --------------- */

/* file unicstr.h */

#ifdef INFO_HIDING

/* programmer can hide the structure String so that he has to
   use functions to access the data
*/

/* typedef void String[6];  will not work! why? */

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;

#endif

/* alloc and dealloc prototypes */
String *StrAlloc(char *str, unsigned amount);
void     StrDealloc(String *str);

/* get info prototypes */
char *get_str(const String *str);
unsigned get_len(const String *str);
unsigned get_alloc(const String *str);


/* ------------------- cut here for file unicstr.c --------------- */

/* file unicstr.c */

#ifdef INFO_HIDING
#undef INFO_HIDING
#endif

#include <alloc.h>
#include <string.h>
#include "unicstr.h"

String *StrAlloc(char *strptr, unsigned amount)
{
String *s;
unsigned len;

  len = strlen(strptr);

  if ((s = (String *) malloc(sizeof(String))) == NULL)
     return (NULL);

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

  if ((s->str = (char *) malloc(amount)) == NULL)
     return (NULL);

  strcpy(s->str,strptr);
  s->length = len;
  s->alloc = amount;

  return (s);
}


void StrDealloc(String *str)
{
  free(str->str);
  free(str);
}


char *get_str(const String *str)
{
  return (str->str);
}


unsigned get_len(const String *str)
{
  return (str->length);
}


unsigned get_alloc(const String *str)
{
  return (str->alloc);
}

/* ----------------- cut here for file test.c -------------------- */

/* file test.c */

#define INFO_HIDING

#include <stdio.h>
#include "unicstr.h"

void main()
{
String *s1, *s2;

  s1 = StrAlloc("This is test 1", 20);
  s2 = StrAlloc("This is test 2", 10);

  printf(" %s\n length = %u\n allocated amount = %u\n", get_str(s1),
         get_len(s1), get_alloc(s1));

  printf(" %s\n length = %u\n allocated amount = %u\n", get_str(s2),
         get_len(s2), get_alloc(s2));

  StrDealloc(s1);
  StrDealloc(s2);
}



More information about the Comp.lang.c mailing list