Jump to content
  • Advertisement
Sign in to follow this  
DevFred

[C] static types?

This topic is 3257 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 have a data structure that uses a recursive node type. Can I hide this type from other translation units? static struct node... does not work.

Share this post


Link to post
Share on other sites
Advertisement
In C the keyword "static" modifies the variable instance not the variable type.

Preserves variable value to survive after its scope ends.

If you want to hide a type from other translation units, don't put the definition of that type inside any header that you include in other translation units. If you want to limit that type definition to a single translation unit, declare it in the ".c" file of that unit, at the top of the file or at least before the point where it's used. Avoid using that type as an argument to any function that you want to export from that translation unit.

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
Why does it even matter? Types don't have linkage in C.

Oh... well, nevermind then :)

Share this post


Link to post
Share on other sites

struct node
{
struct node *next;
int size;
};

#define HEAP_SIZE 1000

static struct node heap[HEAP_SIZE];

As I understand it, static objects are zero-initialized in C*. Does that mean that heap.next will be the null pointer, or will it be sizeof(struct node*) zero bytes? Will (heap.next == 0) yield 1 or 0?

*Yes I know, globals are always static, even without the static :-) I just don't want heap to escape the translation unit.

Share this post


Link to post
Share on other sites
Pointers with static storage duration without an explicit initializer will be initialized to the null pointer.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!