Variables

Where are they in memory?

notion image
  • stack (栈)
    • local vars
  • heap (堆)
    • dynamically allocated vars
  • code/data
    • global vars
    • static global vars
    • static local vars

Global vars

  • vars defined outside any functions
  • can be shared between .cpp files
  • extern

Static

Global vars

  • static global variable inhibits access from outside the .cpp file 只能在本模块中使用
  • so as the static function

Local vars

  • static local variable keeps value in between visits to the same function
  • is initialized at its first access

Summary

  • for global stuff
    • access restriction
  • for local stuff
    • persistence

Pointers to objects

Operators with pointers

  • get address ps = &s
  • get the object (*ps).length()
  • call the function ps->length()

Two ways to access

  • string s;
    • s is the object itself
    • At this line, object s is created and initialized
  • string *ps;
    • ps is a pointer to an object
    • the object ps points to is not known yet.

Reference

Defining references

  • type& refname = name;
    • For ordinary variable definitions
    • An initial value is required
  • type& refname
    • In parameter lists or member variables 在函数参数表里或者类型的字段里
    • Binding defined by caller or constructor

Rules of references

  • References must be initialized when created 引用对象在创建时必须初始化,用=
  • Initialization establishes a binding
    • definition
    • Bindings don’t change at run time, unlike pointers
    • Assignment changes the object referred-to
      • The target of a non-const reference must be an lvalue(左值).

      Example

      Type restrictions

      • No references to references
      • No pointers to references, but reference to pointer is ok
        • int&* p; // illegal
        • void f(int*& p); // ok
      • No arrays of references

      Summary

      Pointers vs. References

      Pointers
      • independent of the bound object, can be uninitialized
      • can be bound to a different object
      • can be set to null
      References
      • dependent on the bound object, just an alias, must be initialized
      • can't be rebound.
      • can't be null

      Dynamically allocated memory

      Dynamic memory allocation

      • new expression
        • new int;
        • new Stash;
        • new int[10];
      • delete expression
        • delete p;
        • delete[] p; delete数组
      ‼️
      • malloc 只是单纯分配内存,但是 new 在分配完内存后,会确保构造函数被执行,从而会进行初始化
      • freedelete 也是一样的, free 只是释放内存,但是 delete 在释放内存前会执行析构函数

      Tips for new and delete

      • Don't mix-use new/delete and malloc/free.
      • Don't delete the same block of memory twice.
      • Use delete (no brackets) if you've used new to allocate a single entity.
      • Use delete[] if you've used new[].
      • delete the null pointer is safe (nothing happens).

      Const

      Constants

      • Constants are like variables
        • 遵循作用域规则
        • const类型修饰符声明
      • C++中的const默认具有内部链接属性
        • 编译器会尽量避免为const创建存储空间,而是将其值保存在符号表中
        • extern关键字会强制为其分配存储空间

      Run-time constants

      Pointers with const

      Pointers and constants

      notion image
       
      Loading...