Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

What do extern and static really do?

This topic is 5187 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I was going to only ask about extern, but if I could get some info on what declaring a variable as static really does, that''d be great too. By variable, here, I do not mean a member of a struct or class. I know what that does; just lettin you know. Anyway, if I declared a variable in C as extern int krpulskitanivic; // ignore name what would that really do to the variable? This is an old question that I don''t recall ever asking about. ~Resist Anna Nichole Smith

Share this post


Link to post
Share on other sites
Advertisement
it tells the compiler that there is a global variable called "krpulskitanivic" elsewhere in another file (an ''external'' file to this one) so you can then use that variable in that file, even though it is defined in another.
And static means that if its say a local variable in a function, it wont lose its value the next time the function is called, eg:

  
int GetNum()
{
static int Num = 0;
return Num++;
}

the first time you call it it will return 0, then if you call it again it will return 1 (not 0 had it not been static), then the next time 2, then 3 and so on.

Share this post


Link to post
Share on other sites
I know that much about static, but what if you declare an object that is global or defined in a source file (versus a header file).

For instance:

//foo.h
typedef unsigned int FooIndex;

void InitFoo ( FooIndex id );



//foo.c
#include "foo.h"

struct __FOO
{
// members
} static foo[1000];

void InitFoo ( FooIndex id )
{
// initialize foo members
}

Share this post


Link to post
Share on other sites
The effect of static depends on where the variable is. If a global variable or function is declared static, it has file scope; it can''t be used anywhere except the file it was defined in. If a variable inside a function is declared static, it has static storage duration; it''ll retain its value and address over all calls to that function. If that makes sense.

quote:
~Resist Anna Nichole Smith


The cow lady compels you.

Share this post


Link to post
Share on other sites
quote:
Original post by Jan Wassenberg
1) headers are just files that are inserted into your source where they''re #included.

At its simplest. In most medium-to-large sized projects, however, multiple files include the same header and inclusion guards are necessary, etc, etc. Header files and the #include directive are legacy machanisms for resolving C and C++''s forward declaration requirements. They need to go.

quote:
2) one more thing: static variables go into the BSS, so they''re initialized to 0.

...For integer or integer-convertible types. For some user-defined types, the default constructor may be invoked.

Share this post


Link to post
Share on other sites
quote:
Original post by Jan Wassenberg
2) one more thing: static variables go into the BSS, so they're initialized to 0.

BSS is irrelevant to C and C++.

[edited by - SabreMan on December 5, 2002 7:16:35 AM]

Share this post


Link to post
Share on other sites
quote:
Oluseyi
At its simplest. In most medium-to-large sized projects, however, multiple files include the same header and inclusion guards are necessary, etc, etc.

Sure. But the #included files are still just inserted into the source file.

quote:
Oluseyi
Header files and the #include directive are legacy machanisms for resolving C and C++''s forward declaration requirements. They need to go.

What?!

> ...For integer or integer-convertible types. For some user-defined types, the default constructor may be invoked. <
Yes yes.


> 1. BSS is irrelevant to C and C++.
nope.

> 2. BSS is the uninitialised data segment.
weeell - I''d say the name BSS is overused.
In my case, VC stuffs static data into a segment it calls _BSS, which is zeroed out.

Share this post


Link to post
Share on other sites
quote:
Original post by Jan Wassenberg
> BSS is irrelevant to C and C++.
nope.

Yep. It has absolutely no bearing on the semantics of a C or C++ program. I'm led to believe there are platforms supporting C that have no concept of BSS, but the Standard mandates a file-scope static must behave in the same way.
quote:

> BSS is the uninitialised data segment.
weeell - I'd say the name BSS is overused.

I deleted that since my comment was irrelevant to what you said.

[edited by - SabreMan on December 5, 2002 7:27:28 AM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!