allowing a function to be called only from a specific function

Discussion in 'C Programming' started by junky_fellow@yahoo.co.in, Nov 29, 2006.

  1. Guest

    Hi,

    Is there any way, by which we can limit a specific function to be
    called
    only from a specific function ? I dont know the advantage of this.
    Someone
    asked this question from me in an interview.

    thanks for any help ...
    , Nov 29, 2006
    #1
    1. Advertising

  2. said:

    > Hi,
    >
    > Is there any way, by which we can limit a specific function to be
    > called
    > only from a specific function ? I dont know the advantage of this.
    > Someone
    > asked this question from me in an interview.
    >
    > thanks for any help ...


    For ease of reference, let's call them foo() and bar(), and you want it to
    be impossible to call bar() except from foo().

    Put foo() and bar() in foo.c. Make bar() static:

    static int bar(double *, void ***, char, unsigned long);

    Compile foo.c to an object file, and publish the object file, together with
    the interface spec (foo.h, which need not and indeed should not even
    mention bar() at all), to your users. Don't give them the source file. :)
    They don't need it, and it'd only get them poking around in the guts of
    bar(), which is presumably what you're trying to prevent.

    Provided you do not export bar()'s address from foo() - and if you don't
    know what I'm talking about, it's extremely unlikely that you'd do this by
    accident! - then you will now only be able to call bar() from foo().

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
    Richard Heathfield, Nov 29, 2006
    #2
    1. Advertising

  3. Guest

    On Nov 29, 3:50 pm, Richard Heathfield <> wrote:
    > said:
    >
    > > Hi,

    >
    > > Is there any way, by which we can limit a specific function to be
    > > called
    > > only from a specific function ? I dont know the advantage of this.
    > > Someone
    > > asked this question from me in an interview.

    >
    > > thanks for any help ...For ease of reference, let's call them foo() and bar(), and you want it to

    > be impossible to call bar() except from foo().
    >
    > Put foo() and bar() in foo.c. Make bar() static:
    >
    > static int bar(double *, void ***, char, unsigned long);
    >
    > Compile foo.c to an object file, and publish the object file, together with
    > the interface spec (foo.h, which need not and indeed should not even
    > mention bar() at all), to your users. Don't give them the source file. :)
    > They don't need it, and it'd only get them poking around in the guts of
    > bar(), which is presumably what you're trying to prevent.
    >
    > Provided you do not export bar()'s address from foo() - and if you don't
    > know what I'm talking about, it's extremely unlikely that you'd do this by
    > accident! - then you will now only be able to call bar() from foo().
    >

    In fact, I also suggested the same thing, but he said that there could
    be
    multilpe functions (say f1(), f2(), f3(), f4() etc) in the some file
    and
    f1() should be allowed to be called only from f2() and not from any
    other function in that file or in some other file. Is there any way of
    doing it ?
    , Nov 29, 2006
    #3
  4. Guest

    wrote:
    > On Nov 29, 3:50 pm, Richard Heathfield <> wrote:
    > > said:
    > >
    > > > Hi,

    > >
    > > > Is there any way, by which we can limit a specific function to be
    > > > called only from a specific function ?

    ....
    > > Put foo() and bar() in foo.c. Make bar() static:
    > >
    > > static int bar(double *, void ***, char, unsigned long);
    > >
    > > Compile foo.c to an object file, and publish the object file, together with
    > > the interface spec (foo.h, which need not and indeed should not even
    > > mention bar() at all), to your users. Don't give them the source file. :)
    > > They don't need it, and it'd only get them poking around in the guts of
    > > bar(), which is presumably what you're trying to prevent.
    > >
    > > Provided you do not export bar()'s address from foo() - and if you don't
    > > know what I'm talking about, it's extremely unlikely that you'd do this by
    > > accident! - then you will now only be able to call bar() from foo().
    > >

    > In fact, I also suggested the same thing, but he said that there could
    > be multilpe functions (say f1(), f2(), f3(), f4() etc) in the some file
    > and f1() should be allowed to be called only from f2() and not from any
    > other function in that file or in some other file. Is there any way of
    > doing it ?


    Not within the specification of the language, I wouldn't think.

    There could be platform-specific ways, involving all manner of horrid
    sub-rosa knowledge.
    , Nov 29, 2006
    #4
  5. santosh Guest

    wrote:
    > On Nov 29, 3:50 pm, Richard Heathfield <> wrote:
    > > said:
    > >
    > > > Hi,
    > > > Is there any way, by which we can limit a specific function to be
    > > > called only from a specific function ? I dont know the advantage of this.
    > > > Someone asked this question from me in an interview.
    > > >
    > > > thanks for any help ...

    > >
    > > For ease of reference, let's call them foo() and bar(), and you want it to
    > > be impossible to call bar() except from foo().
    > >
    > > Put foo() and bar() in foo.c. Make bar() static:

    <snip>
    > > Provided you do not export bar()'s address from foo() - and if you don't
    > > know what I'm talking about, it's extremely unlikely that you'd do this by
    > > accident! - then you will now only be able to call bar() from foo().
    > >

    > In fact, I also suggested the same thing, but he said that there could
    > be multilpe functions (say f1(), f2(), f3(), f4() etc) in the some file
    > and f1() should be allowed to be called only from f2() and not from any
    > other function in that file or in some other file. Is there any way of
    > doing it ?


    I think you want C++.
    santosh, Nov 29, 2006
    #5
  6. Chris Dollin Guest

    wrote:

    >
    > wrote:
    >> On Nov 29, 3:50 pm, Richard Heathfield <> wrote:
    >> > said:
    >> >
    >> > > Hi,
    >> >
    >> > > Is there any way, by which we can limit a specific function to be
    >> > > called only from a specific function ?

    > ...
    >> > Put foo() and bar() in foo.c. Make bar() static:
    >> >
    >> > static int bar(double *, void ***, char, unsigned long);
    >> >
    >> > Compile foo.c to an object file, and publish the object file, together with
    >> > the interface spec (foo.h, which need not and indeed should not even
    >> > mention bar() at all), to your users. Don't give them the source file. :)
    >> > They don't need it, and it'd only get them poking around in the guts of
    >> > bar(), which is presumably what you're trying to prevent.
    >> >
    >> > Provided you do not export bar()'s address from foo() - and if you don't
    >> > know what I'm talking about, it's extremely unlikely that you'd do this by
    >> > accident! - then you will now only be able to call bar() from foo().
    >> >

    >> In fact, I also suggested the same thing, but he said that there could
    >> be multilpe functions (say f1(), f2(), f3(), f4() etc) in the some file
    >> and f1() should be allowed to be called only from f2() and not from any
    >> other function in that file or in some other file. Is there any way of
    >> doing it ?

    >
    > Not within the specification of the language, I wouldn't think.


    /* insert any necessary forward declarations here
    (but, of course, not for f1)
    */

    f3() { ... }

    f4() { ... }

    f5() { ... }

    static f1() { ... }

    f2() { ... f1() ... }

    --
    Chris "subtle, like a barrel" Dollin
    "Our future looks secure, but it's all out of our hands"
    - Magenta, /Man and Machine/
    Chris Dollin, Nov 29, 2006
    #6
  7. On Wed, 29 Nov 2006 04:11:43 -0800, mark_bluemel wrote:

    >
    > wrote:
    >> On Nov 29, 3:50 pm, Richard Heathfield <> wrote:
    >> > said:
    >> >
    >> > > Hi,
    >> >
    >> > > Is there any way, by which we can limit a specific function to be
    >> > > called only from a specific function ?

    > ...

    <snip>
    > Not within the specification of the language, I wouldn't think.
    >
    > There could be platform-specific ways, involving all manner of horrid
    > sub-rosa knowledge.

    Off Topic:
    Or you could use a specific compiler's language extension, for example gcc
    allows nested functions.
    Duncan Muirhead, Nov 29, 2006
    #7
  8. In article <>,
    <> wrote:

    >In fact, I also suggested the same thing, but he said that there
    >could be multilpe functions (say f1(), f2(), f3(), f4() etc) in the
    >some file and f1() should be allowed to be called only from f2() and
    >not from any other function in that file or in some other file. Is
    >there any way of doing it ?


    It's hard to make much sense of this without knowing the purpose of
    the constraint. If someone can change f3 so that it tries to call f1,
    they can edit the rest of the file to remove any mechanism you have
    used to prevent it (unless you have some magic editor that only allows
    you to change part of the file, in which case it could enforce the
    rule about calling f1 too).

    If you suppose that the aim is to prevent someone inadvertently calling
    it in a context where it would be inappropriate, you might do it by
    naming the function "f1_but_do_not_call_this_function", and putting
    #define f1 f1_but_do_not_call_this_function at the beginning of f2
    and #undef f1 at the end of it.

    I would probably just put a comment on f1 describing the circumstances
    in which it was safe to call it.

    -- Richard
    --
    "Consideration shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.
    Richard Tobin, Nov 29, 2006
    #8
  9. Eric Sosman Guest

    wrote:
    > [...]
    > In fact, I also suggested the same thing, but he said that there could
    > be
    > multilpe functions (say f1(), f2(), f3(), f4() etc) in the some file
    > and
    > f1() should be allowed to be called only from f2() and not from any
    > other function in that file or in some other file. Is there any way of
    > doing it ?


    Make f1() static and place it next-to-last in the file,
    right before f2(). Threaten to crush the gizzards of anyone
    who writes a forward declaration of f1() earlier in the file.

    Make f1() static and place it first in the file, right
    before f2(). Follow f2 with `#define f1 >* die, slime! *<'
    and threaten to crush the gizzards of anyone who writes an
    `#undef f1'.

    Give f1() external linkage, publish its declaration in an
    appropriate header file, and threaten to crush the gizzards
    of anyone who calls it from anyplace except within f2().

    There's nothing I can think of that doesn't ultimately
    rest on someone's unwillingness to have his gizzards crushed.
    Compilers are not nannies.

    --
    Eric Sosman
    lid
    Eric Sosman, Nov 29, 2006
    #9
  10. <> wrote:

    > In fact, I also suggested the same thing, but he said that there could
    > be
    > multilpe functions (say f1(), f2(), f3(), f4() etc) in the some file
    > and
    > f1() should be allowed to be called only from f2() and not from any
    > other function in that file or in some other file. Is there any way of
    > doing it ?


    Not really, but you could do something like this anyway - document
    f1() to be called by a macro INVOKE_F1 and only define the macro to
    correctly invoke f1() inside f2():

    #include <stdio.h>
    #include <stdlib.h>

    #define INVOKE_F1() fprintf( stderr, "Invalid invocation of f1\n" ); abort();

    void f1() {
    printf( "Invoked f1()\n" );
    }

    void f2() {
    #undef INVOKE_F1
    #define INVOKE_F1() f1()
    INVOKE_F1();
    #undef INVOKE_F1
    #define INVOKE_F1() fprintf( stderr, "Invalid invocation of f1\n" ); abort();
    }

    void f3() {
    INVOKE_F1();
    }

    int main(void) {
    f2();
    f3();
    return 0; /* Not reached. */
    }

    --
    C. Benson Manica | I *should* know what I'm talking about - if I
    cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, Nov 29, 2006
    #10
  11. Malcolm Guest

    <> wrote in message
    news:...
    >
    >
    > On Nov 29, 3:50 pm, Richard Heathfield <> wrote:
    >> said:
    >>
    >> > Hi,

    >>
    >> > Is there any way, by which we can limit a specific function to be
    >> > called
    >> > only from a specific function ? I dont know the advantage of this.
    >> > Someone
    >> > asked this question from me in an interview.

    >>
    >> > thanks for any help ...For ease of reference, let's call them foo() and
    >> > bar(), and you want it to

    >> be impossible to call bar() except from foo().
    >>
    >> Put foo() and bar() in foo.c. Make bar() static:
    >>
    >> static int bar(double *, void ***, char, unsigned long);
    >>
    >> Compile foo.c to an object file, and publish the object file, together
    >> with
    >> the interface spec (foo.h, which need not and indeed should not even
    >> mention bar() at all), to your users. Don't give them the source file.
    >> :)
    >> They don't need it, and it'd only get them poking around in the guts of
    >> bar(), which is presumably what you're trying to prevent.
    >>
    >> Provided you do not export bar()'s address from foo() - and if you don't
    >> know what I'm talking about, it's extremely unlikely that you'd do this
    >> by
    >> accident! - then you will now only be able to call bar() from foo().
    >>

    > In fact, I also suggested the same thing, but he said that there could
    > be
    > multilpe functions (say f1(), f2(), f3(), f4() etc) in the some file
    > and
    > f1() should be allowed to be called only from f2() and not from any
    > other function in that file or in some other file. Is there any way of
    > doing it ?
    >

    The interviewer is out of date.
    Some old-time C compilers allowed the use of local function definitons

    int foo()
    {
    int bar(int, int);

    return bar(1,2);
    }

    int bar()
    int x,
    int y
    {
    return 0;
    }

    bar is now local to foo(). I think I've got that right. It never caught on
    and now static is the universally-approved way of restricting access to a
    function.
    --
    www.personal.leeds.ac.uk/~bgy1mm
    freeware games to download.
    Malcolm, Nov 29, 2006
    #11
  12. Old Wolf Guest

    Chris Dollin wrote:
    > > wrote:
    > >> > > Is there any way, by which we can limit a specific function to be
    > >> > > called only from a specific function ?

    >
    > f3() { ... }
    >
    > f4() { ... }
    >
    > f5() { ... }
    >
    > static f1() { ... }
    >
    > f2() { ... f1() ... }


    f3() can still write:
    int f1();

    to declare f1, and then call it.
    Old Wolf, Nov 29, 2006
    #12
  13. Old Wolf Guest

    santosh wrote:
    > wrote:
    > > In fact, I also suggested the same thing, but he said that there could
    > > be multilpe functions (say f1(), f2(), f3(), f4() etc) in the some file
    > > and f1() should be allowed to be called only from f2() and not from any
    > > other function in that file or in some other file. Is there any way of
    > > doing it ?

    >
    > I think you want C++.


    C++ does not support this either.
    Old Wolf, Nov 29, 2006
    #13
  14. "Malcolm" <> writes:
    [...]
    > The interviewer is out of date.
    > Some old-time C compilers allowed the use of local function definitons
    >
    > int foo()
    > {
    > int bar(int, int);
    >
    > return bar(1,2);
    > }
    >
    > int bar()
    > int x,
    > int y
    > {
    > return 0;
    > }
    >
    > bar is now local to foo(). I think I've got that right. It never caught on
    > and now static is the universally-approved way of restricting access to a
    > function.


    That's not a local function definition; it's merely a local function
    declaration. It's still perfectly legal, just as it always has been,
    though it's widely considered to be poor style. (This specific
    example would have been illegal before the introduction of prototypes,
    but a local "int bar();" would have been legal.)

    (There are other problems with the code.)

    A local function *definition* would look like this:

    int foo(void)
    {
    int bar(int x, int y)
    {
    return 0;
    }
    return bar(1, 2);
    }

    This has never been legal in standard C. <OT>gcc allows it as an
    extension.</OT>

    Nested functions make the compiler's job more difficult if it has to
    support an inner function referring to an object declared in an outer
    function.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Nov 30, 2006
    #14
  15. Old Wolf wrote:
    > Chris Dollin wrote:
    > > > wrote:
    > > >> > > Is there any way, by which we can limit a specific function to be
    > > >> > > called only from a specific function ?

    > >
    > > f3() { ... }
    > >
    > > f4() { ... }
    > >
    > > f5() { ... }
    > >
    > > static f1() { ... }
    > >
    > > f2() { ... f1() ... }

    >
    > f3() can still write:
    > int f1();
    >
    > to declare f1, and then call it.


    When f1 is defined static, it cannot.
    =?utf-8?B?SGFyYWxkIHZhbiBExLNr?=, Nov 30, 2006
    #15
  16. wrote:
    > Hi,
    >
    > Is there any way, by which we can limit a specific function to be
    > called
    > only from a specific function ? I dont know the advantage of this.
    > Someone
    > asked this question from me in an interview.
    >
    > thanks for any help ...


    Lotsa ways to do it:

    #define f5 if( strcmp( __FUNC__, "f2" ) == 0 ) f5 (); else
    printf("cant call f2 from here");

    or if your compiler doesnt have __FUNC__, test for __LINE__

    or:

    #define f5(x) f5 ( __FUNC__, x )
    ... then have f5 do the strcmp() for the function names its allowed to
    be called from.

    or:

    in global scope have: char f5;

    so nobody can call f5, except f2 is:

    void f2( void ){ void f5(); // or whatever, now f5 can be called but
    only from in here
    }
    Ancient_Hacker, Nov 30, 2006
    #16
  17. CBFalconer Guest

    Harald van D?k wrote:
    > Old Wolf wrote:
    >> Chris Dollin wrote:
    >>> wrote:
    >>>
    >>>> Is there any way, by which we can limit a specific function to be
    >>>> called only from a specific function ?
    >>>
    >>> f3() { ... }
    >>>
    >>> f4() { ... }
    >>>
    >>> f5() { ... }
    >>>
    >>> static f1() { ... }
    >>>
    >>> f2() { ... f1() ... }

    >>
    >> f3() can still write:
    >> int f1();
    >>
    >> to declare f1, and then call it.

    >
    > When f1 is defined static, it cannot.


    Yes it can. The 'static' only prevents visibility outside the
    compilation unit.

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>
    CBFalconer, Nov 30, 2006
    #17
  18. Chris Dollin Guest

    Old Wolf wrote:

    > Chris Dollin wrote:
    >> > wrote:
    >> >> > > Is there any way, by which we can limit a specific function to be
    >> >> > > called only from a specific function ?

    >>
    >> f3() { ... }
    >>
    >> f4() { ... }
    >>
    >> f5() { ... }
    >>
    >> static f1() { ... }
    >>
    >> f2() { ... f1() ... }

    >
    > f3() can still write:
    > int f1();
    >
    > to declare f1, and then call it.


    No solution - not even the ones in languages which support interesting
    visibility rules - is robust against source editing.

    --
    Chris "subtle, like a barrel" Dollin
    "A facility for quotation covers the absence of original thought." /Gaudy Night/
    Chris Dollin, Nov 30, 2006
    #18
  19. CBFalconer wrote:
    > Harald van D?k wrote:
    > > Old Wolf wrote:
    > >> Chris Dollin wrote:
    > >>> wrote:
    > >>>
    > >>>> Is there any way, by which we can limit a specific function to be
    > >>>> called only from a specific function ?
    > >>>
    > >>> f3() { ... }
    > >>>
    > >>> f4() { ... }
    > >>>
    > >>> f5() { ... }
    > >>>
    > >>> static f1() { ... }
    > >>>
    > >>> f2() { ... f1() ... }
    > >>
    > >> f3() can still write:
    > >> int f1();
    > >>
    > >> to declare f1, and then call it.

    > >
    > > When f1 is defined static, it cannot.

    >
    > Yes it can. The 'static' only prevents visibility outside the
    > compilation unit.


    No, it cannot. f3 is defined before f1 is declared. You can refer to a
    static function or variable with an extern declaration if and only if
    the a static declaration is already in scope, and you cannot use static
    on a function declaration with block scope.
    =?utf-8?B?SGFyYWxkIHZhbiBExLNr?=, Nov 30, 2006
    #19
  20. Old Wolf <> wrote:

    > santosh wrote:


    > > I think you want C++.


    > C++ does not support this either.


    Right, although he may have been thinking of friends and similar
    things, which can do something like OP wanted, albeit for classes and
    not functions as was required.

    --
    C. Benson Manica | I *should* know what I'm talking about - if I
    cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, Nov 30, 2006
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?anNhbGU=?=

    Problem with asp.net app only allowing 1 user at a time

    =?Utf-8?B?anNhbGU=?=, Dec 7, 2004, in forum: ASP .Net
    Replies:
    7
    Views:
    424
    John M Deal
    Dec 7, 2004
  2. Aaron

    Only allowing certain html tags

    Aaron, Jan 22, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    390
    Aaron
    Jan 22, 2006
  3. Helge Preuss
    Replies:
    2
    Views:
    430
    tom_usenet
    Jul 22, 2004
  4. Ali
    Replies:
    4
    Views:
    343
    Grant Edwards
    Jun 23, 2005
  5. Simon Brooke
    Replies:
    2
    Views:
    380
    Joseph J. Kesselman
    May 27, 2008
Loading...

Share This Page