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
24 1369
<ju**********@y ahoo.co.inwrote in message
news:11******** **************@ j72g2000cwa.goo glegroups.com.. .
>
On Nov 29, 3:50 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
>junky_fel...@y ahoo.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 ?
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.
Chris Dollin wrote:
ju**********@ya hoo.co.in 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.
santosh wrote:
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 ?
I think you want C++.
C++ does not support this either.
"Malcolm" <re*******@btin ternet.comwrite s:
[...]
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_Keit h) ks***@mib.org <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.
Old Wolf wrote:
Chris Dollin wrote:
ju**********@ya hoo.co.in 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. ju**********@ya hoo.co.in 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
}
Harald van D?k wrote:
Old Wolf wrote:
>Chris Dollin wrote:
>>ju**********@ya hoo.co.in 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>
Old Wolf wrote:
Chris Dollin wrote:
ju**********@ya hoo.co.in 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/
CBFalconer wrote:
Harald van D?k wrote:
Old Wolf wrote:
Chris Dollin wrote: ju**********@ya hoo.co.in 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.
Old Wolf <ol*****@inspir e.net.nzwrote:
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)gma il.com | don't, I need to know. Flames welcome. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
}
|
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..
|
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.
|
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...
|
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.
| |
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...
|
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
|
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)
{
|
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...
|
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...
|
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,...
| |
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...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
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
| |