Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #10144

Re: Does this confirm to ISO C?

From jt@toerring.de (Jens Thoms Toerring)
Newsgroups comp.lang.c
Subject Re: Does this confirm to ISO C?
Date 2011-09-11 21:22 +0000
Organization Freie Universitaet Berlin
Message-ID <9d4n3tF8svU1@mid.uni-berlin.de> (permalink)
References <f091c144-c8df-4205-9607-b7451825ec76@y39g2000yqd.googlegroups.com>

Show all headers | View raw


Максим Фомин <maxim@maxim-fomin.ru> wrote:
> I want to hide several fields within structure from "client" code. Can
> I use following (just example):

> - declare struct X { int visible; }; in "export.h" and several
> functions, which take struct X as an argument;
> - "export.h" is included by client code;
> - declare struct X { int visible; int hidden; }; in "private.h";
> - "private.h" is included in .c file which contains definitions of
> functions which work with struct X

> Thus, when mentioned functions process struct X, they may access to
> hidden integer, however external code even doesn't suspect that there
> is one.

> The first issue of this technique is that client code cannot hold
> allocation.

Looks extremely dirty to me, especially naming both the same.
And yes, the user can't allocate such a structure, nor can he
make copies - and there's nothing that would allow you to
control that the client doesn't do it anyway and mess up your
whole scheme. And you can only pass pointers to the struc-
ture between your and the clients code (otherwise the client
would need a full definition or things would get messed up
completely if the clients code would receive a larger struc-
ture than it is made to expect).  Why not then simplify life
a lot by making it a completely opaque structure and export a
function that allows the client to set the visible member?
While you're at it you could also supply a function for allo-
cation and deallocation. Looks a lot safer to me and will cost
you not much more than the work of adding a rather small number
of functions. With a typedef you could even make it invisible
to the client that it's dealing with just pointers to that
kind of structures.
                            Regards, Jens
-- 
  \   Jens Thoms Toerring  ___      jt@toerring.de
   \__________________________      http://toerring.de

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Does this confirm to ISO C? Максим Фомин <maxim@maxim-fomin.ru> - 2011-09-11 12:49 -0700
  Re: Does this confirm to ISO C? Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-09-11 17:18 -0400
  Re: Does this confirm to ISO C? jt@toerring.de (Jens Thoms Toerring) - 2011-09-11 21:22 +0000
  Re: Does this confirm to ISO C? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-09-11 22:58 +0100
    Re: Does this confirm to ISO C? Fred <fred.l.kleinschmidt@boeing.com> - 2011-09-12 07:53 -0700
      Re: Does this confirm to ISO C? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-09-12 20:01 +0100
        Re: Does this confirm to ISO C? Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-09-12 20:16 -0400
          Re: Does this confirm to ISO C? Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-09-15 14:40 +0300
  Re: Does this confirm to ISO C? Максим Фомин <maxim@maxim-fomin.ru> - 2011-09-12 23:41 -0700

csiph-web