469,640 Members | 1,560 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,640 developers. It's quick & easy.

simple question: namespace

Hi,

is it true that I can't define two functions with the same name even if
I put them into two different c-files?

Inside each file I wanted to generally have a function named f_init()
but not module1_f_init(), module2_f_init(), ...

Felix
Nov 14 '05 #1
6 953
Felix Kater <f.******@gmx.net> wrote:
is it true that I can't define two functions with the same name even if
I put them into two different c-files? Inside each file I wanted to generally have a function named f_init()
but not module1_f_init(), module2_f_init(), ...


If they are used only inside the file they are defined in you can use the
same name in each file, all you need is to declare them as "static" - that
restricts the scope (i.e. visibility) of the function to the source file
it's defined in. But, of course, then you can't invoke the function from a
different source file anymore (well, unless its address is passed around
via a pointer, but I guess that's a different story).

Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #2
Felix Kater <f.******@gmx.net> spoke thus:
is it true that I can't define two functions with the same name even if
I put them into two different c-files?
I believe the behavior in that case is undefined; IOW, you are
correct.
Inside each file I wanted to generally have a function named f_init()
but not module1_f_init(), module2_f_init(), ...


If the f_init() function is identical for each module, you can declare
its prototype in a header file and only place its definition in one
source file.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #3
Je***********@physik.fu-berlin.de:
all you need is to declare them as "static" - that
restricts the scope (i.e. visibility) of the function to the source
file


Thanks, that is what I was looking for.
Felix
Nov 14 '05 #4
Felix Kater wrote:
Hi,

is it true that I can't define two functions with the same name even if
I put them into two different c-files?

Inside each file I wanted to generally have a function named f_init()
but not module1_f_init(), module2_f_init(), ...


You can reuse a name in a second translation unit if it is marked
static. Of course, you can't use that function in the first file, but
it wouldn't make sense to have two different functions with the same
name visible in the first file.
For example:

/* first translation unit */

#include <stdio.h>
int foo2(void);

int foo(void)
{
return 1;
}

int main(void)
{
printf("foo from this translation unit: %d\n", foo());
printf("foo from the other translation unit,\n"
" called via foo2: %d\n", foo2());
return 0;
}

/* second translation unit */

static int foo(void)
{
return 2;
}

int foo2(void)
{
return foo();
}
[output]
foo from this translation unit: 1
foo from the other translation unit,
called via foo2: 2
Nov 14 '05 #5
On Mon, 24 Jan 2005 21:22:02 +0000, Jens.Toerring wrote:
Felix Kater <f.******@gmx.net> wrote:
is it true that I can't define two functions with the same name even if
I put them into two different c-files?
Inside each file I wanted to generally have a function named f_init()
but not module1_f_init(), module2_f_init(), ...


If they are used only inside the file they are defined in you can use the
same name in each file, all you need is to declare them as "static" - that
restricts the scope (i.e. visibility) of the function to the source file
it's defined in.


What it changes is "linkage", i.e. whether 2 declarations of the same name
in the name namespace are "linked" i.e. refer to the same object or
function. Scope is *always* limited to at most the source file (or more
precisely translation unit) of the declaration.
But, of course, then you can't invoke the function from a
different source file anymore (well, unless its address is passed around
via a pointer, but I guess that's a different story).


Yes, you can call a function from anywhere if you have a pointer to it,
even a static function.

Lawrence
Nov 14 '05 #6
Lawrence Kirby wrote:
On Mon, 24 Jan 2005 21:22:02 +0000, Jens.Toerring wrote:
Felix Kater <f.******@gmx.net> wrote:

is it true that I can't define two functions with the same name
even if I put them into two different c-files?

Inside each file I wanted to generally have a function named
f_init() but not module1_f_init(), module2_f_init(), ...


If they are used only inside the file they are defined in you
can use the same name in each file, all you need is to declare
them as "static" - that restricts the scope (i.e. visibility) of
the function to the source file it's defined in.


What it changes is "linkage", i.e. whether 2 declarations of the
same name in the name namespace are "linked" i.e. refer to the
same object or function. Scope is *always* limited to at most the
source file (or more precisely translation unit) of the
declaration.


This is a perenial problem - how to arrange to automatically run
initialization code in a variety of modules. Unfortunately there
is no quick and easy way to do so in C, but an adequate substitute
is usually to initialize on first use. To make this independent I
do something like:

/* file module.h */
int fee(int param);
int fie(int param);
/* These are the only external accesses to module exposed */

/* file module.c */
#include "module.h"

static int initialized; /* default 0 at startup */

/* whatever static functions and data are needed */

static void initialize(void)
{
/* initialization stuff, once per program run */
initialized = 1;
}

int fee(int param)
{
if (!initialized) initialize();
/* code for fee */
}

int fie(int param)
{
if (!initialized) initialize();
/* code for fie */
}

Now the module can be used, and initialization will always occur
before it is really needed. The cost is the overhead of testing
<initialized> at every entry, which will normally be minor. The
code in initialize() will only run once. No namespace pollution
has occured. But the guarded call to initialize must be in every
externally accesible function.

You can move the test to the initialize function itself, but then
the overhead will be an extra function call at every entry to the
module.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

22 posts views Thread by lokman | last post: by
7 posts views Thread by Mark Prenter | last post: by
3 posts views Thread by Katie S | last post: by
7 posts views Thread by SL | last post: by
3 posts views Thread by Marco T. | last post: by
3 posts views Thread by Chrism2671 | last post: by
4 posts views Thread by =?Utf-8?B?SmFtZXMgUGFnZQ==?= | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.