Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

static in C

Name: Anonymous 2010-10-16 17:23

I can see why you would want static variables in functions but what's the point of static globals?

Name: Anonymous 2010-10-16 17:52

IHNBT.

Name: Anonymous 2010-10-16 17:54

>>1

All variables with the "static" storage class qualifier are global, i.e., there exists only one copy of the variable.  Static variables declared at file scope are only accessible from that translation unit, those declared at function scope are only accessible from that function.  Static variables (and functions) have no linkage.

The "no linkage" part is the most important part.  Suppose you have static int myglobal; defined in header "defs.h", and files "main.c" and "input.c" both include "defs.h".  Then they both get their own separate copy of myglobal, because it has no linkage they are not combined into one object.

If an object has no linkage, you can name it whatever you want without worrying about it clashing with another object defined somewhere else in the program.  Basically, you use it any time you have a global variable that you want to be accessible from only a few functions, as long as they're all defined in the same translation unit.

It's much more common to see static functions.  I use them quite often, they're kind of like private functions in C++ except they're much nicer because they don't go in the header files.

(P.S. Yes, there is a such thing as an "object" in C.)

Name: Anonymous 2010-10-16 17:56

It's useful when you want a variable to not be visible from other sources. If the variable is static its name is not exported into the object file. Its good to declare your variables as static if they are global as much as possible, it also increases runtime performance I believe.

Name: Anonymous 2010-10-16 17:57

To remove external linkage.

C doesn't have namespaces, so all global symbols are in the same one and the fewer symbols you make global the less likely you'll have a random collision.

Also, it gives the compiler a bit more leeway in optimizing static functions, since by definition nothing outside the current scope can call it. This makes it more likely to inline functions since a non-inlined copy won't have to be emitted, and (in theory, not practice) also allows the compiler to automatically use non-standard calling conventions.

Name: Anonymous 2010-10-16 18:16

>>5
C does have namespaces.  There is a "tag" namespace, a "label" namespace, and an "object" namespace.  (And if you have random collisions, it's because you're a bad programmer.  Don't name your functions randomly and they won't collide randomly.)

Name: Anonymous 2010-10-16 19:12

There is a "tag" namespace, a "label" namespace, and an "object" namespace.
HIBT?

Name: Anonymous 2010-10-16 19:41

>>7
#include <stdio.h>
struct x { long x; };
int main(int argc, char *argv[])
(
    struct x x;
    x.x = strtol(argv[i]);
    if (x.x) goto x;
    return 0;
x:
    return 1;
}


Note that none of the "x" collide because they are in different namespaces.  There's a namespace for tags (struct x), one for each structure for fields (forgot to mention it), one for labels, and one for objects (the "x" variable).  It's a matter of nitpicking, but according to the standard there is one namespace for each structure's fields, but only one namespace for all labels... but labels won't be in scope outside a function.

Common Lisp also has different namespaces, most notable it has a separate namespaces for functions and data:

(defun f (car) (car car))

Name: Anonymous 2010-10-16 19:58

>>8
People usually mean "object namespace" when they say "namespace" without qualification. The namespaces you're showing off are entirely different spaces without the possibility of name collisions, i.e. you can't use a struct as a label, etc.

You're not wrong but you are being redundant.

Name: Anonymous 2010-10-16 22:24

>>1
``Global'' is a bad word to use because it has no well-defined meaning in C and people usually have personal definitions for it. For example, >>3 has used it to mean ``static storage duration'' while >>5 has used it to mean ``external linkage''.

Your usage indicates you want to ask what the purpose is of objects defined at file scope with static. The purpose is to have an object that can be shared between functions in the translation unit, but whose name is invisible to other translation units.

>>3
They have internal linkage, not no linkage. See C99 6.2.2.

Name: Anonymous 2010-10-17 0:42

>>6
Does anyone name their functions randomly? My example was more that if you have two libraries that do similar things, chances are that there will be some symbols with similar to identical names.

For instance, get_bit(), which I've had to deal with an actual collision between two different libraries that had functions named that.

Name: Anonymous 2010-10-17 0:49

>>6
Also, the only namespace that matters for collisions whatsoever is objects, since that's the only one that exists in object files. (hurr)

Name: Anonymous 2010-10-17 11:05

>>11
For instance, get_bit(), which I've had to deal with an actual collision between two different libraries that had functions named that.
I thought name mangling was supposed to avoid this. (Actual question, not trolling.)

Name: Anonymous 2010-10-17 11:06

>>13
IHBT
name mangling is c++ thing to deal with overloading

Name: Anonymous 2010-10-17 12:48

>>3
Are you retarded or just trolling? C has no objects, it's not object-oriented like C++.

Name: Anonymous 2010-10-17 13:11

>>15
-1/10

Name: Anonymous 2010-10-17 13:38

>>15
Just because a person can speak doesn't mean that they're 'socially oriented'.

Name: Anonymous 2010-10-17 15:01

>>15
You might want to learn C before shooting your mouth off.

Name: Anonymous 2010-10-17 15:12

>>18
Canonically, it's foot, not mouth; also YHBT.

Name: Anonymous 2010-10-17 15:14

>>19
Idiomatically, it's <i>shoot your mouth off</i>, not <i>shoot your foot off</i>; also IHBT.

Name: Anonymous 2010-10-17 15:16

>>19
I don't care if he injures himself, I just want him to smarten- and/or shut- up.

Name: Anonymous 2010-10-17 15:17

>>20
Normally, it's [m][i][/m]BBCode[/i], not <i>HTML</i>; also WHBT.

Name: Anonymous 2010-10-17 15:33

>>21
-
Fuck off.

Name: Anonymous 2010-10-17 15:33

>>22
Ironically, it's [m][i][/m]BBCode[/i], not [#][m][i][/m][/#][b][i]BBCode[/i][/b][m][/i][/m], also IHPBT

Name: Anonymous 2010-10-17 15:34

>>24
Well, shit.

Name: Anonymous 2010-10-17 15:38

>>25
Yeah.

Name: Anonymous 2010-10-17 18:01

>>24
Perhaps it is [i]BBCode[/i], and not any of the aforementioned.

Name: Anonymous 2010-10-17 19:10

>>27
Test [i]BBCode[/i]

Name: Anonymous 2010-10-17 19:10

>>28
YES

Name: Anonymous 2010-10-17 19:25

>>23
They are bullets. They go in your feet.

Name: Anonymous 2010-10-18 15:00

Bee bee coed.

Name: BBCode failure thread 2010-10-18 21:57

This thread has too many BBcode failures.
You can't reply anymore.

Name: Name: BBCode failure thread 2010-10-18 23:31

This thread has too many [up]B[/up]B<i>code</i> failures.
You can't reply anymore.

Name: Anonymous 2010-12-23 23:21

Name: Anonymous 2011-02-03 0:16


Don't change these.
Name: Email:
Entire Thread Thread List