BP-4-3 Scope and Lifetime

Chapter 04 Procedural Abstraction - Function

4. Scope and Lifetime

4.1 Multi-Module Structure
  • A module is made up of several relevant definitions of program bodies. And a module usually consists of two parts:

    • Module interface gives some definition and declaration on functions and variables that will be used in other modules and we store this part of codes in a header file with .h as a filename extension.

    • Module implementation is the main part and we store these codes in a source file with .cpp as a filename extension. And if we want to use a module interface, we need to use compile preprocessing commands like below.

      #include <file_name.h>
      
  • The basic principle for designing modules is that we should increase cohesion inside the module as much as we can and decrease coupling between modules as much as possible, which is based on structured programming.

4.2 Scope for Identifier
namemeaning
local scopeonly effective from the definition to the end of the compound statement including it
potential scopea local scope excluding another inner local scope with the same name
global scopea kind of linkage that can be used globally and if we want to use it in the inner local scope which has the same name, we need to put a global scope resolution operator :: before it.
file scopeif we want a variable only effective in this file, we can put static before its definition.
function scopea variable defined in a function can only be accessed in this function body
function prototype scopethe parameter of a function declaration in only effective in this function prototype
4.3 Namespace
namespace <name-of-namespace> {
    <definition-of-global-variables/constants/functions>;
}

If we want to use the name in a namespace outside this namespace, we should use namespace + scope resolution operator :: to restrict it.

And if we want use names from one namespace from now on, we could use the following instruction:

using namespace <name-of-namespace>;

Moreover, the following two bricks of code is the same:

namespace {//a namespace without a name can only be used in this file
    int x,y;
}
//-----------
static int x, y;
4.4 Lifetime and Memory Allocation

Lifetime of a variable refers to how long this variable occupies part of the memory.

  • Static lifetime
    • Variables with static lifetime will get a room in the static data part of the memory in the beginning of the program which won’t be withdrawn until the end of the program.
    • The keyword static has two meanings:
      • Change the scope of a global variable into a file scope
      • Give a local variable a static life time, which means this local variable has the features of a global variable to this function. That is to say, you can still use the value at the time of last function call of this variable in the current function call because it is preserved during multiple calls.
  • Automatic lifetime
    • Variables with automatic lifetime get a room in the stack part of the memory only when it is defined and the room will be withdrawn when reaching the end of the compound statement containing it, that is to say when its lifetime in the local scope is over.
    • A local variable’s default lifetime is automatic, but we can also use keyword auto to assign it explicitly.
  • Dynamic lifetime
    • The lifetime of dynamic variables is begin with keyword new or function malloc and end with keyword delete or function free.
    • Variables with dynamic lifetime will be given a room in the heap part of the memory also called free store.

There’s another area in the memory called code, which stores the instructions. And remember that each part in the memory has maximum size which is relevant to the hardware of the computer.

The keyword register means to store the variable in the register of CPU, which makes accessing to it much faster.

Example: Linear Congruential Method for Random Number Production

r[k] = (multiplier * r[k - 1] + increment) % modulus

r[k - 1] is the previous random number, r[k] is the current random number. Modulus points out the range of random numbers, if we choose multiplier and increment carefully and properly, we’ll get quite well-versed random sequence.

The next random number is based on the last random number, so it is necessary to conserve that.

unsigned int random(){
    static unsigned int seed = 1;
    const int modulus = 65536, multiplier = 25173, increment = 13849; 
    seed = (multiplier * seed + increment) % modulus;
    return  seed;
}
4.5 Function Call Based on Stack

Stack is a liner data structure whose number of elements is editable and the addition and delete of its elements can only be done on one end of the stack. Stack has an important feature: Last In First Out (LIFO).

See the source image

  • You can just compare stack in C++ to environment paradigms in Python.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值