C-PRIMER PLUS读书笔记

第九章 内存模型和名称空间

9.1 单独编译

可以把原程序分为3个部分

  • A header file that contains the structure declarations and prototypes for functions using those structures --coordin.h

    通过使用#ifndef...

            #endif来避免头文件重复包含,实际工作是 make it ignore the contents of all but the first inclusion

            

  • A source code file that contains the code for the structure-related functions--file2.cpp

  • A source code file that contains the code that calls upon those functions--file1.cpp

9.2 存储持续,作用域和链接性(Storage Duration,Scope,and Linkage

  • Variables declared inside a function definition have automatic storage duration; that includes function parameters. They are created when program execution enters the function or block in which they are defined, and the memory used for them is freed when execution leaves the function or block. C++ has two kinds of automatic storage duration variables.

  • Variables defined outside of a function definition or else by using the keyword static have static storage duration. They persist for the entire time a program is running. C++ has three kinds of static storage duration variables.

  • Memory allocated by the new operator persists until freed with the delete operator or until the program ends, whichever comes first. This memory has dynamic storage duration and sometimes is termed the free store.

9.2.1作用域和链接

作用域(scope)描述了名称在文件(翻译单元)的多大范围内可见。

链接性(linkage)描述了名称如何在不同单元内共享。

9.2.2自动存储持续

9.2.3静态持续变量

Table 9.1. The Five Kinds of Variable Storage
Storage DescriptionDurationScopeLinkageHow Declared
automaticautomaticblocknonein a block (optionally with the keyword auto)
registerautomaticblocknonein a block with the keyword register
static with no linkagestaticblocknonein a block with the keyword static
static with external linkagestaticfileexternaloutside of all functions
static with internal linkagestaticfileinternaloutside of all functions with the keyword static

全局还是局部?

通常情况下,应使用局部变量,应在需要知晓时才传递数据,而不应不加区分地使用全局变量来使数据可用。

2。静态持续,内部链接性

只有一个文件包含了该变量的外部定义,其他文件要使用该变量,必须在引用声明中使用关键字extern

REMEMBER:

In a multifile program, you can define an external variable in one and only one file. All other files using that variable have to declare that variable with the extern keyword 否则使用static来隐藏外部变量。

9.2.4说明符和限定符

The keyword static, when used with a file-scope declaration, indicates internal linkage. When used with a local declaration, it indicates static storage duration for a local variable.

The keyword extern indicates a reference declaration, that is, that the declaration refers to a variable defined elsewhere

9.2.5 函数和链接性

You also can use the keyword static to give a function internal linkage, confining its use to a single file. You would apply this keyword to the prototype and to the function definition

9.2.6语言链接

 

extern "C" void spiff(int);   // use C protocol for name look-up
extern void spoff(int);       // use C++ protocol for name look-up
extern "C++" void spaff(int); // use C++ protocol for name look-up

The first uses C language linkage. The second and third use C++ language linkage. The second does so by default, and the third explicitly.

9.2.7存储方案和动态分配

9.3 名称空间

You might want the List class from one library and the Tree from the other, and each might expect its own version of Node. Such conflicts are termed namespace problems.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值