Automatic vs. Static Duration
I come from a background in Pascal, where there is no "automatic duration", unless you want to count function data variables that are pushed onto the stack when the function is called. Static duration would mean that global variables, like in an asm program, are thrown in a part of the actual .exe, whether initialized or not.
I'm confused about the meaning of automatic duration in C++. Are the variables actually allocated at runtime? Would that be on the heap or on the stack? I understand that global variables are static the entire program, so I assume they are defined at compile time. I also understand that within any block, variables are either dynamic or automatic duration; meaning that main() itself uses variables that are auto. Does this mean that if I write:
char myStr[] = "abcdefghijklmnopqrstuvwxyz";
in main, or another block, that first space is made on the stack for myStr, and then that string is copied to the stack? As opposed to storing the string at compile-time and pointing myStr to it? Wouldn't this have efficiency draw backs? Or am I misunderstanding the meaning of it?
Thanks for clarifying!
Re: Automatic vs. Static Duration
Quote:
Originally posted by JMB
I come from a background in Pascal, where there is no "automatic duration", unless you want to count function data variables that are pushed onto the stack when the function is called. Static duration would mean that global variables, like in an asm program, are thrown in a part of the actual .exe, whether initialized or not.
I'm confused about the meaning of automatic duration in C++. Are the variables actually allocated at runtime? Would that be on the heap or on the stack? I understand that global variables are static the entire program, so I assume they are defined at compile time. I also understand that within any block, variables are either dynamic or automatic duration; meaning that main() itself uses variables that are auto. Does this mean that if I write:
char myStr[] = "abcdefghijklmnopqrstuvwxyz";
in main, or another block, that first space is made on the stack for myStr, and then that string is copied to the stack? As opposed to storing the string at compile-time and pointing myStr to it? Wouldn't this have efficiency draw backs? Or am I misunderstanding the meaning of it?
Thanks for clarifying!
Basically, when you enter a function block, the compiler creates a stack frame to hold space for the variables declared in that block......In C++, an instance of an object is created when the flow of code first encounters its declaration.....so at that point, the relevant constructors are called, and its values are copied to the stack frame........then when the function ends, the stack frame is discarded...so the values still sit there, but the compiler doesnt guarantee they will stay as they were (chances are they will be overwritten rather quickly) and wont let you access them.....therefore these variables are automatic...they exist in the stackframe created by the function, and when the function ends, the stack frame is no more - therefore the variable is finished....
Interestingly,
Code:
char* sz = "Hello";
and
Code:
char sx[] = "Hello";
are treaded differently.......With the first, the "Hello" is usually copied to a global section (usually const), and the pointer to it (sz) is copied to the stack....with the second, the charectors are each copied to the stack, and the compiler substitutes sx for a pointer to the first char sat on the stack....So with the first, 4 bytes are copied to the stack (assuming 32bit processor and most common systems) but the data cant be altered, with the second, 5 bytes are copied (again assuming chars are 1 byte) and an extra 1 for the NULL char, but they can be altered.....