Original Post
🔒 Locked
Pointers to global objects, in C
Advertisement
Yes, it's perfectly fine because the address of t will never change.
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()).
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()).
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement