static functions and prototypes 
Author Message
 static functions and prototypes

Should I also declare the prototype static if I
define the function static? Or is it sufficient
to only have one of them static? My purpose is to
hide the function and make it a black box.
--



Tue, 14 Jan 2003 03:00:00 GMT  
 static functions and prototypes

Quote:

> Should I also declare the prototype static if I
> define the function static? Or is it sufficient
> to only have one of them static? My purpose is to
> hide the function and make it a black box.

If you don't even mention the function in the interface
header, it will automatically satisfy that requirement.
--



Wed, 15 Jan 2003 03:00:00 GMT  
 static functions and prototypes

deja.com writes

Quote:
>Should I also declare the prototype static if I
>define the function static? Or is it sufficient
>to only have one of them static? My purpose is to
>hide the function and make it a black box.

I am pretty sure that you should declare the prototype static, I am not
sure that you would also need to apply static to the definition. It is
the prototype (if declared) that introduces the name to the compiler.

Francis Glassborow      Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
--



Wed, 15 Jan 2003 03:00:00 GMT  
 static functions and prototypes

Quote:

> Should I also declare the prototype static if I
> define the function static? Or is it sufficient
> to only have one of them static?
> My purpose is to hide the function and make it a black box.

I'm not sure how 'making it a block box' would come into this.  All
compiled functions with no source code available are effectively black
boxes, to their user. static functions, OTOH, are not black boxes:
they're completely hidden from the user of a compiled sourcefile,
i.e. there is no box at all.

There are mainly two different 'schools' regarding treatment of static
functions:

1) No prototypes at all for static functions --- just have their
definitions occur before any of their uses, in the source file.

2) Prototypes for static functions, too, but in the source, not
the header.  

In case 1), you obviously have to add the 'static' qualifier to the
definition, for lack of any other place to put it.  For the second
case, the 'static' has to be added to the prototype.  If you want to
keep picky compilers silent, you'll want to add them to the
definition, too.

--

Even if all the snow were burnt, ashes would remain.
--



Fri, 17 Jan 2003 03:00:00 GMT  
 static functions and prototypes


Quote:

> > Should I also declare the prototype static if I
> > define the function static? Or is it sufficient
> > to only have one of them static? My purpose is to
> > hide the function and make it a black box.

> If you don't even mention the function in the interface
> header, it will automatically satisfy that requirement.

Not so.  Nothing prevents some other module from declaring the
function and calling it.  The linker will now get at your
non-hidden code.

It should be declared static in the source module, not the .h
file.  It should never appear in the .h file, because that passes
knowledge of the black-box out to the user.

I prefer to simply define the function (as static) before use in
the .c file.  That avoids any miscopying of headers, and greatly
eases maintenance.  THE ONLY THINGS in the .h file are the access
I want to give to the .c file, along with the necessary types and
definitions to use that access.

Similarly, all data in the .c file is normally declared static,
unless I SPECIFICALLY want to share it.  Makeing that (sharing)
rare also eases maintenance, especially for the poor guy who never
heard of my code before.  He doesn't have to search every possible
file for a reference to each variable and/or function before
making changes.  He can also make new modules without worrying
about name clashes.

--

 http://www.qwikpages.com/backstreets/cbfalconer/
--



Sat, 25 Jan 2003 03:00:00 GMT  
 static functions and prototypes

Quote:




> > > Should I also declare the prototype static if I
> > > define the function static? Or is it sufficient
> > > to only have one of them static? My purpose is to
> > > hide the function and make it a black box.
> > If you don't even mention the function in the interface
> > header, it will automatically satisfy that requirement.
> Not so.  Nothing prevents some other module from declaring the
> function and calling it.  The linker will now get at your
> non-hidden code.

No, it won't.  The function's identifier has static linkage,
so the function cannot be invoked from outside the file
containing it, except via a stored pointer to it (which would
have to be intentionally set up in the file containing it).

Quote:
> It should be declared static in the source module, not the .h
> file.

He said "if I define the function static".  That can be done
only in the source file containing its body.  I took that
hypothetical as granted and answered the conditional question.

Quote:
> It should never appear in the .h file, because that passes
> knowledge of the black-box out to the user.

Which is the advice I gave.
--



Sun, 26 Jan 2003 03:00:00 GMT  
 static functions and prototypes

Quote:




>> > > Should I also declare the prototype static if I
>> > > define the function static? Or is it sufficient
>> > > to only have one of them static? My purpose is to
>> > > hide the function and make it a black box.
>> > If you don't even mention the function in the interface
>> > header, it will automatically satisfy that requirement.
>> Not so.  Nothing prevents some other module from declaring the
>> function and calling it.  The linker will now get at your
>> non-hidden code.

>No, it won't.  The function's identifier has static linkage,
>so the function cannot be invoked from outside the file
>containing it, except via a stored pointer to it (which would
>have to be intentionally set up in the file containing it).

I assumed something like

File a:

       int foo(int bar) {whatever; return x}
       <EOF>

File b:
       whatever;
       foo(2);

which will happily link to file a's foo without complaint
(except possibly about missing prototype, which can be
satisfied by a little more typing in file b).

This is what is avoided by adding the static.

I am sure you (gwyn) know all this, and I just wanted to make
the thread 'perfectly clear'.


http://www.qwikpages.com/backstreets/cbfalconer/


 http://www.qwikpages.com/backstreets/cbfalconer/

-----------------------------------------------------------
--



Sun, 26 Jan 2003 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. static functions and prototypes

2. Prototypes of static functions

3. Curious: static functions and their prototypes

4. Static functions prototype

5. One Problem in Calling Static function from Non static function

6. Accessing non-static functions from a static function

7. Function returning a function-pointer - how to prototype ?

8. prototype (?) of function passed to function

9. Function prototypes for pointers to functions.

10. HELP: function prototype with pointer to function as parameter

11. static/extern in prototype?

12. Pointers to non-static vs static functions

 

 
Powered by phpBB® Forum Software