Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > allowing a function to be called only from a specific function

Reply
Thread Tools

allowing a function to be called only from a specific function

 
 
junky_fellow@yahoo.co.in
Guest
Posts: n/a
 
      11-29-2006
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 ...

 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      11-29-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.
 
Reply With Quote
 
 
 
 
junky_fellow@yahoo.co.in
Guest
Posts: n/a
 
      11-29-2006


On Nov 29, 3:50 pm, Richard Heathfield <(E-Mail Removed)> wrote:
> (E-Mail Removed) 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 ?

 
Reply With Quote
 
mark_bluemel@pobox.com
Guest
Posts: n/a
 
      11-29-2006

(E-Mail Removed) wrote:
> On Nov 29, 3:50 pm, Richard Heathfield <(E-Mail Removed)> wrote:
> > (E-Mail Removed) 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.

 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      11-29-2006
(E-Mail Removed) wrote:
> On Nov 29, 3:50 pm, Richard Heathfield <(E-Mail Removed)> wrote:
> > (E-Mail Removed) 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++.

 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      11-29-2006
(E-Mail Removed) wrote:

>
> (E-Mail Removed) wrote:
>> On Nov 29, 3:50 pm, Richard Heathfield <(E-Mail Removed)> wrote:
>> > (E-Mail Removed) 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/

 
Reply With Quote
 
Duncan Muirhead
Guest
Posts: n/a
 
      11-29-2006
On Wed, 29 Nov 2006 04:11:43 -0800, mark_bluemel wrote:

>
> (E-Mail Removed) wrote:
>> On Nov 29, 3:50 pm, Richard Heathfield <(E-Mail Removed)> wrote:
>> > (E-Mail Removed) 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.
 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      11-29-2006
In article <(E-Mail Removed) .com>,
(E-Mail Removed) <(E-Mail Removed)> 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.
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      11-29-2006
(E-Mail Removed) 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
(E-Mail Removed)lid
 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      11-29-2006
(E-Mail Removed) <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Allowing specific elements from another namespace... Simon Brooke XML 2 05-27-2008 05:22 PM
PIX- limit web browsing for a specific machine, allowing all others. barret bonden Cisco 2 01-15-2008 08:35 PM
allowing only certain computers acces to the wirless network =?Utf-8?B?cGhpbA==?= Wireless Networking 2 12-29-2005 09:39 PM
Allowing Specific MAC Address Access Albie Cisco 1 11-15-2005 03:01 AM
Allowing function pointer code to work on a member function Enquiries, Hopkins Research C++ 11 10-29-2005 07:41 PM



Advertisments