Click here to Skip to main content
15,911,531 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: static variable Pin
George_George25-Aug-07 21:25
George_George25-Aug-07 21:25 
GeneralRe: static variable Pin
codeII24-Aug-07 1:49
codeII24-Aug-07 1:49 
GeneralRe: static variable Pin
George_George25-Aug-07 21:27
George_George25-Aug-07 21:27 
AnswerRe: static variable Pin
David Crow24-Aug-07 6:05
David Crow24-Aug-07 6:05 
GeneralRe: static variable Pin
George_George25-Aug-07 21:29
George_George25-Aug-07 21:29 
AnswerRe: static variable Pin
ghle26-Aug-07 3:42
ghle26-Aug-07 3:42 
GeneralRe: static variable Pin
George_George26-Aug-07 6:42
George_George26-Aug-07 6:42 
GeneralRe: static variable [modified] Pin
ghle26-Aug-07 10:00
ghle26-Aug-07 10:00 
George_George wrote:
You mean global static variable and function local variable are handled in the same way by compiler?


George, both the global statics and local statics are placed onto the heap - by the compiler. I'd say they are handled similar, but not identical. For example, global statics are initialized at start-up of the program, but local statics are not initialized until they first come into scope. Both are destroyed only upon termination of the program, that is when the heap is destroyed.

You said "function local variable", but I mean function local STATIC variable. A function local variable - non-static - are pushed onto the stack not put in the heap. When the function goes out of scope, so do it's variables. Returning from the function to the calling program will pop the stack, removing all local non-static variables from existence. But the function static variables were put on the heap, and they still exist after the function goes out of scope, available to the routine/method once it comes back into scope from another call. (And as noted earlier in this thread, I found local statics were available from outside of the local function if the address of the static is returned by the method.)


Heap
Global statics
Main() statics
Method B statics
Method C statics
/Heap
<br />
Stack after calling Main()<br />
Main() non-static variables<br />
/Stack<br />
<br />
Stack after calling method B from Main()<br />
Main() non-static variables<br />
Method B non-static variables<br />
/Stack<br />
<br />
Stack after calling method C from B<br />
Main() non-static variables<br />
Method B non-static variables<br />
Method C non-static variables<br />
/Stack<br />
<br />
Stack after calling method C from method C (recursion)<br />
Main() non-static variables<br />
Method B non-static variables<br />
Method C non-static variables<br />
Susequent method C non-static variables<br />
/Stack<br />
<br />
Stack after returning from method C to method B<br />
Main() non-static variables<br />
Method B non-static variables<br />
/Stack<br />
<br />
Stack after returning to Main() from method B<br />
Main() non-static variables<br />
/Stack

Heap after returning to Main().  All statics now initialized.
Global statics
Main() statics
Method B statics
Method C statics
/Heap



Gary



-- modified at 8:43 Monday 27th August, 2007
GeneralRe: static variable Pin
George_George26-Aug-07 19:22
George_George26-Aug-07 19:22 
Questionregistry cleaner for window xp Pin
niki dutta23-Aug-07 18:26
niki dutta23-Aug-07 18:26 
AnswerRe: registry cleaner for window xp Pin
User 58385223-Aug-07 19:18
User 58385223-Aug-07 19:18 
AnswerRe: registry cleaner for window xp Pin
_AnsHUMAN_ 23-Aug-07 20:04
_AnsHUMAN_ 23-Aug-07 20:04 
AnswerRe: registry cleaner for window xp Pin
ThatsAlok23-Aug-07 20:11
ThatsAlok23-Aug-07 20:11 
AnswerPlease continue on the same thread !! Pin
toxcct23-Aug-07 21:57
toxcct23-Aug-07 21:57 
AnswerRe: registry cleaner for window xp Pin
Hamid_RT24-Aug-07 2:51
Hamid_RT24-Aug-07 2:51 
AnswerRe: registry cleaner for window xp Pin
David Crow24-Aug-07 6:18
David Crow24-Aug-07 6:18 
QuestionMFC Dialog I/O Pin
alberthyc23-Aug-07 17:53
alberthyc23-Aug-07 17:53 
AnswerRe: MFC Dialog I/O Pin
Nishad S23-Aug-07 21:21
Nishad S23-Aug-07 21:21 
AnswerRe: MFC Dialog I/O Pin
toxcct23-Aug-07 22:00
toxcct23-Aug-07 22:00 
GeneralRe: MFC Dialog I/O Pin
alberthyc24-Aug-07 4:01
alberthyc24-Aug-07 4:01 
AnswerRe: MFC Dialog I/O Pin
ghle26-Aug-07 11:00
ghle26-Aug-07 11:00 
QuestionHelp on creating .exe for another PC that has no Visual Studio Pin
Llasus23-Aug-07 16:01
Llasus23-Aug-07 16:01 
AnswerRe: Help on creating .exe for another PC that has no Visual Studio Pin
Llasus23-Aug-07 20:14
Llasus23-Aug-07 20:14 
GeneralRe: Help on creating .exe for another PC that has no Visual Studio Pin
ShilpiP23-Aug-07 21:11
ShilpiP23-Aug-07 21:11 
GeneralRe: Help on creating .exe for another PC that has no Visual Studio Pin
Arun krishnan23-Aug-07 21:34
Arun krishnan23-Aug-07 21:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.