open-courses
  • 公开课笔记
  • CMU 15-445/645 Database Systems
    • Relational Data Model
    • Advanced SQL
    • Database Storage
    • Buffer Pools
    • Hash Tables
    • Tree Indexes
    • Index Concurrency Control
    • Query Processing
    • Sorting&Aggregations
    • Join Algorithms
    • Query Optimization
    • Parallel Execution
    • Embedded Database Logic
    • Concurrency Control Theory
    • Two Phase Locking
    • Timestamp Ordering Concurrency Control
    • Multi-Version Concurrency Control
    • Logging Schemes
    • Database Recovery
    • Introduction to Distributed Databases
    • Distributed OLTP Databases
    • Distributed OLAP Databases
  • UCB - CS162
    • OS intro
    • Introduction to the Process
    • Processes, Fork, I/O, Files
    • I/O Continued, Sockets, Networking
    • Concurrency: Processes & Threads
    • Cooperating Threads, Synchronization
    • Semaphores, Condition Variables, Readers/Writers
    • Scheduling
    • Resource Contention & Deadlock
    • Address Translation, Caching
    • File System (18,19,20)
    • Distributed Systems, Networking, TCP/IP, RPC (21,22)
    • Distributed Storage, Key-Value Stores, Security (23)
    • Security & Cloud Computing (24)
    • Topic: Ensuring Data Reaches Disk
  • MIT - 6.006
    • Sequence and Set Interface
    • Data Structure for Dynamic Sequence Interface
    • Computation Complexity
    • Algorithms and Computation
    • Structure Of Computation
    • Graph & Search
    • Tree & Search
    • Weighted Shortest Paths
    • String Matching, Karp-Rabin
    • Priority Queue Interface & Implementation
    • Dictionary Problem & Implementation
    • Sorting
    • Dynamic Programming
    • Backtracking
    • Self-Balancing Tree
  • MIT - 6.824
    • 2PC & 3PC
    • Introduction and MapReduce
    • RPC and Threads
    • Primary/Backup Replication
    • Lab: Primary/Backup Key/Value Service
    • Google File System (GFS)
    • Raft
    • Lab: Raft - Leader Election
    • Lab: Raft - Log Replication
  • Stanford-CS107
    • 原始数据类型及相互转化
    • 指鹿为马
    • 泛型函数
    • 泛型栈
    • 运行时内存结构
    • 从 C 到汇编
    • 函数的活动记录
    • C 与 C++ 代码生成
    • 编译的预处理过程
    • 编译的链接过程
    • 函数的活动记录续、并发
    • 从顺序到并发和并行
    • 信号量与多线程 1
    • 信号量与多线程 2
    • 复杂多线程问题
    • 函数式编程 - Scheme 1
    • 函数式编程 - Scheme 2
    • 函数式编程 - Scheme 3
    • 函数式编程 - Scheme 4
    • 函数式编程 - Scheme 5
    • Python 基础
  • MIT - 6.001 - SICP
    • 什么是程序
    • 程序抽象
    • 替代模型
    • 时间/空间复杂度
    • 数据抽象
    • 高阶函数
    • Symbol
    • 数据驱动编程与防御式编程
    • 数据抽象中的效率与可读性
    • 数据修改
    • 环境模型
    • 面向对象-消息传递
    • 面向对象 - Scheme 实现
    • 构建 Scheme 解释器
    • Eval-Apply Loop
    • Normal Order (Lazy) Evaluation
    • 通用机
    • 寄存器机器
    • 子程序、栈与递归
    • 在寄存器机器中执行
    • 内存管理
  • MIT - 6.046
    • Randomized Algorithms
    • Skip Lists
  • System Design
    • Twitter
    • Cache Consistency & Coherence
  • DDIA 笔记
    • Replication
    • Transactions
    • The Trouble with Distributed Systems
    • Consistency & Consensus
  • Papers We Love
    • Consistent Hashing and Random Trees (1997)
    • Dynamic Hash Tables (1988)
    • LFU Implementation With O(1) Complexity (2010)
    • Time, Clocks, and the Ordering of Events in a Distributed System (1978)
    • Dapper, a Large-Scale Distributed Systems Tracing Infrastructure (2010)
    • Gorilla: A Fast, Scalable, In-Memory Time Series Database (2015)
  • Release It 笔记
    • Anti-patterns & Patterns in Microservice Architecture
  • Database Design
    • Log Structured Merge (LSM) Tree & Usages in KV Stores
    • Prometheus
Powered by GitBook
On this page
  • Reference 与 Pointer
  • c++ Class
  1. Stanford-CS107

C 与 C++ 代码生成

第十一课

Reference 与 Pointer

Swap Example

C code generation

void foo() {
    int x;
    int y;

    x = 11;
    y = 17;

    swap(&x, &y);
}

void swap (int *ap, int *bp) {
    int temp = *ap;
    *bp = *ap;
    *ap = temp;
}
  • 调用 foo

foo 函数执行时,运行时中的函数栈如下图所示:

saved pc 中保存着 foo 执行完成后返回的地址。前三行初始化 foo 函数的局部变量,中间省略调用 swap 函数的过程,在最后一行回收局部变量,sp 重新指向 saved pc 然后回到调用地址。

  • 调用 swap

swap 函数执行时,运行时中的函数栈如下图所示:

在调用 swap 函数之前,初始化 swap 函数的参数 ap 和 bp,然后才开始调用 swap,中间省略 swap 函数执行的过程,在最后一行回收局部变量,sp 重新指向 saved pc 然后回到调用地址。

  • 执行 swap

swap 函数执行,首先分配局部变量 temp,完成交换功能后,回收局部变量 temp 并回到 saved pc 所指的调用地址。

C++ code generation

void swap(int& a, int& b) {
    int temp = a;
    a = b;
    b = temp;
}

实际上,c++ swap 编译后的结果和 C 代码编译后的结果完全一致。只是 C++ 在编译过程中,识别了 reference 语法,完成相应的代码转换,使得最终代码与 C 版本代码生成的结果一致。

Simpler Example

c code

int x = 17;
int y = x;
int *z = &y;

c++ code

int x = 17;
int y = x;
int& z = y;

二者编译后的结果是一样的,三个变量的关系如下图所示:值得一提的是,reference 和 pointer 的区别在于,reference 一旦绑定,就无法更改。如本例所示,在 c++ 代码中,一旦 z 指向 y,z将无法被修改;而在 c 代码中,指针可以重新赋值。

c++ Class

c++ code

class binky {
    public:
        int dunky(int x, int y);
        char * minky(int *z) {
            int w = *z;
            return slinky + dunky(winkey, winkey);
            // return this.slinky + this.dunky(this.winkey, this.winkey);
        }
    private:
        int winky;
        char *blinky;
        char slinky[8];
}

int n = 17;
binky b;
b.minky(&n); // binky::minky(&b, &n);

class instance 在调用 instance method 的时候,会自动将实例的指针作为第 1 个参数传给 instance method,既 b.minky(&n) 实际调用过程是 binky::minky(&b, &n), 其运行时的函数栈如下图所示:

c code 和 c++ code 只是在 assembly code 上加的一层语法。一旦 c 和 c++ 编译成 assembly,所有高级语言的概念如 class、reference、pointer、template 都将消失。

参考资料

Previous函数的活动记录Next编译的预处理过程

Last updated 6 years ago

Stanford CS107: lecture 11