473,493 Members | 2,265 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Pointers to standard library functions

This may well be implementation-defined or undefined behavior... if
so, then of course that's a good enough answer.

Consider the following situation:

/* file1.c */

int (*f)(const char *, const char *);

function1()
{
...
f=strcmp;
...
}

/* file2.c */

extern int (*f)(const char *, const char *);

/* function1 has definitely been called by now */
function2()
{
/* uses f */
}

Is the function2 guaranteed to invoke strcmp? In other words, are
addresses of standard library functions guaranteed to be constant
across different files?

(Obviously what I've presented looks silly - actually what I have is a
table of function pointers set up in one file that I want to access
from another, and some of them may be pointers to library functions.
The above is a simplification illustrating the question.)

May 26 '07 #1
7 1721
Fr************@googlemail.com wrote:
This may well be implementation-defined or undefined behavior... if
so, then of course that's a good enough answer.

Consider the following situation:

/* file1.c */

int (*f)(const char *, const char *);

function1()
{
...
f=strcmp;
...
}

/* file2.c */

extern int (*f)(const char *, const char *);

/* function1 has definitely been called by now */
function2()
{
/* uses f */
}

Is the function2 guaranteed to invoke strcmp? In other words, are
addresses of standard library functions guaranteed to be constant
across different files?
There will only one instance of any function with global scope.

--
Ian Collins.
May 26 '07 #2
Ian Collins wrote:
Fr************@googlemail.com wrote:
>This may well be implementation-defined or undefined behavior... if
so, then of course that's a good enough answer.

Consider the following situation:

/* file1.c */

int (*f)(const char *, const char *);

function1()
{
...
f=strcmp;
...
}

/* file2.c */

extern int (*f)(const char *, const char *);

/* function1 has definitely been called by now */
function2()
{
/* uses f */
}

Is the function2 guaranteed to invoke strcmp? In other words, are
addresses of standard library functions guaranteed to be constant
across different files?
There will only one instance of any function with global scope.
I should have added make sure the standard library functions in question
are implemented as functions and not as macros.

--
Ian Collins.
May 26 '07 #3
On May 26, 11:37 am, Ian Collins <ian-n...@hotmail.comwrote:
Ian Collins wrote:
Francine.Ne...@googlemail.com wrote:
This may well be implementation-defined or undefined behavior... if
so, then of course that's a good enough answer.
Consider the following situation:
/* file1.c */
int (*f)(const char *, const char *);
function1()
{
...
f=strcmp;
...
}
/* file2.c */
extern int (*f)(const char *, const char *);
/* function1 has definitely been called by now */
function2()
{
/* uses f */
}
Is the function2 guaranteed to invoke strcmp? In other words, are
addresses of standard library functions guaranteed to be constant
across different files?
There will only one instance of any function with global scope.

I should have added make sure the standard library functions in question
are implemented as functions and not as macros.
Which standard library functions are allowed to be implemented as
macros?
--
Ian Collins.

May 26 '07 #4

<Fr************@googlemail.comha scritto nel messaggio
news:11**********************@u30g2000hsc.googlegr oups.com...
On May 26, 11:37 am, Ian Collins <ian-n...@hotmail.comwrote:
>Ian Collins wrote:
Francine.Ne...@googlemail.com wrote:
This may well be implementation-defined or undefined behavior... if
so, then of course that's a good enough answer.
>Consider the following situation:
>/* file1.c */
>int (*f)(const char *, const char *);
>function1()
{
...
f=strcmp;
...
}
>/* file2.c */
>extern int (*f)(const char *, const char *);
>/* function1 has definitely been called by now */
function2()
{
/* uses f */
}
>Is the function2 guaranteed to invoke strcmp? In other words, are
addresses of standard library functions guaranteed to be constant
across different files?
There will only one instance of any function with global scope.

I should have added make sure the standard library functions in question
are implemented as functions and not as macros.

