c++ 笔记(一)基础篇
c 基础知识编译与执行流程函数指针指针函数回调函数异常处理函数模板与类模板泛型编程函数模板类模板文件IO强制类型转换const_caststatic_castreinterpert_castdynamic_cast编译与执行流程c编译过程 预处理–编译–汇编–链接预处理阶段输入源代码文件.cpp、.h。输出预处理后的文件.i。主要任务处理#include指令将头文件内容插入到源文件中。处理#define宏定义进行文本替换。处理条件编译指令如#if、#ifdef。删除注释。编译阶段输入预处理后的文件.i。输出汇编代码文件.s。主要任务词法分析将源代码分解为词法单元如标识符、关键字、运算符等。语法分析生成抽象语法树AST。语义分析检查类型、作用域等语义规则。优化对代码进行优化如常量折叠、死代码消除。生成汇编代码。汇编输入汇编代码文件.s。输出目标文件.o 或 .obj。主要任务将汇编代码转换为机器指令。生成目标文件包含机器码、符号表、重定位信息等。链接输入目标文件.o 或 .obj和库文件.lib 或 .a。输出可执行文件.exe 或 .out。主要任务符号解析将未定义的符号如函数、变量与定义关联。重定位调整代码中的地址引用。合并目标文件和库文件生成最终的可执行文件。程序运行的过程 加载–初始化–执行–终止加载操作系统将可执行文件加载到内存中。分配内存空间代码段、数据段、堆、栈等。初始化全局变量和静态变量初始化。调用main函数之前的初始化代码如C全局对象的构造函数。执行从main函数开始执行程序逻辑。动态分配内存堆、函数调用栈等。终止调用main函数之后的清理代码如C全局对象的析构函数。释放资源程序退出。内存分布代码区 (Text Segment)存放编译后的CPU机器指令函数体二进制代码。 只读共享。生命周期是整个程序运行期间由操作系统管理防止程序意外修改指令。全局/静态区 (Data Segment)细分为 .data已初始化、.bss未初始化、.rodata只读常量。存放全局变量、静态变量static 和常量如字符串常量。 生命周期是整个程序运行期间。程序结束时由操作系统回收。栈区 (Stack)存放函数的局部变量、函数参数、返回地址等。由编译器自动管理后进先出。函数调用时分配返回时自动释放。注意不要返回局部变量的地址指针/引用因为其内存已被回收。堆区 (Heap) 存放由程序员通过 new / malloc 动态分配的内存。由程序员手动管理生命周期由 new 开始到 delete 结束。若忘记释放会导致内存泄漏程序结束时由OS回收。这是C中最灵活也最易出错的内存区域。内存映射区Memory-mapped Segment存储动态链接库、文件映射等。函数指针函数指针指的是 “指向函数”的指针存储的是函数在内存中的地址。通过函数指针可以在运行时动态的选择要调用的函数。例如回调函数这种。语法返回值类型 (*指针名称)(参数列表);intadd(inta,intb){returnab;}int(*func_ptr)(inta,intb);intmain(){//将add函数地址赋值给func_ptrfunc_ptradd;//通过func_ptr来调用此函数intresultfunc_ptr(10,20);std::coutresult:resultstd::endl;return0;}指针函数表示函数的返回值是指针。语法:返回值类型* 函数名称(参数列表);int*add(inta,intb){int*cnewint(ab);returnc;}intmain(){int*resultadd(10,20);std::coutresult:*resultstd::endl;if(result!nullptr){delete result;}return0;}回调函数回调函数是一种特殊的函数可以作为参数传递。相当于把一段可执行的代码通过参数传递给其他代码而这段代码会在某一个时刻被调用这就叫回调。voidcallbackFunction(intresult){std::coutResult: resultstd::endl;}// 定义接收回调函数的函数voidadd(inta,intb,void(*callback)(int)){intresultab;//调用回调函数callback(result);}intmain(){//传递回调函数add(10,20,callbackFunction);return0;}异常处理异常处理原理编译器在 try 块里生成“异常表”记录异常和对应的 catch。当 throw 发生时程序会沿调用栈回溯stack unwinding找到匹配的 catch。在回溯过程中所有局部对象会自动调用析构函数。因此正常情况下try-catch 几乎没有性能损耗零开销模型。一旦抛出异常就会有栈回溯和对象析构的开销。try{inta10;intb0;if(b0){throw std::runtime_error(Division by zero is not allowed.);}std::coutResult: a/bstd::endl;}catch(conststd::exceptione){std::cerrError: ;std::cerre.what()\n;}std::exception是所有标准异常类的基类定义了异常的基本接口。它有一个虚函数 what()用于返回异常信息的 C 风格字符串。std::runtime_error: 表示运行时错误通常是由于程序逻辑问题导致的异常例如无效的参数、无法打开文件等。throw std::runtime_error(“Runtime error occurred”);std::logic_error: 表示逻辑错误通常是由于程序逻辑错误导致的异常例如逻辑断言失败、索引越界等。throw std::logic_error(“Logic error occurred”);std::invalid_argument: 表示传递给函数的参数无效。throw std::invalid_argument(“Invalid argument”);std::out_of_range: 表示访问超出有效范围的对象如数组、容器等。throw std::out_of_range(“Out of range”);std::overflow_error 和 std::underflow_error: 表示数值计算时出现溢出或下溢。throw std::overflow_error(“Overflow occurred”);throw std::underflow_error(“Underflow occurred”);std::bad_alloc: 表示内存分配失败。throw std::bad_alloc(); // 内存分配失败时抛出函数模板与类模板泛型编程函数模板templatetypename T,typename Uautoadd(T a,U b)-decltype(ab){//C11的风格使用了尾置返回类型C14之后可以直接使用auto来推断返回类型returnab;}intmain(){std::coutadd(10,20)std::endl;std::coutadd(10.5,20.6)std::endl;std::coutadd(10.5,50)std::endl;return0;}类模板templatetypename Tclass CusData{private:T data;public:voidsetData(T d){datad;}TgetData()const{returndata;}CusData(){}~CusData(){}};intmain(){CusDataintintData;intData.setData(42);std::coutInteger data: intData.getData()std::endl;return0;}文件IO#include 输出文件流用于创建文件或者往文件中写入文件。#include 输入文件流用于打开文件读取文件信息。#include 保留上面两种的功能模式标志ios::app 追加模式。所有写入都追加到文件末尾。ios::ate 文件打开后定位到文件末尾。ios::in 打开文件用于读取。ios::out 打开文件用于写入。ios::trunc 如果该文件已经存在其内容将在打开文件之前被截断即把文件长度设为 0。ios::binary 以二进制模式打开//以文本模式写入文件std::string filenameexample.txt;std::ofstreamoutputFile(filename,std::ios::out|std::ios::trunc);if(!outputFile){std::cerrError opening file for writing: filenamestd::endl;return1;}outputFileHello, this is a test file.std::endl;outputFileThis file is created using C.std::endl;outputFile.close();//读文本文件std::ifstreaminFile(filename,std::ios::in);if(!inFile){std::cerrError opening file for reading: filenamestd::endl;return1;}std::string line;while(std::getline(inFile,line)){std::coutlinestd::endl;}inFile.close();//以二进制方式写入和读取数据std::string filenameexample.txt;std::ofstreamoutputFile(filename,std::ios::out|std::ios::trunc|std::ios::binary);if(!outputFile){std::cerrError opening file for writing: filenamestd::endl;return1;}inta10;intb20;intc30;doubled10.5;outputFile.write(reinterpret_castconstchar*(a),sizeof(a));outputFile.write(reinterpret_castconstchar*(b),sizeof(b));outputFile.write(reinterpret_castconstchar*(c),sizeof(c));outputFile.write(reinterpret_castconstchar*(d),sizeof(d));outputFile.close();std::ifstreaminFile(filename,std::ios::in|std::ios::binary);if(!inFile){std::cerrError opening file for reading: filenamestd::endl;return1;}inta1,b1,c1;doubled1;inFile.read(reinterpret_castchar*(a1),sizeof(a1));inFile.read(reinterpret_castchar*(b1),sizeof(b1));inFile.read(reinterpret_castchar*(c1),sizeof(c1));inFile.read(reinterpret_castchar*(d1),sizeof(d1));std::coutRead values: a1, b1, c1, d1std::endl;inFile.close();强制类型转换const_cast常量转换主要用于添加或者移除const或者volatile修饰符。inta10;constint*ptrAa;//指向常量的指针// *ptrA 20; //错误不能修改指向的值int*ptrBconst_castint*(ptrA);//去掉const属性允许修改指向的值*ptrB20;//正确可以修改指向的值int*ptrBa;constint*bconst_castconstint*(ptrB);//添加const属性禁止修改指向的值// *b 30; //错误不能修改b的值因为它是一个const指针static_cast静态类型转化基本数据类型转化有继承关系的父类与子类之间的指针或引用转换。class Person{public:virtualvoiddisplay(){std::coutI am a person.std::endl;}};class Student:public Person{public:virtualvoiddisplay(){std::coutI am a student.std::endl;}};intmain(){//基本类型转换intnum97;charcstatic_castchar(num);std::coutThe character representation of num is: cstd::endl;//指针类型转换int*ptrnum;void*vPtrstatic_castvoid*(ptr);std::coutThe void pointer is: vPtrstd::endl;std::coutThe value of num is: *static_castint*(vPtr)std::endl;//子类转父类上行转换是安全的因为子类对象包含父类对象的所有成员。Student student;Person*personPtrstatic_castPerson*(student);//上行转换安全personPtr-display();Person person;//父类转子类下行转换是不安全的因为父类对象可能不包含子类对象的成员。//Student* studentPtr static_castStudent*(person); //下行转换不安全//只有父类确定是指向子类对象的指针时下行转换才是安全的。Person*personPtr2newStudent();Student*studentPtr2static_castStudent*(personPtr2);//下行转换studentPtr2-display();return0;}reinterpert_castreinterpert_cast是c中最危险、最底层的转化操作。不会进行任何类型检查只是告诉编译器将这个类型的内存按照二进制方式解释成另一种类型。intx65;char*preinterpret_castchar*(x);// 把 int 地址当作 char* 使用// 不改变内存内容只是编译器认为 p 指向的是 charstd::coutThe character representation of x is: *pstd::endl;// 输出 A因为 ASCII 码 65 对应 Adynamic_cast动态类型转换会在运行时进行类型检查。主要用于多态类型之间的转化。如果转化失败对于指针返回nullptr。对于则抛出异常std::bad_cast但由于是运行时类型检查是会有一定的性能开销。必须满足以下条件转化的类型必须要有一个虚函数也就是基类必须有虚函数。以支持运行时类型检查RTTI编译器必须启用RTTI大部分的c编译器默认已经启用。Person*personPtr3newStudent();//dynamic_cast用于运行时类型检查确保转换的安全性。Student*studentPtrdynamic_castStudent*(personPtr3);//下行转换安全if(studentPtr){studentPtr-display();}Person penson;Student*studentPtr2dynamic_castStudent*(penson);//转换失败因为penson是一个Person对象不是Student对象。 返回nullptrif(studentPtr2){studentPtr2-display();}else{std::coutConversion failed: personPtr3 is not pointing to a Student object.std::endl;}
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2503776.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!