Re: Static functions in C code
On Fri, Jun 02, 2006 at 02:53:48AM +0100, Steve Fairhead wrote: > Denis Doroshenko said: > > So how do you specify that a function should be visible only to the > > local compilation unit? Or, how do you keep others from using your > > locally-scoped (but not declared static) function in a global context? > > >> why would you even want that (moreover in opensource)? hide for what > reason? << > There's a myth that complex software has to be buggy. I don't believe in it. > The key lies in managing complexity. Hiding the internals of a module is one > of the tools that allows us to manage complexity. Well said. I'll add that tools are not enough, and won't help someone who doesn't *really* understand what they're for or how to use them. Tools of all kinds are often misused, and some few get frowned upon because they're misused so often. Write good, clean code with static declarations that obviously make things better and there'd probably be a lot less problem with it. Talk about sprinkling static around like holy water and the reception will be less friendly. ;) -- Darrin Chandler| Phoenix BSD Users Group [EMAIL PROTECTED] | http://bsd.phoenix.az.us/ http://www.stilyagin.com/ |
Re: Static functions in C code
Denis Doroshenko said: > So how do you specify that a function should be visible only to the > local compilation unit? Or, how do you keep others from using your > locally-scoped (but not declared static) function in a global context? >> why would you even want that (moreover in opensource)? hide for what reason? << I'm not a fan of C++, for many reasons. However some (not all) of the principles of OOP are conducive to good design practice. One of these has to do with distinguishing between private and public functions (I won't say variables; I avoid globals like the plague) within a module. The public interface is all-you-need-to-know about the module. The private (static) stuff is in the none-of-your-business category. There's a myth that complex software has to be buggy. I don't believe in it. The key lies in managing complexity. Hiding the internals of a module is one of the tools that allows us to manage complexity. Steve http://www.fivetrees.com [demime 1.01d removed an attachment of type application/ms-tnef which had a name of winmail.dat]
Re: Static functions in C code
On 5/31/06, Brett Lymn <[EMAIL PROTECTED]> wrote: On Tue, May 30, 2006 at 04:55:14PM +0300, Denis Doroshenko wrote: > > why would you even want that (moreover in opensource)? hide for what reason? It's called lexical scoping - it has nothing really to do with security more to do with preventing namespace pollution. Clearly you have never written a library. clearly you're impying too much here. i must admit that i have not used statics since there was nothing in my tiny projects that would need it. i won't argue with you, from "you have never written a library" sounds like it would be fruitless. if you want multiple functions named say "do_it" in your code to not cause name clashes, then do_it(); i always try to name things the way they very unlikely to clash with something and yet such names seem to make more sense. i like to debug my problems fast, to see every step, what was called, where and with what parameters. as i said, i didn't hit the need for it.
Re: Static functions in C code
On Tue, May 30, 2006 at 04:55:14PM +0300, Denis Doroshenko wrote: > > why would you even want that (moreover in opensource)? hide for what reason? > It's called lexical scoping - it has nothing really to do with security more to do with preventing namespace pollution. Clearly you have never written a library. By scoping functions static you are indicating that the functions are private and are not part of the interface available for use. You do this actually to protect the users of your code - you don't need to care about namespace clashes e.g. you can call the internal function next_one() without fear, if the function is not statically scoped then you would have to prefix the function with __mylib_next_one() or suchlike otherwise a consumer of your library would get a duplicate symbol if they created their own function next_one(), or even worse the consumer's function will be called by the library internals... no doubt doing the wrong thing. Secondly it means that you, as the library creator, are able to change the internal interfaces at whim without needing to be concerned about the impact on the consumers of your library. Sure, people can modify the source and remove the static from the function but that this point they are lining a gun up on their foot with their finger on the trigger - if they happen to put a bullet through their foot they have noone to blame but themselves. Again, it's not a security issue - it's a usuability/api issue. -- Brett Lymn
Re: Static functions in C code
On 5/30/06, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: On Fri, May 26, 2006 at 08:29:58AM -0500, Marco Peereboom wrote: > Static has it's uses however for some > reason the (open source) world at large seem not to understand > what they are. Same is true with typedef, it has its uses too but > mostly it is abused. What are some examples of abuse/misuse of typedef? (That's an honest question, not trolling.) well everything was already said within this very thread. RTFA in case you've missed it. > I bet you have never wasted days finding a non-bug because of > static. So how do you specify that a function should be visible only to the local compilation unit? Or, how do you keep others from using your locally-scoped (but not declared static) function in a global context? why would you even want that (moreover in opensource)? hide for what reason? I've seen situations where someone saw a function that was not declared static, but clearly intended to have only local visibility, and someone went off and prototyped that function in some other module so they could use it. (Then again, even using 'static' probably wouldn't have solved the problem---that person would have just deleted the keyword!) well, as i asked before, i think it is not like OpenBSD developers consider hiding functions from other developers (and effectively from themselves). this increase of obscurity has little to go with "use the source Luke"... Just curious! Thanks, Matt
Re: Static functions in C code
On Fri, May 26, 2006 at 08:29:58AM -0500, Marco Peereboom wrote: > My answer is correct. It is not my fault that you don't have a > clue about programming. Static has it's uses however for some > reason the (open source) world at large seem not to understand > what they are. Same is true with typedef, it has its uses too but > mostly it is abused. What are some examples of abuse/misuse of typedef? (That's an honest question, not trolling.) > I bet you have never wasted days finding a non-bug because of > static. So how do you specify that a function should be visible only to the local compilation unit? Or, how do you keep others from using your locally-scoped (but not declared static) function in a global context? I've seen situations where someone saw a function that was not declared static, but clearly intended to have only local visibility, and someone went off and prototyped that function in some other module so they could use it. (Then again, even using 'static' probably wouldn't have solved the problem---that person would have just deleted the keyword!) Just curious! Thanks, Matt
Re: Static functions in C code
On 5/26/06, Matthias Kilian <[EMAIL PROTECTED]> wrote: No sarcasm. If you've clashes, the linker will tell you. But if you make everything static, you may using the same name for different things without noticing, and this *may* be confusing when reading the code. That's a very reasonable explanation to me. Thank you. -- DG
Re: Static functions in C code
Original message from "Diego Giagio" [EMAIL PROTECTED]: > > 1. there are debugging requirements. Static functions do not expose entry > > points. > > Even for user-level code? If you are thinking there is a difference between kernel code & userland code, no. Compilers compile code based upon the files & switches provided. The only difference between static & non-static functions from a compiler's perspective is that non-static functions have the symbol representing their address made public in the resulting object file; static functions do have an analogous symbol, but it is not made public. Look at "nm" for more discussion: http://www.openbsd.org/cgi-bin/man.cgi?query=nm&apropos=0&sektion=0&manpath=OpenBSD+Current&arch=i386&format=html End result: the keyword "static" simply plays games with what the linker receives as input. Jim
Re: Static functions in C code
On Fri, May 26, 2006 at 11:59:51AM -0300, Diego Giagio wrote: > >Because it'll clash. Clashing is good. > > I thought you were being sarcastic, and I was wrong. I strongly apologize. No sarcasm. If you've clashes, the linker will tell you. But if you make everything static, you may using the same name for different things without noticing, and this *may* be confusing when reading the code. Ciao, Kili
Re: Static functions in C code
On 5/26/06, Jacob Yocom-Piatt <[EMAIL PROTECTED]> wrote: wow. this is just about the most offensive thing i've ever seen on list. that's not to say it should be censored ;). I wrongly interpreted Marco's statement, and shot him badly. all this from someone who spends time pointing finding holes in swiss cheese (read: ethereal). what would we do without that guy doing the quality control at the swiss cheese factory? At least you know how to use Google. -- DG
Re: Static functions in C code
On 5/26/06, Jason Crawford <[EMAIL PROTECTED]> wrote: And Marco was explaining why he (and probably other OpenBSD devs) don't use static: name clashes. static makes things more difficult to debug, and having 50 different static functions named the same thing could get pretty confusing in large projects. Thank you. -- DG
Re: Static functions in C code
On 5/25/06, Marco Peereboom <[EMAIL PROTECTED]> wrote: Because it'll clash. Clashing is good. I thought you were being sarcastic, and I was wrong. I strongly apologize. -- DG
Re: Static functions in C code
On 5/26/06, Diego Giagio <[EMAIL PROTECTED]> wrote: On 5/25/06, Marco Peereboom <[EMAIL PROTECTED]> wrote: > Because it'll clash. Clashing is good. I'm pretty sure you would be more successfull on a humor TV show as a clown than wasting people time and bandwith with stupid statements like that. And I don't mind if you are a OpenBSD developer, contributor, US president or a dirty bitch. Dear Diego, Again this is not a troll. But being quick to get offended and be angry at people trying to help will mostly make us look like real fools infront of others. And it has happened to me a lot of times. ( Still trying to get over it ). I am sure Marco has a reason to state that considering who he is. http://www.peereboom.us/ If you are interested in knowledge perhaps a little more polite request to elaborate his point would yeild more information. Kind Regards :-) Siju
Re: Static functions in C code
Original message >Date: Fri, 26 May 2006 10:14:04 -0300 >From: "Diego Giagio" <[EMAIL PROTECTED]> >Subject: Re: Static functions in C code >To: "Marco Peereboom" <[EMAIL PROTECTED]> >Cc: misc@openbsd.org > >On 5/25/06, Marco Peereboom <[EMAIL PROTECTED]> wrote: >> Because it'll clash. Clashing is good. > >I'm pretty sure you would be more successfull on a humor TV show as a >clown than wasting people time and bandwith with stupid statements >like that. And I don't mind if you are a OpenBSD developer, >contributor, US president or a dirty bitch. > wow. this is just about the most offensive thing i've ever seen on list. that's not to say it should be censored ;). all this from someone who spends time pointing finding holes in swiss cheese (read: ethereal). what would we do without that guy doing the quality control at the swiss cheese factory?
Re: Static functions in C code
On 5/26/06, Diego Giagio <[EMAIL PROTECTED]> wrote: On 5/25/06, Ted Unangst <[EMAIL PROTECTED]> wrote: > how many parse_config functions do you think spamd needs? It was an example. The point is: is there a reason for not using static on functions with internal linkage? There's at least one reason to use static: name clashes. And Marco was explaining why he (and probably other OpenBSD devs) don't use static: name clashes. static makes things more difficult to debug, and having 50 different static functions named the same thing could get pretty confusing in large projects.
Re: Static functions in C code
My answer is correct. It is not my fault that you don't have a clue about programming. Static has it's uses however for some reason the (open source) world at large seem not to understand what they are. Same is true with typedef, it has its uses too but mostly it is abused. I bet you have never wasted days finding a non-bug because of static. And let me state again that clashing is good. Diego Giagio wrote: On 5/25/06, Marco Peereboom <[EMAIL PROTECTED]> wrote: Because it'll clash. Clashing is good. I'm pretty sure you would be more successfull on a humor TV show as a clown than wasting people time and bandwith with stupid statements like that. And I don't mind if you are a OpenBSD developer, contributor, US president or a dirty bitch. -- DG
Re: Static functions in C code
On Fri, May 26, 2006 at 10:14:04AM -0300, Diego Giagio wrote: > On 5/25/06, Marco Peereboom <[EMAIL PROTECTED]> wrote: > >Because it'll clash. Clashing is good. > > I'm pretty sure you would be more successfull on a humor TV show as a > clown than wasting people time and bandwith with stupid statements > like that. And I don't mind if you are a OpenBSD developer, > contributor, US president or a dirty bitch. nobody cares if you use static or not. if you do not understand answers given you can as well make your own decisions yourself godamnit! cu -- paranoic mickey (my employers have changed but, the name has remained)
Re: Static functions in C code
On 5/26/06, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: Either because: 1. there are debugging requirements. Static functions do not expose entry points. Even for user-level code? 2. most developers don't consider limiting global namespace pollution as this doesn't frequently hinder development. Consider being concerned about how many names are in the global namespace the programmatic equivalent to flossing. I'm not yet convinced for not using static on functions with internal linkage. If someone else knows a real reason, please step ahead. Anyway, thanks for you long answer. -- DG
Re: Static functions in C code
On 5/25/06, Ted Unangst <[EMAIL PROTECTED]> wrote: how many parse_config functions do you think spamd needs? It was an example. The point is: is there a reason for not using static on functions with internal linkage? There's at least one reason to use static: name clashes. -- DG
Re: Static functions in C code
On 5/25/06, Marco Peereboom <[EMAIL PROTECTED]> wrote: Because it'll clash. Clashing is good. I'm pretty sure you would be more successfull on a humor TV show as a clown than wasting people time and bandwith with stupid statements like that. And I don't mind if you are a OpenBSD developer, contributor, US president or a dirty bitch. -- DG
Re: Static functions in C code
Original message from Diego Giagio [EMAIL PROTECTED]: > ... > I have a concern, thought: why most applications don't use the 'static' > keyword for > functions with internal linkage ? Wouldn't that avoid function name clashes > when > developing large programs? Either because: 1. there are debugging requirements. Static functions do not expose entry points. 2. most developers don't consider limiting global namespace pollution as this doesn't frequently hinder development. Consider being concerned about how many names are in the global namespace the programmatic equivalent to flossing. As an aside, note in C++ that the keyword "static" is even less in vogue than it was in C given its overuse within the language definition. "static" has at least five distinct meaning based upon its context: 1. "static" global instances -- no public linkage for names are created in object files. 2. "static" automatic variables within functions -- " 3. "static" functions -- " 4. "static" members within class definitions -- class variables of which one instance is shared across all instances of the class. 5. "static" member functions -- functions with no knowledge of individual class instances other than whatever static members which may be defined. There may be a sixth, but I haven't verified it lately. This may only be a compiler implementation issue: 6.? "static" constants -- may force the compiler to allocated memory for the instance. This isn't to say that there are equivalents elsewhere in the language which obviate all uses of "static", but namespaces sidesteps some of these issues. Jim
Re: Static functions in C code
On 5/25/06, Diego Giagio <[EMAIL PROTECTED]> wrote: Lately I've been reading OpenBSD code, both user-level and kernel-level, and I find it very clean and well organized. I have a concern, thought: why most applications don't use the 'static' keyword for functions with internal linkage ? Wouldn't that avoid function name clashes when developing large programs? See below spamd.c snippet: void usage(void); char *grow_obuf(struct con *, int); int parse_configline(char *); void parse_configs(void); how many parse_config functions do you think spamd needs?
Re: Static functions in C code
Because it'll clash. Clashing is good. Diego Giagio wrote: Lately I've been reading OpenBSD code, both user-level and kernel-level, and I find it very clean and well organized. I have a concern, thought: why most applications don't use the 'static' keyword for functions with internal linkage ? Wouldn't that avoid function name clashes when developing large programs? See below spamd.c snippet: void usage(void); char *grow_obuf(struct con *, int); int parse_configline(char *); void parse_configs(void); ... Thanks. -- DG