Which standard library functions are allowed to be implemented as
macros?
All of them (if they are, they must evalue each argument exactly
once, have enough parens, etc., that is the user needn't be aware
they are macros). The exception is that getc and putc may evalue
argument several times.
But there must also be an actual function (except for assert() and few
others).
Here it is irrelevant, since the fact that strcmp isn't followed by
a ( prevents the preprocessor to replace it with the macro, so the
actual function is used.
May 26 '07 #5
Ian Collins wrote:
Fr************@googlemail.com wrote:
>This may well be implementation-defined or undefined behavior... if
so, then of course that's a good enough answer.

[... module A aims a function pointer at strcmp, module B
uses the pointer ...]

Is the function2 guaranteed to invoke strcmp? In other words, are
addresses of standard library functions guaranteed to be constant
across different files?
There will only one instance of any function with global scope.
How can a program detect the number of "instances" of
a function? It can, extending Francine Neary's sample, form
a lot of pointers to a function and then compare them, and
for Standard library functions I believe they will always
compare equal (7.1.2p6 says Standard library function names
have external linkage, and equality should follow).

But pointer comparisons can only detect the particular
"instances" that the pointers point to, and can't tell whether
other "instances" are the same or are separate. That is,
there might be forty-two copies of strcmp() lying around in a
program, forty-one of them expanded in-line at the points of
call and another compiled separately to be a target for all
those pointer variables. I can't find any explicit permission
for an implementation to make Standard functions `inline', but
I can't find a prohibition, either.

Fifteen or more years ago there was a thread about whether
the expression `memcpy == memmove' could be true. I recall that
the debate went on for quite a while, but I don't recall it coming
to a definitive conclusion.

At any rate: Francine Neary's function pointer will at the
very least point to "a" strcmp, even if not to "the" strcmp.

--
Eric Sosman
es*****@acm-dot-org.invalid
May 26 '07 #6
In article <f3*********@tdi.cu.mi.it>, Army1987 <pl********@for.itwrote:
>
<Fr************@googlemail.comha scritto nel messaggio
news:11**********************@u30g2000hsc.googleg roups.com...
>Which standard library functions are allowed to be implemented as
macros?

All of them (if they are, they must evalue each argument exactly
once, have enough parens, etc., that is the user needn't be aware
they are macros). The exception is that getc and putc may evalue
argument several times.
In general, any macro masking a standard library function must act exactly
like the function it's masking, unless the standard specifically says
otherwise. (I think there's a blanket exemption for lacking sequence
points that would be in the function call, but verifying that would take
more than the quick grep of n869 I just made.)
(The fact that function-like macros don't get expanded unless the name
of the macro is followed by a '(' makes the "use function name as pointer
to that function" part of that trivial.)
>But there must also be an actual function (except for assert() and few
others).
assert isn't a standard library function; it's a macro defined by the
standard library.
dave

--
Dave Vandervies dj******@csclub.uwaterloo.ca
Well, it's logically consistent and interesting. That appears to be
all mathematicians need.
--James Riden in the scary devil monastery
May 26 '07 #7
dj******@caffeine.csclub.uwaterloo.ca (Dave Vandervies) writes:
[...]
In general, any macro masking a standard library function must act exactly
like the function it's masking, unless the standard specifically says
otherwise. (I think there's a blanket exemption for lacking sequence
points that would be in the function call, but verifying that would take
more than the quick grep of n869 I just made.)
[...]

Correct.

Quoting n1124 7.1.4p1:

...

Any function declared in a header may be additionally implemented
as a function-like macro defined in the header, so if a library
function is declared explicitly when its header is included, one
of the techniques shown below can be used to ensure the
declaration is not affected by such a macro.
...
Any invocation of a library function that is implemented as a
macro shall expand to code that evaluates each of its arguments
exactly once, fully protected by parentheses where necessary, so
it is generally safe to use arbitrary expressions as arguments.

with a footnote:

Such macros might not contain the sequence points that the
corresponding function calls do.

One interesting consequence is that the expression sin(x) + cos(x) may
invoke undefined behavior, since both sin and cos can modify errno
without an intervening sequence point.

--
Keith Thompson (The_Other_Keith) 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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
May 26 '07 #8

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

Similar topics

1
1845
by: stanlo | last post by:
Hello to all, i have a progam fragment here for the follow up of my project ; mathematicl expression. i don t want to use pointers, this is the fragment.my problem is there an alternative way of...
12
3447
by: pooja | last post by:
Hi I m pooja.I m working on a prj in C. I m unable to increment a char pointer in a file, it gives the same address as that of the first char.Eg if # is on loc 3000 then i,n ,c ,l,u,d,e are on...
9
5036
by: Mikhail Teterin | last post by:
Hello! I'd like to have a variable of a pointer-to-function type. The two possible values are of type (*)(FILE *) and (*)(void *). For example: getter = straight ? fgetc : gzgetc; nextchar...
11
429
by: Seven Kast USA | last post by:
hi What is use of pointerrs in c programming.is fasster than other types by KAST
39
19552
by: Martin Jørgensen | last post by:
Hi, I'm relatively new with C-programming and even though I've read about pointers and arrays many times, it's a topic that is a little confusing to me - at least at this moment: ---- 1)...
45
899
by: noridotjabi | last post by:
What is the purpose of the function pointer? Why do you need a pointer to a function. I cannot really think of any application where this is the only or even easiest solution to a problem. I'm...
92
4998
by: Jim Langston | last post by:
Someone made the statement in a newsgroup that most C++ programmers use smart pointers. His actual phrase was "most of us" but I really don't think that most C++ programmers use smart pointers,...
69
3123
by: Yee.Chuang | last post by:
When I began to learn C, My teacher told me that pointer is the most difficult part of C, it makes me afraid of it. After finishing C program class, I found that all the code I wrote in C contains...
160
5475
by: raphfrk | last post by:
Is this valid? int a; void *b; b = (void *)a; // b points to a b += 5*sizeof(*a); // b points to a a = 100;
0
7119
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,...
0
7157
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,...
1
6873
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...
0
7367
tracyyun
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...
1
4889
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...
0
3088
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...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1400
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 ...
0
285
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...

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.