[CPP] Stack locals, globals and statics

We have covered these topics again and agin in “Expressions” and “Variables” but after the class today, i am willing to share some more useful knowledge and interesting examples with you.

Process structure in memory

enter image description here

Stack

Function calls (stack frames) are stored on a stack in memory.
Note that most stacks go down in memory addresses, i.e. the stack frame for the new function is lower in memory

stack frame

when a function call is made, necessary information is collected together
  • Who called the function? So the program knows where to return to when the function ends.
  • What parameters were supplied?
  • Space to put the return value?
  • Somewhere to store local variables while they are needed
    All these values are stored together in a ‘stack frame’

Lifetime of local variables

enter image description here
  • The local variables for function 2 and function 3 are overwritten by those for function 4
  • The local variables for function 4 are overwritten by those for function 5
  • Your local variables only exist for as long as the block in which they are defined
Don’t access local variables after they are destroyed–e.g. through pointers.
Since my local variables get destroyed, where can i put things i need to keep?

Global and static local variables

Global variables

variable declared outside of all functions and available to all functions in the file.
Global variables last for the duration of the program and local variables just last for the duration of the block they are defined within.

Static local variables

local variables can be static
  • means not moving/unchanging
  • not the same as static member variables!
  • not the same as const
    Like global variables, static local variables remember their value between function calls. But you can only access them (by name) inside the one function they are defined in.(Unless you keep a pointer to them)

Example

enter image description here
Static variable remembers its value. Initialization only occurs in the first function call. Non-static creates a new variable for each call. Initialization once for each new variable/function call.
enter image description here

another interesting example

enter image description here
enter image description here
Why does this happen? Because we visit a local variable after the function call.
  • Don’t refer to data on the stack outside the function(this means local variables or actual parameters)
  • Things to avoid
  • Returning a pointer to a local variable or parameter
    1. Storing the address of a local variable or parameter
    2. You can refer to them, but should not!
  • The variable no longer exists, and the memory may be reused
  • Using your pointer will corrupt whatever is using the memory now

Variable shadowing(putting other variables with the same name into the shadows)

enter image description here
enter image description here

Sharing things between files

Global variables and functions are always accessible from anywhere within the same file.
You can hide them from other files by using the static keyword, e.g.
static int g_hidden = 1;
They are then accessible everywhere within the same file but not from other files.
If not static(hidden), then:
  • You can access global functions from other files
  • You can access global variables from other files
  • (use keyword ‘extern’ in a declaration)
  • extern changes a definition into a declaration

Summary for static

  1. For local variables:
    Static means the value is maintained between function calls.
  2. For global variables:
    Static limits visibility/access to within the file
  3. For C++ and Java(not c) classes
    Method or variable is associated with the class not the object

评论

此博客中的热门博文

[MLE] W2 Multivariate linear regression

[MLE] W1 Introduction

[AIM] MetaHeuristics