473,836 Members | 1,809 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

allowing a function to be called only from a specific function

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 '06 #1
24 1349
ju**********@ya hoo.co.in 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.
Nov 29 '06 #2


On Nov 29, 3:50 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
junky_fel...@ya hoo.co.in 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 '06 #3

ju**********@ya hoo.co.in wrote:
On Nov 29, 3:50 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
junky_fel...@ya hoo.co.in 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 '06 #4
ju**********@ya hoo.co.in wrote:
On Nov 29, 3:50 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
junky_fel...@ya hoo.co.in 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++.

Nov 29 '06 #5
ma**********@po box.com wrote:
>
ju**********@ya hoo.co.in wrote:
>On Nov 29, 3:50 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
junky_fel...@ya hoo.co.in 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/

Nov 29 '06 #6
In article <11************ **********@j72g 2000cwa.googleg roups.com>,
ju**********@ya hoo.co.in <ju**********@y ahoo.co.inwrote :
>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_funct ion", and putting
#define f1 f1_but_do_not_c all_this_functi on 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
--
"Considerat ion shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Nov 29 '06 #7
On Wed, 29 Nov 2006 04:11:43 -0800, mark_bluemel wrote:
>
ju**********@ya hoo.co.in wrote:
>On Nov 29, 3:50 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
junky_fel...@ya hoo.co.in 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.
Nov 29 '06 #8
ju**********@ya hoo.co.in 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
es*****@acm-dot-org.invalid
Nov 29 '06 #9
ju**********@ya hoo.co.in <ju**********@y ahoo.co.inwrote :
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)gma il.com | don't, I need to know. Flames welcome.
Nov 29 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
2111
by: Dave | last post by:
Hello NG, If a particular dynamic library gets loaded, I need to ensure that a function in that library gets executed on startup (and I want the mechanism to use only Standard C++). Here's what I've got: void func() { // This is the function that must be executed }
11
2302
by: Enquiries, Hopkins Research | last post by:
Hi all I have a conundrum that is puzzling me. I have a large codebase in C that I am converting to C++ as fast as possible (i.e. slowly because I keep learning new idioms and stumbling with C++ 'features'). One part of the C code is some optimisation functions that expect a pointer to a function which is sent an array of doubles and returns a double i.e. simplified..
4
3638
by: anonymous | last post by:
Thanks your reply. The article I read is from www.hakin9.org/en/attachments/stackoverflow_en.pdf. And you're right. I don't know it very clearly. And that's why I want to understand it; for it's useful to help me to solve some basic problem which I may not perceive before. I appreciate your help, sincerely.
7
2922
by: jsale | last post by:
I'm currently using ASP.NET with VS2003 and SQL Server 2003. The ASP.NET app i have made is running on IIS v6 and consists of a number of pages that allow the user to read information from the database into classes, which are used throughout the application. I have made class collections which, upon reading from the DB, create an instance of the class and store the DB values in there temporarily. My problem is that if user1 looks at...
14
1853
by: Mr Newbie | last post by:
I am often in the situation where I want to act on the result of a function, but a simple boolean is not enough. For example, I may have a function called isAuthorised ( User, Action ) as ????? OK, this function may return a boolean, and if this is true, then no message back is really required, but if it fails then some supporting message needs to be returned to the calling code. As I see it there are a few options.
3
3662
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules' that takes arguments of type (void *) because the ADT must be able to deal with any type of data. In my actual code, I will code the function to take arguments of their real types, then when I pass this pointer through an interface function, I...
89
6093
by: Cuthbert | last post by:
After compiling the source code with gcc v.4.1.1, I got a warning message: "/tmp/ccixzSIL.o: In function 'main';ex.c: (.text+0x9a): warning: the 'gets' function is dangerous and should not be used." Could anybody tell me why gets() function is dangerous?? Thank you very much. Cuthbert
28
4349
by: Larax | last post by:
Best explanation of my question will be an example, look below at this simple function: function SetEventHandler(element) { // some operations on element element.onclick = function(event) {
6
5936
by: RandomElle | last post by:
Hi there I'm hoping someone can help me out with the use of the Eval function. I am using Access2003 under WinXP Pro. I can successfully use the Eval function and get it to call any function with or without parms. I know that any function that is passed to Eval() must be declared Public. It can be a Sub or Function, as long as it's Public. I even have it where the "function" evaluated by Eval can be in a form (class) module or in a standard...
0
9810
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9654
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10818
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10526
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9348
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7770
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4436
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3094
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.