IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> C++ Primer : Memory Models and Namespaces [Progress Check] -> 正文阅读

[C++知识库]C++ Primer : Memory Models and Namespaces [Progress Check]

Memory Models and Namespaces

Separate Compilation of Programs

  • C++ allows and encourages you to locate component functions of a program in separate files.
  • Compile files separately and then link them into the final executable program.
  • header files:
    • function prototypes
    • symbolic constants defined using #define or const
    • structure declarations: no variables created
    • class declaration
    • template declaration
    • inline function
  • How to include header file ?
    • use "coordin.h"
    • NOT use <coordin.h>
    • < >: C++ looks at system’s file system
    • " ": C++ first looks at current working directory or source code director, after that, looks at system’s file.
  • Example of header file
#ifndef COORDIN_H_ // avoid multiple define
#define COORDIN_H_
...

#endif

Storage duration, Scope, and Linkage

Storage Duration

  • Storage categories affect how information can be shared across files.
  • C++ has 4 separate schemes for storing data:
    • Automatic storage duration:
      • Variables declared inside a function definition.
      • Created when program execution enters the function or block.
      • Freed from memory when execution leaves the function or block.
    • Static storage duration:
      • Variables defined outside a function definition or using the keyword static.
      • Persist for the entire time a program is running.
    • Thread storage duration(under C++ 11):
      • Variables declared with the keyword thread_local.
      • Concurrent programming.
    • Dynamic storage duration:
      • Memory allocated by the new operator persists until freed with delete operator or the program ends, whichever comes first.

Scope and Linkage

Automatic Storage Duration

  • Function parameters and variables declared inside a function have automatic storage duration by default.
  • Automatic storage duration means variables can only be seen inside a block or function.
    • variables with the same name, the new definition inside the block will hide the prior definition.
    • prior definition comes out after block or function ends.
int main()
{
    int a = 5; 
    {
        int b = 6; // b only valid in the block
        int a = 7; // a is 7 inside the block
    }
    ... // a is 5 and b does not exist
    ...
    return 0
}
  • To manage automatic variables, C++ uses the Stack, following last-in-first-out

Static Duration Variables

  • Three types:
    • external linkage
    int global = 100;
    
    • internal linkage
    static int one_file = 10;
    
    • no linkage
    void func1(int n)
    {
        static int count = 0;
    }
    
  • Notes:
    • global can be used in this file and other linked files.
    • one_file can only be used in this file.
    • count is defined inside the block and only used by func1. It is like automatic variables but it lasts in the memory longer.
  • All types of static duration variables last for the entire duration of the program.
  • Compiler does not need any special tools to manage static duration variables and just to use a fixed block of memory to hold all of them. If no explicit declaration, the compiler sets it to 0.
  • Initialization: zero-initialization by default.
#include <cmath>
int x; // zero-init
int y=5; // constant-expression init
long z=13*13; // constant-expression init
int w = 2 * sizeof(long) +1; // constant-expression init
const double pi = 4.0 * atan(1.0); // dynamic init, because of callin atan()
Static Duration, External Linkage
  • External Variable:
    • variables with external linkage.
    • defined above main() function or in header file.
    • also called global variable, compared with automatic variables (local variable)
  • One Definition Rule
    • there can be only one definition of a variable.
      • defining declaration
        • it causes storage for the variable to be allocated.
      • referencing declaration
        • not cause storage to be allocated and only refers to an existing variable.
    • referencing declaration: extern:
    double up;  // definition, up is 0
    extern int blem;  // blem is defined elsewhere.
    extern char gr = 'z';  // definition (initialized)
    
Static Duration, Internal Linkage
  • static modifier: file-scope variable with internal linkage.
// file 1
int error = 20; // external declaration

// file 2
static int error = 5; // known only in file 2

// wrong!!!
int error = 5; // violate one definition rule

Placement new: Dynamic Allocation

  • new & delete
  • The compiler uses 3 separate memory chunks:
    • static variable
    • automatic variable
    • dynamic storage

Namespaces

Create Named Namespace

  • The same name defined in 2 namespace will not conflict with each other
namespace Jack {
    double pail; // variable declaration
    void fetch(); // function declaration
    int pal; // variable declaration
    struct Well {...}; // struct declaration
}

namespace Jill {
    double bucket (double n) {...}
    double fetch;
    int pal;
    struct Hill {...};
}
  • Name declared inside a namespace has external linkage by default.

using Declaration and using Directives

using Jill::fetch; // declaration
using namespace Jill; // directive
  • using declaration: make a single name available
  • using directives: make all names inside the namespace available.
  • It is safer to using declaration.
namespace Jill {
    double bucket(dounle n) {...}
    double fetch;
    struct Hill {...}
}

char fetch; // global namespace

int main() {
    using namespace Jill; // import all namespace names
    Hill Thrill; // create a type Jill::Hill
    double fetch; // hide Jill::fetch

    // local fetch
    cin >> fetch;

    // global fetch
    cin >> ::fetch;

    // Jill::fetch
    cin >> Jill::fetch;
}
  • DON’T use using directives in the header file !!!
    • the order of the header files may affect behavior!!!
  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2021-08-24 15:22:34  更:2021-08-24 15:23:37 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年12日历 -2024/12/27 6:06:32-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码
数据统计