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


Groups > comp.lang.c > #58058

Re: Is there any way to make a function local to two TUs without including it as static in both?

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: Is there any way to make a function local to two TUs without including it as static in both?
Date 2015-03-10 10:37 -0700
Organization None to speak of
Message-ID <874mps3gae.fsf@kst-u.example.com> (permalink)
References <5bnrfahtskm5osi399eb9954bleglspe2k@4ax.com> <87lhj62bt3.fsf@kst-u.example.com> <mdl3hd$gtu$1@dont-email.me> <87h9tt2afx.fsf@kst-u.example.com> <mdn0rh$kdn$1@dont-email.me>

Show all headers | View raw


Richard Heathfield <rjh@cpax.org.uk> writes:
> On 10/03/15 14:28, Keith Thompson wrote:
>> Richard Heathfield <rjh@cpax.org.uk> writes:
>>> On 09/03/15 19:47, Keith Thompson wrote:
>>>> DSF <notavalid@address.here> writes:
>>>>> Is there any way to make a function local to two TUs without including
>>>>> it as static in both?
>>>> [...]
>>>>
>>>> You can declare it in a header, and #include that header only in those
>>>> two translation units.  Would tnat satisfy your requirements?
>>>
>>> It might, but it might not. Consider this function:
>>>
>>> static unsigned char uccount(void)
>>> {
>>>   static unsigned char count;
>>>   return count++;
>>> }
>>
>> I didn't suggest defining it as static.  Just define it normally,
>> declare it in a header, and #include that header only where it's needed.
>
> I thought you did, but evidently I was wrong. Once the word 'static'
> is in one's head, it tends to stay there (for etymological reasons, no
> doubt). Sorry about that.
>
> Whilst your approach is of course entirely workable in Real Life, it
> doesn't prevent other TUs from accessing the function, so it doesn't
> (quite) answer the OP's original question.

*Nothing* can reliably prevent other translation units from accessing
a function.  Even if it's defined as static, you can always remove
the static keyword and recompile, or you can arrange to make its
address available.  If it's not declared in a header, you can add
your own declaration.

There are no secure mechanisms in C for protecting parts of a
program from other parts of the same program.

It can certainly make sense, in the interests of information
hiding, to make it *inconvenient* for other TUs to call a function.
The question is, how far do you go to accomplish this goal?

One possibility, I suppose, is to add a step to the build process
(Makefile?) that detects any attempt to #include the appropriate
header other than in the two permitted source files.  Of course
it's still easy enough to edit the Makefile.  Coding standards can
help -- if they're enforced and/or everyone actually follows them.

I think we're making assumptions about what DSF meant by "local"
(easy to do, since we all know what the word means, but we don't
all mean the same thing, and the standard doesn't define or use the
word in a way that's particularly helpful to the current discussion).

DSF, if you're still here: What exactly do you mean by "local"?

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

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


Thread

Is there any way to make a function local to two TUs without including it as static in both? DSF <notavalid@address.here> - 2015-03-09 15:02 -0400
  Re: Is there any way to make a function local to two TUs without including it as static in both? James Kuyper <jameskuyper@verizon.net> - 2015-03-09 15:22 -0400
    Re: Is there any way to make a function local to two TUs without including it as static in both? Richard Heathfield <rjh@cpax.org.uk> - 2015-03-09 19:34 +0000
  Re: Is there any way to make a function local to two TUs without including it as static in both? Keith Thompson <kst-u@mib.org> - 2015-03-09 12:47 -0700
    Re: Is there any way to make a function local to two TUs without including it as static in both? Richard Heathfield <rjh@cpax.org.uk> - 2015-03-09 21:33 +0000
      Re: Is there any way to make a function local to two TUs without including it as static in both? Keith Thompson <kst-u@mib.org> - 2015-03-10 07:28 -0700
        Re: Is there any way to make a function local to two TUs without including it as static in both? Richard Heathfield <rjh@cpax.org.uk> - 2015-03-10 14:59 +0000
          Re: Is there any way to make a function local to two TUs without including it as static in both? Keith Thompson <kst-u@mib.org> - 2015-03-10 10:37 -0700
            Re: Is there any way to make a function local to two TUs without including it as static in both? Richard Heathfield <rjh@cpax.org.uk> - 2015-03-10 18:45 +0000
  Re: Is there any way to make a function local to two TUs without including it as static in both? Lőrinczy Zsigmond <nospam@for.me> - 2015-03-09 21:04 +0100
  Re: Is there any way to make a function local to two TUs without including it as static in both? August Karlstrom <fusionfile@gmail.com> - 2015-03-09 21:39 +0100
  Re: Is there any way to make a function local to two TUs without including it as static in both? Kaz Kylheku <kaz@kylheku.com> - 2015-03-09 21:20 +0000
  Re: Is there any way to make a function local to two TUs without including it as static in both? Robert Wessel <robertwessel2@yahoo.com> - 2015-03-09 19:23 -0500
    Re: Is there any way to make a function local to two TUs without including it as static in both? wssimms@gmail.com - 2015-03-09 21:42 -0700
      Re: Is there any way to make a function local to two TUs without including it as static in both? Robert Wessel <robertwessel2@yahoo.com> - 2015-03-10 04:52 -0500
    Re: Is there any way to make a function local to two TUs without including it as static in both? Keith Thompson <kst-u@mib.org> - 2015-03-10 07:35 -0700
      Re: Is there any way to make a function local to two TUs without including it as static in both? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-03-10 12:53 -0700
  Re: Is there any way to make a function local to two TUs without including it as static in both? Les Cargill <lcargill99@comcast.com> - 2015-03-09 19:58 -0500
    Re: Is there any way to make a function local to two TUs without including it as static in both? Tim Rentsch <txr@alumni.caltech.edu> - 2015-03-24 13:06 -0700
      Re: Is there any way to make a function local to two TUs without including it as static in both? Les Cargill <lcargill99@comcast.com> - 2015-03-24 20:03 -0500
        Re: Is there any way to make a function local to two TUs without including it as static in both? James Kuyper <jameskuyper@verizon.net> - 2015-03-25 10:26 -0400
        Re: Is there any way to make a function local to two TUs without including it as static in both? Tim Rentsch <txr@alumni.caltech.edu> - 2015-03-25 10:36 -0700
          Re: Is there any way to make a function local to two TUs without including it as static in both? Kaz Kylheku <kaz@kylheku.com> - 2015-03-25 17:47 +0000
            Re: Is there any way to make a function local to two TUs without including it as static in both? Tim Rentsch <txr@alumni.caltech.edu> - 2015-03-25 16:03 -0700
      Re: Is there any way to make a function local to two TUs without including it as static in both? Tim Rentsch <txr@alumni.caltech.edu> - 2015-03-25 10:28 -0700
        Re: Is there any way to make a function local to two TUs without including it as static in both? Kaz Kylheku <kaz@kylheku.com> - 2015-03-25 17:46 +0000
          [OT] Re: meaning of "signature" in other languages Tim Rentsch <txr@alumni.caltech.edu> - 2015-03-25 16:01 -0700
        Re: Is there any way to make a function local to two TUs without including it as static in both? Tim Rentsch <txr@alumni.caltech.edu> - 2015-03-28 09:05 -0700

csiph-web