Quick Question. Global Vars...

Started by
12 comments, last by Radagar 21 years, 9 months ago
quote:Original post by doctorsixstring
Although it should compile fine, it will not perform correctly. Local variables are created at the start of a function, and destroyed when the function is done. If you assign a pointer to a local variable (which will soon be destroyed) you are left with a dangling pointer (memory leak).


Incorrect. He is using static local vaiables. Static local variables lives on when your function is inactive.
'ROIDRAGE!!! - I wanna work for the chinese communist regime!
Advertisement
quote:Original post by En Dum En
Incorrect. He is using static local vaiables. Static local variables lives on when your function is inactive.


True. This would compile and run fine, but it''s still not a good idea to return a reference or pointer to something that is local, IMO. That defeats the whole point of it being local. If it''s supposed to be global, then it should be global from the start and not just suddenly become global. It''s just bad design.

/*=========================================*/
/* Chem0sh */
/* Lead Software Engineer & Tech Support */
/* http://www.eFaces.biz */
/*=========================================*/
/*=========================================// Chem0sh// Lead Software Engineer & Tech Support// http://www.eFaces.biz=========================================*/
quote:Original post by Chem0sh

True. This would compile and run fine, but it''s still not a good idea to return a reference or pointer to something that is local, IMO. That defeats the whole point of it being local. If it''s supposed to be global, then it should be global from the start and not just suddenly become global. It''s just bad design.


Incidentally, this how one version of the Singleton pattern is implemented.

Oops - I didn''t notice that static there. However, I agree with Chem0sh: It''s just bad design.

This topic is closed to new replies.

Advertisement