473,856 Members | 1,485 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

calling my custom function same as library function

Hi,

I would like to call one of my functions the exact name as an existing
C library function (for example K&R2 exercises asks me to make an atof
function). If I don't include the header with the declaration for the
C library function (stdlib.h in this case) then define/declare my own
function with the same name, am I safe? It seems to work on my
compiler, but I wonder if this is portable or even acceptable? thx

Nov 14 '05 #1
19 2522
In article <11************ **********@l41g 2000cwc.googleg roups.com>,
Deniz Bahar <de*********@gm ail.com> wrote:
Hi,

I would like to call one of my functions the exact name as an existing
C library function (for example K&R2 exercises asks me to make an atof
function). If I don't include the header with the declaration for the
C library function (stdlib.h in this case) then define/declare my own
function with the same name, am I safe? It seems to work on my
compiler, but I wonder if this is portable or even acceptable? thx


Yes, but you can't talk about it here.

Nov 14 '05 #2
In article <11************ **********@l41g 2000cwc.googleg roups.com>,
Deniz Bahar <de*********@gm ail.com> wrote:
:I would like to call one of my functions the exact name as an existing
:C library function (for example K&R2 exercises asks me to make an atof
:function). If I don't include the header with the declaration for the
:C library function (stdlib.h in this case) then define/declare my own
:function with the same name, am I safe?

Library function names are not 'reserved'.

If your function has the same prototype as the existing one, then
there is no problem including the header file. The standard
header files only give the linkage information for the cases where
you -do- use the library function: the header files do not in
any way "bring in" the library functions. When you use the library
functions, it is the linker that puts them into the program address
space, not the header files.

:It seems to work on my
:compiler, but I wonder if this is portable or even acceptable? thx

It is portable to any system I have ever heard of, and it is
often used. It is a fundamental mechanism by which functionality
can be extended, such as for replacing the malloc() function with
one that is more efficient or which provides debugging hooks.

Exact linker details differ, but the general practice is that
the code for the standard C library functions is stored in an
object "library", and that the linker will only pull in references
to those library functions for which it has unsatisfied references.
When you provide your own version of a library function, then code
that calls upon that function will find your definition of that
name and will use that: there will not be any unsatisfied references
to the name and so the linker will not try to reference the library
routine by that name.
There are two major competing linkage paradigms:

- In one of them, the order that the files are named is unimportant: the
linker examines all the user-provided object files, finds all the
definitions and unsatisfied references to routines, and to the greatest
extent possible uses the definitions from -any- of the given object
files. The linker then only pulls in those library routines which
there is a reference to but which were not defined by the user.
This paradigm requires at least two passes over the object files,
once to find out what all is defined in any user-provided file, and the
second time to do the linking.

- In the second linker paradigm, the order of the files is important.
The linker proceeds from left [first on the line] towards the right
[last on the line], accumulating definitions and unsatisfied
references as it goes. At any point, when there is an unsatisfied
reference to a function and a definition of that function is seen,
the definition is used to satisfy the reference. That definition
may reference other functions; if those other functions are defined
in the same object then that definition is used but otherwise a new
unsatisfied reference is created that must be filled by something
further to the right. For such linkers, there is no record kept
of definitions which have -already- been seen: if the first
file defines FOO and the second file calls upon FOO, then the
linker will -not- go back to the first file to find FOO: it will
expect -another- FOO to be defined in a later object. This kind
of linkage is "one pass": it never needs to go back to an existing
file, and never needs to keep track of which file defines which
object [only of which unsatisfied references there are.]
I have not used the first paradigm very much, so I am not sure
how it handles multiple definitions. In the second paradigm, if
you want to define something that pre-empts a library routine,
all you have to do is put its definition further right on the
command line than anything that calls upon that function:
as the linker passes from left to right, it will see your atof()
or whatever and will use that to satisfy any calls to atof()
that were further left; with there no longer being any unsatisfied
calls to atof(), the library object copy of the routine will not
be needed.

There are some other tricks to linkers as well, but they tend
to be more implimentation-dependant. One-pass linkers are fairly
common, but you should read the manual pages for the linker you
are using so that you know how it processes definitions.
Redefining a library function is not exceptional for utility
packages, and there is almost always -some- way to do it.
--
Preposterous!! Where would all the calculators go?!
Nov 14 '05 #3
On 27 Feb 2005 17:55:39 -0800, "Deniz Bahar" <de*********@gm ail.com>
wrote in comp.lang.c:
Hi,

I would like to call one of my functions the exact name as an existing
C library function (for example K&R2 exercises asks me to make an atof
function). If I don't include the header with the declaration for the
C library function (stdlib.h in this case) then define/declare my own
function with the same name, am I safe? It seems to work on my
compiler, but I wonder if this is portable or even acceptable? thx


According to the C standard, it is neither portable nor acceptable to
use the name of a library function in a context where it has external
linkage.

That means, you can define a function named "atof" if and only if you
define it with the static keyword, which means you can only call it by
name from within the same source file.

Otherwise you generate undefined behavior.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #4
On 28 Feb 2005 02:48:17 GMT, ro******@ibd.nr c-cnrc.gc.ca (Walter
Roberson) wrote in comp.lang.c:
In article <11************ **********@l41g 2000cwc.googleg roups.com>,
Deniz Bahar <de*********@gm ail.com> wrote:
:I would like to call one of my functions the exact name as an existing
:C library function (for example K&R2 exercises asks me to make an atof
:function). If I don't include the header with the declaration for the
:C library function (stdlib.h in this case) then define/declare my own
:function with the same name, am I safe?

Library function names are not 'reserved'.


The C standard disagrees:

"7.1.3 Reserved identifiers
1 Each header declares or defines all identifiers listed in its
associated subclause, and optionally declares or defines identifiers
listed in its associated future library directions subclause and
identifiers which are always reserved either for any use or for use as
file scope identifiers."

....and then, in the itemized list that is also part of paragraph 1:

"All identifiers with external linkage in any of the following
subclauses (including the future library directions) are always
reserved for use as identifiers with external linkage."

....therefore, it is undefined to define your own function with the
same name as any standard library function, unless you use the static
keyword to limit it to internal linkage.

Whether or not this particular instance of undefined behavior "works"
is in the eye of the beholder, since the C standard requireth not.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #5
In article <ut************ *************** *****@4ax.com>,
Jack Klein <ja*******@spam cop.net> wrote:
:According to the C standard, it is neither portable nor acceptable to
:use the name of a library function in a context where it has external
:linkage.

That does not sound right. Every library function has external linkage
relative to the user code.
--
I don't know if there's destiny,
but there's a decision! -- Wim Wenders (WoD)
Nov 14 '05 #6
Walter Roberson wrote:
In article <11************ **********@l41g 2000cwc.googleg roups.com>,
Deniz Bahar <de*********@gm ail.com> wrote:
:I would like to call one of my functions the exact name as an existing
:C library function (for example K&R2 exercises asks me to make an atof
:function). If I don't include the header with the declaration for the
:C library function (stdlib.h in this case) then define/declare my own
:function with the same name, am I safe?

Library function names are not 'reserved'.
That will come as a surprise to the authors of
Section 7.1.3, paragraph 1, fourth point:

"All identifiers with external linkage in any
of the following subclauses (including the future
library directions) are always reserved for use
as identifiers with external linkage."
[stuff about the way many linkers work]


Even if the linker is able to substitute a user-written
function for an actual library function, there is no guarantee
the program will work. Library functions may have private,
unpublished interfaces with each other, "back doors" into each
others' operations. In some cases this seems the only way to
implement the function: for example, fflush(NULL) needs some
way to find the eligible streams, free() needs a way to tell
malloc() that the memory has become available again, exit()
needs to find all the functions registered with atexit(),
and so on. The nature and number of such interrelationsh ips
varies from one C implementation to the next, and there is no
requirement that they be documented.

As a practical matter it is quite likely that replacing
atof() will work (even though it may well be connected with
strtod() and the *scanf() family in arcane ways). However,
the Standard does not promise it will work -- the Standard,
in fact, forbids the attempt -- and if the program misbehaves
you have no one to blame but yourself.

--
Eric Sosman
es*****@acm-dot-org.invalid
Nov 14 '05 #7
In article <94************ *************** *****@4ax.com>,
Jack Klein <ja*******@spam cop.net> wrote:
|On 28 Feb 2005 02:48:17 GMT, ro******@ibd.nr c-cnrc.gc.ca (Walter
|Roberson) wrote in comp.lang.c:

|> Library function names are not 'reserved'.

|The C standard disagrees:

|"7.1.3 Reserved identifiers
|1 Each header declares or defines all identifiers listed in its
|associated subclause, and optionally declares or defines identifiers
|listed in its associated future library directions subclause and
|identifiers which are always reserved either for any use or for use as
|file scope identifiers."

The 'and identifiers which are always reserved' is distinct in that
sentance from the declarations listed and the future directions list.
That sentance does *not* say that the identifiers in headers are
reserved: it says that one of the things that might be in a header
file is a declaration or definition of an identifier that is always
reserved. The part of the sentance about "for any use" is
in contrast to "as file scope identifiers": header files might
contain declarations or definitions for one or both types, or neither.
:...and then, in the itemized list that is also part of paragraph 1:

:"All identifiers with external linkage in any of the following
:subclauses (including the future library directions) are always
:reserved for use as identifiers with external linkage."

Reserving an identifier for external linkage does not reserve it
for implimentation library definition.

:...therefore, it is undefined to define your own function with the
:same name as any standard library function, unless you use the static
:keyword to limit it to internal linkage.

No, the part about being reserved for external linkage that you
quote would preclude using the identifier with internal linkage.
:Whether or not this particular instance of undefined behavior "works"
:is in the eye of the beholder, since the C standard requireth not.

The C standard is, of course, not about to say that if you
define a function with the same name as a library function that
somehow you are going to magically get the library function
behaviour even though your own code would be called. The C standard
cannot make any promises about what is going to happen if you
define your own version of a function -- not because such a thing
is prohibitted, but simply because the C standardization committee
can't know what your version of the routine is going to do.
The OP suggested that he might omit the header file. Any "reserved"
behaviour that you are imputing by way of the initial part of paragraph
1 of 7.1.3 would then be irrelevant. (And I don't agree that
that portion reserves any behaviour.)
--
If a troll and a half can hook a reader and a half in a posting and a half,
how many readers can six trolls hook in six postings?
Nov 14 '05 #8
In article <cv**********@c anopus.cc.umani toba.ca>
Walter Roberson <ro******@ibd.n rc-cnrc.gc.ca> wrote:
Library function names are not 'reserved'.


This is not quite right.

Library function names are reserved for use as external-linkage
identifiers. Hence the following source-file-fragment is valid,
guaranteed C code:

#include <stdio.h>
/* do not include <math.h> */

static int sin(void) {
puts("let he who is without sin() cast the first stone");
}

/* code that calls sin() */

but this one is not:

#include <stdio.h>
/* again, do not include <math.h> */

int tan(void) {
puts("sit in the sun for a few minutes for a nice tan()");
}

/* code that calls tan() */

Among other problems with attempting to override a library function
is the fact that modern C compilers may not actually *call* anything
where you put in a library-function call. For instance, this code:

/* #include <string.h> -- deliberately commented out */
#include <stdlib.h> /* for size_t */

void *memcpy(void *, const void *, size_t);

void f(int *p) {
memcpy(p, "123", 4);
}

produces the following x86 assembly code when run through GCC:

.p2align 2,,3
.globl f
.type f,@function
f:
movl $3355185, (%eax)
ret

(The function f() consists entirely of two instructions, neither
one a call to memcpy(). Compile with "-O -fomit-frame-pointer
-mregparm=3" to obtain this code. Otherwise you will get a little
extra code to deal with stack frames and/or parameter passing.)

(If you make the memcpy() declaration "static", gcc will emit a
call to the [missing] function, as required for Standard C. Of
course, you then have to define your static [internal-linkage]
memcpy() as well.)

(Even if you get a call to a function where you used a Standard C
Library function call, there is no guarantee that the call will be
made in the "usual fashion", or to the name you wrote. But this
tends to be less common than inline expansion of the "known"
function, since the latter has a greater return-on-investment in
most cases.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #9
In article <pP************ ********@comcas t.com>,
Eric Sosman <es*****@acm-dot-org.invalid> wrote:
: Even if the linker is able to substitute a user-written
:function for an actual library function, there is no guarantee
:the program will work. Library functions may have private,
:unpublished interfaces with each other, "back doors" into each
:others' operations.

That is a good point, which is of concern with "global" linkers
that examine all the files first before choosing which function
to link to. It turns out, though, not to be a problem with
one-pass linkers unless the user explicitly names the relevant
libraries before they name their object file [and even then,
it turns out not to be a problem for the standard library.]

In one pass linkers, if the user's object file defining [say]
malloc() is placed between their other object files and the
[possibly implicit] reference to the standard C library, then
the user's definition will satisfy any malloc() references in
the user's code; the one-pass linker will then "forget" that it
has seen a definition of malloc() and will proceed onwards. When
it then encounters an unsatisfied reference to malloc() inside the
standard libraries, then it will either satisify the reference from
the same library or will look further right for the definition,
just as would normally be the case. That is, absent deliberate
manipulation to cause otherwise, user definitions of library
functions will [for one pass linkers] apply only to the user
code, and the library definition will apply for anything further
right such as the other libraries. One-pass linkers do not
do "global" replacement of function references: only
unsatisfied references to the left of the current object.

I can't speak on what happens in practice with multipass linkers.
I would, though, point out that it would be unusual for
private interfaces to exist in library functions except at file
scope, which would use internal linkage rather than external.

--
How does Usenet function without a fixed point?
Nov 14 '05 #10

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

Similar topics

3
2563
by: Glenn C. Rhoads | last post by:
I'm having a problem calling a C function from a publicly available library file. To get the code to compile, I had to declare the function as an external C function as follows. extern "C" lib_function(....); But when I run my C++ program, I get a segmentation fault in one of the library functions (not the function I directly called but one a few levels down in the calling hierarchy). According to the user manual provided with the...
19
4278
by: Ross A. Finlayson | last post by:
Hi, I hope you can help me understand the varargs facility. Say I am programming in ISO C including stdarg.h and I declare a function as so: void log_printf(const char* logfilename, const char* formatter, ...); Then, I want to call it as so:
11
1975
by: j23 | last post by:
I have library (static) testlib.cpp: #include <stdarg.h> void xxx(...) { char buf; va_list args; va_start(args, buf); va_end(args); }
1
2922
by: Jesse McGrew | last post by:
Hi all, I'm trying to make a plugin DLL for a third-party application, using VC++ .NET 2003. This DLL acts as a bridge between the C++ plugin API of the application, and my actual plugin code written in C#. When the app calls my unmanaged functions, they work fine. But as soon as my unmanaged functions call managed functions (in the same source file!), the app reports an "unknown exception" error.
5
3447
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS 5), but fails on IIS 6 running on a Win2003 server. The web uses Pages derived from a custom class I wrote (which itself derives from Page) to provide some common functionality. The Page_Load handler the failing webpage starts out like this: ...
6
1713
by: Amjad | last post by:
Hi, I want to make a project that calls and executes a function (VB code) made in a seperate file in the Application Folder. I know I can create the function in my project and call it internally, but I want to put the function's code in an external file, so that future updates to the function will require a replacing the function file instead of re-installing the whole project. Can you give me ideas on how I can make small updates to my...
0
1049
by: mike.gilmore | last post by:
Hi, I've created a custom control that contains a collection of link buttons (PagingControl.vb). I want an event to fire when the link button is clicked. The event handler is located in my primary asp.net file (ShowData.aspx). However, when I attempt to compile my class file, I get an error because the event the link button is attempting to wire to doesn't exist.
15
22889
by: dspfun | last post by:
Hi, Is it possible to print the function name of the calling function? For example, f1() and f2() both calls f3(), in f3() I would like to print the name of the function calling f3() which could either be f1() or f2(). BRs!
0
4188
by: phancey | last post by:
hi, I am trying to call a WCF service from Excel VBA. I do not want to rely on anything installed on client machine apart from my Excel spreadsheet so I wanted to use the MEX method of invoking as explained here: http://msdn.microsoft.com/en-us/library/ms752245.aspx This works fine if all parameters and return values are basic types but it is not obvious to me what needs to be done if I want to use a custom type. I currently get a...
0
9922
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
9766
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
11065
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
10391
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 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...
0
9536
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...
0
7098
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();...
0
5763
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...
2
4177
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3203
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.