Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!swrinde!elroy.jpl.nasa.gov!ncar!gatech!psuvax1!rutgers!modus!simm!yachaya!oliver!newsuser From: newsuser@oliver.SUBLINK.ORG (Ugo Cei) Newsgroups: comp.lang.c Subject: Re: Info Hiding in C Keywords: Hiding Message-ID: <961@oliver.SUBLINK.ORG> Date: 18 Apr 91 20:18:22 GMT References: <1991Apr17.004044.22940@ux1.cso.uiuc.edu> Organization: Oliver, Pavia, Italy Lines: 127 unicorn@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 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@oliver.sublink.org * OLIVER * | Via Colombo 7 | office: cei@ipvvis.unipv.it **************** | 27100 Pavia ITALY | "Real Programs Dump Core"