Jump to content
  • Advertisement
Sign in to follow this  
tekmoto

Pointers to global objects, in C

This topic is 4371 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

In C, is it OK to have a pointer to a global object? E.g.,: /////////////// Code ////////////// typedef struct _tagAccount { int amt; } Transaction ; Transaction t; Transaction* pTrans; void main() { pTrans = &t; }

Share this post


Link to post
Share on other sites
Advertisement
Yes.

Alternatively, if you *only* really need(/want) the *pointer* to be global, and just want "a single object... that is accessible through a pointer, for the lifetime of the program", you could set it up to point to a local object in main(). That also allows you to do setup/teardown work in main() and thus gain some control over the order of initialization.

*Do not* try to free() the global object via the global pointer. (Or a local-to-main() object for that matter). Only free() what you malloc() (or calloc()).

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!