510 likes | 637 Vues
This chapter delves into the organization of C++ program structures through practical examples like the grep function. It covers essential concepts such as the compilation process, precedence and associativity of operators, as well as external and internal objects. Key topics include scope and lifetime of variables, the role of the preprocessor, and function definitions, specifically focusing on getline and strindex. The structure of the program is illustrated with pseudocode and the importance of function declarations prior to utilization is emphasized for correct type checking during compilation.
E N D
Chapter 4 functions and program structure Speaker: Lung-Sheng Chien
OutLine • Example: grep - organization of program structure- compilation process • Precedence and Associativity • External/Internal objects • Scope and Lifetime • Preprocessor (前處理器)
grep – print lines matching a pattern Ah Love! could you and I with Fate conspire To grasp this sorry Scheme of Things entire, Would not we shatter it to bite -- and then Re-mould it nearer to the Heart’s Desire! Search for the pattern “ould” Ah Love! could you and I with Fate conspire Would not we shatter it to bite -- and then Re-mould it nearer to the Heart’s Desire!
Structure of the program Function getline Pseudocode while ( there is another line) if ( the line contains the pattern ) then print it endif endwhile Function strindex Function printf File getline.cpp File strindex.cpp File main.cpp function strindex function getline function main (driver) call
grep project – getline function [1] Project Add to Project New 1. project grep 2. source file getline.cpp
grep project – getline function [2] 1. Source file getline.cpp Q1 Q2 definition of getline Question 1: why we need input parameter lim? Question 2: what is execution order in predicate of while-loop?
grep project – strindex function [1] Project Add to Project New 1. project grep 2. source file strindex.cpp
grep project – strindex function [2] source file strindex.cpp definition of strindex Question: what is procedure of function strindex ?
grep project – main function [1] Project Add to Project New 1. project grep 2. source file main.cpp
grep project – main function [2] declaration, not definition source file main.cpp remember to add new line character Question: who does function main return found to ?
grep project – in Linux machine Upload project grep by sftp first 1. compile 3 source files 2. feed article.txt into a.out article.txt Ah Love! could you and I with Fate conspire To grasp this sorry Scheme of Things entire, Would not we shatter it to bite -- and then Re-mould it nearer to the Heart’s Desire! 3. 上一個指令的傳回值
“grep” is embedded in Linux machine Search manual of grep 1. pattern 2. File
Function definition 白話文 return-type function-name ( argument declarations) { declarations and statements } 文言文 (grammar, page 234, 235) function-definition: declaraction-specifiers(opt)declarator declaraction-list(opt) compound-statement declarator: pointer(opt) direct-declarator declaration-specifier: storage-class-specifier declaration-specifier(opt) type-specifier declaration-specifier(opt) type-qualifier declaration-specifier(opt) direct-declarator: Identifier direct-declarator( parameter-type-list )
definition of main function [1] declaraction-specifiers declarator compound-statement type-specifiers
definition of main function [2] declarator: Pointer(opt) direct-declarator direct-declarator: direct-declarator ( parameter-type-list ) Identifier compound-statement: { declaration-list statement-list }
Why we need declaration of getline and strindex in main.cpp Compiler 是逐個檔案作編譯, 當他讀main.cpp 且看見 getline(line, MAXLINE) 時需作 type checking, 可是 getline 的 defintion 是在 getline.cpp 內, 並不在 main.cpp, 所以我們需在 call getline 之前宣告 getline 的原型, 注意: compiler 只讀檔一次, 所以必需在compiler作 type checking 之前就告訴他函數的原型
編譯順序 -- [1] main.cpp 秀出編譯過程 c++ 編譯器 編譯為組合語言 先編譯 main.cpp 翻譯組語成 object code, ccEg6xsf.o檔 GNU assembler
編譯順序 -- [2] getline.cpp c++ 編譯器 編譯為組合語言 再編譯 getline.cpp 翻譯組語成 object code, ccnNjsxn.o檔
編譯順序 -- [3] strindex.cpp c++ 編譯器 編譯為組合語言 最後編譯 strindex.cpp 翻譯組語成 object code, ccQJUtuu.o檔
連接程序 – linking process Collect2 被作為ld (GNU linker 連接器 ) strindex.cpp main.cpp getline.cpp Source code Compiler g++ / icpc Assembler as Linker collect2 / ld Executable file a.out Assembly code .s file object code .o file
OutLine • Example: grep • Precedence and Associativity • External/Internal objects • Scope and Lifetime • Preprocessor (前處理器)
Precedence and Associativity of operators [1] Execution order Q2 Question 2: what is execution order in predicate of while-loop?
Precedence and Associativity of operators, page 53 [2] arithmetic, 先乘除後加減 relational logical assignment Precedence : () > arithmetic > relational > logical > assignment
Parsing tree of • Execution order • bottom-up • Left subtree right subtree associativity of && is left to right != is higher than && > is higher than && != is higher than && -- is higher than > () is higher than !=
Three equivalent coding style 用括號表示優先次序 c is well-defined
OutLine • Example: grep • Precedence and Associativity • External/Internal objects • Scope and Lifetime • Preprocessor (前處理器)
External/internal objects [1] • External objects- external variable (global variable): defined outside any function- functions • Internal objects- variables inside function without declaring static- parameters of a function Grammar in page 234 translation-unit: external-declaration translation-unit external-declaration external-declaration: function-definition declaration declaration: declaration-sepcifiers init-declarator-list(opt)
External/internal objects [2] file main.cpp External variable’s definition Local variables External object: function main
Local variables External object: function strindex file getline.cpp file strindex.cpp Local variables External object: function getline
OutLine • Example: grep • Precedence and Associativity • External/Internal objects • Scope and Lifetime • Preprocessor (前處理器)
Scope and visibility • The scope of a name is the part of the program within which the name can be used. • For automatic variable (automatic storage) declared at the beginning of a function, the scope is the function in which the name is declared. • Local variables of the same name in different functions are unrelated. The same is true of the parameters of the function, which are local variables also. • The scope of an external variable or a function lasts from the point at which it is declared to the end of the file being compiled. • External variable in file1 can be accessed by functions in file2 if it is declared by keyword extern in file2. • staticexternal variable is encapsulated inside a file1, no other functions of file2 can access it.
Scope graph Level 0 Level 1 Level 2 block scope
Lifetime (生命周期) [1] • “Lifetime” is the period during execution of a program in which a variable or function exists. The storage duration of the identifier determines its lifetime. • Identifiers with static storage duration (global lifetime) are alive during (main) program execution and die when program terminates.Example: global variable, static variable • Identifiers, which are defined inside a function, with automatic storage duration(local lifetime) is allocated new storage each time the function is called and die when function returns.Example: local variable, function parameter • Although an identifier with a global lifetime exists throughout the execution of the program, it may not be visible in all parts of the program.
MSDN Library: search “extern” extern and static [1] • A variable declared with the externstorage-class specifier is a reference to a variable with the same name defined at the external level in any of the source files ofthe program. • The internal extern declaration is used to make the external-level variable definition visible within the block. • Unless otherwise declared at the external level, a variable declared with the extern keyword is visible only in the block in which it is declared. global variable
extern and static [2] file main.cpp definition file strindex.cpp declaration
OutLine • Example: grep • Precedence and Associativity • External/Internal objects • Scope and Lifetime • Preprocessor (前處理器)
CProProcessor (cpp) [1] • File inclusion#include “filename”#include <filename> • Macro (巨集) substitution#define forever for ( ; ; ) // infinite loop#define square(x) (x)*(x) • Conditional inclusion#ifndef _INC_STDIO#define _INC_STDIO /* content of stdio.h */#endif /* _INC_STDIO */
CPreProcessor (cpp) [2] Source code C preprocessor cpp Compiler g++ / icpc Assembler as Linker collect2 / ld Executable file a.out Assembly code .s file object code .o file
CPP – macro substitution Question: Why no max operation in math.h cpp handles this but the compiler don’t see this statement
CPP - #include “filename” [1] cpp 會複製 stdio.h 到此檔案以便 compiler 作 type checking cpp 將替換 HELLO_STRING 輸出至檔案main_cpp.txt 讀檔案 main_cpp.txt一次一頁
CPP - #include “filename” [2] line 28, include feature.h 按空白鍵接下一頁 Recursively file inclusion, 需滿足因果律
CPP - #include “filename” [3] standard input standard output
CPP – conditional inclusion [1] 如果巨集 _WIN32 或巨集 __WIN32__ 有定義, 則執行程式碼 printf("This is Win32 Application\n"); 否則執行程式碼 printf("This is Linux Application\n"); Result in Visual Studio Result in Linux
CPP – conditional inclusion [2] Macro _WIN32 is always defined in Visual Studio
CPP – causality (因果律) [1] add header file getline.h content of getline.h
CPP – causality (因果律) [2] add header file strindex.h content of strindex.h
CPP – causality (因果律) [3] Question 1: why we have infinite recursion? Question 2: How to solve infinite recursion?
CPP – causality (因果律) [4] getline.h strindex.h getline.cpp strindex.cpp