[008]C/C++系列
文章平均质量分 91
C/C++系列
Zero Ice
编辑&代码艺术
展开
-
[008]C/C++系列 | 最小的程序 不一样的HelloWorld!!!
一、需求 日常的HelloWorld打印,需要用到C语言库里面的函数(printf),我们希望“小程序”脱离C语言库,使它成为一个独立于任何库的纯正“程序” 日常使用的程序,都是以main作为入口,现在我们自定义一个入口(nomain) 日常的HelloWorld会产生很多段,现在我们需要将所有的段合并到"tinytext"段 二、实现2.1GCC内嵌汇编int write(int filedesc, char* buffer, int size);函数实现..原创 2020-09-16 15:05:07 · 338 阅读 · 0 评论 -
[008]C/C++系列 | 软中断算法实现
//此图来源于网络/* * trab.cpp * Copyright (C) 2020 root <root@tw.com> * * Distributed under terms of the MIT license. */#include <iostream>#include <stdio.h>#include <csignal>#include <unistd.h>#include <sys/t...原创 2020-09-14 19:20:46 · 559 阅读 · 0 评论 -
[008]C/C++系列 | 进程创建 && 子进程处理
/* * thread.cpp * Copyright (C) 2020 root <root@tw.com> * * Distributed under terms of the MIT license. */#include <iostream>#include<unistd.h>using namespace std;int main(){ pid_t pid; cout<<"父进程"<<endl;.原创 2020-09-14 18:03:09 · 200 阅读 · 0 评论 -
[008]Python系列 | // 和 / & : ::区别
“ / ” 为浮点数除法,返回浮点结果“ // ” 表示整数除法,返回不大于结果的一个最大整数原创 2020-08-30 14:05:56 · 958 阅读 · 0 评论 -
[008]C/C++系列 | g++ 一些选项说明
-g- turn on debugging (so GDB gives more friendly output) -Wall- turns on most warnings -Oor-O2- turn on optimizations -o<name>- name of the output file -c- output an object file (.o) -I<include path>- specify an include directory...原创 2020-08-25 12:45:14 · 202 阅读 · 0 评论 -
[008]C/C++系列 | C++ 11区间迭代
一、数组int array[] = {1,2,3,4,5};for(auto &x : array) { std::cout << x << std::endl;}二、容器std::vector<int> arr(5, 100);for(std::vector<int>::iterator i = arr.begin(); i != arr.end(); ++i) { std::cout << *i .原创 2020-08-25 12:24:41 · 277 阅读 · 1 评论 -
[008]C/C++系列 | C++11 auto & decltype
一、背景C++ 11引入了auto &decltype实现类型推导,在某种意义上提供了无需操心变量类型的习惯(类似弱类型语言)。二、auto使用auto进行类型推导的一个最为常见而且显著的例子就是迭代器。例如:for(vector<int>::const_iterator itr = vec.cbegin(); itr != vec.cend(); ++itr)替换为auto:for(auto itr = vec.cbegin(); itr !=...原创 2020-08-25 12:20:06 · 148 阅读 · 0 评论 -
[008]C/C++系列 | C/C++混合调用(二) 函数指针
当我们C和C++混合编程时,有时候会用一种语言定义函数指针,而在应用中将函数指针指向另一中语言定义的函数。如果C和C++共享同一中编译和连接、函数调用机制,这样做是可以的。然而,这样的通用机制,通常不然假定它存在,因此我们必须小心地确保函数以期望的方式调用。而且当指定一个函数指针的编译和连接方式时,函数的所有类型,包括函数名、函数引入的变量也按照指定的方式编译和连接。如下例:typedef int (*FT) (const void* ,const void*);//style of ...原创 2020-08-25 00:31:54 · 173 阅读 · 0 评论 -
[008]C/C++系列 | C/C++混合调用(一) extern
一、引言在用C++的项目源码中,经常会不可避免的会看到下面的代码:#ifdef __cplusplusextern "C" {#endif /*...*/ #ifdef __cplusplus}#endif它到底有什么用呢,你知道吗?而且这样的问题经常会出现在面试or笔试中。下面我就从以下几个方面来介绍它:1、#ifdef _cplusplus/#endif _cplusplus及发散 2、extern "C" 2.1、extern关键字 2.2、"C" 2.原创 2020-08-25 00:18:01 · 170 阅读 · 0 评论 -
[008]C/C++系列 | C++11弃用的特性
注意:弃用不等于废用!!!弃用表示即将废弃,避免使用!!!弃用的特性:char *str = "hello world!"; // 将出现弃用警告 如果一个类有析构函数,为其生成拷贝构造函数和拷贝赋值运算符的特性被弃用了。 不再允许字符串字面值常量赋值给一个char *。如果需要用字符串字面值常量赋值和初始化一个char *,应该使用const char *或者auto。 C++98 异常说明、unexpected_handler、s...原创 2020-08-24 23:31:15 · 592 阅读 · 0 评论 -
[008]C/C++系列 | istringstream和ostringstream区别
一、头部<sstream>二、区别istringstream:string转数字 => 字符串输入流 ostringstream:数字转string => 字符串输出流三、实例3.1string转数字istringstream ss("123");int tmp;ss>>tmp;3.2数字转sringostringsteam ss;ss<<123;string tmp = ss.str();...原创 2020-08-18 21:40:52 · 168 阅读 · 0 评论 -
[008]C/C++系列 | NULL和nullptr区别
一、C程序中的NULL在C语言中,NULL通常被定义为:#define NULL ((void *)0)所以说NULL实际上是一个空指针,如果在C语言中写入以下代码,编译是没有问题的,因为在C语言中把空指针赋给int和char指针的时候,发生了隐式类型转换,把void指针转换成了相应类型的指针。int *pi = NULL;char *pc = NULL;二、C++程序中的NULL但是问题来了,以上代码如果使用C++编译器来编译则是会出错的,因为C++是强类型语言,void*是不能.转载 2020-08-12 00:47:49 · 137 阅读 · 0 评论 -
[008]C/C++系列 | Pointer-Like classes(二)迭代器
备注:本篇文章仅仅是本人记录而已,假如需要了解其他关于迭代器知识,请自行百度!!!template<class T, class Ref, class Ptr>struct __list_interator { typedef __list_interator<T, Ref, Ptr> self; typedef Ptr pointer; typedef Ref reference; typedef __list_node<T&..原创 2020-07-09 11:40:48 · 140 阅读 · 0 评论 -
[008]C/C++系列 | Pointer-Link classes(二)智能指针
备注:本篇文章仅仅是本人记录而已,假如需要了解其他关于智能指针知识,请自行百度!!!template<class T>class shared_ptr{public: T& operator*() const { return *pr; } T* operator->() const { return px; } shared_ptr(T* p) : px(p) { }private: T* p..原创 2020-07-09 11:26:58 · 109 阅读 · 0 评论 -
[008]C/C++系列 | 转换函数(二)conversion function VS non-explicit-one-argument ctor
备注:one-argument:Fraction(int num, int den=1)一个参数class Fraction{public: Fraction(int num, int den=1) : m_numerator(num), m_denominator(den) {} Fraction operator+(const Fraction& f) { return Fraction(...); }..原创 2020-07-02 15:47:02 · 149 阅读 · 0 评论 -
[008]C/C++系列 | 转换函数(一)conversion function
class Fraction{public: Fraction(int num, int den=1) : m_numerator(num), m_denominator(den) {} operator double() const { return (double)( m_numerator / m_denominator ); }private: int m_numerator; // 分子 int m_denominat.原创 2020-07-02 15:17:59 · 150 阅读 · 0 评论 -
[008]C/C++系列 | 继承&虚函数
一、定义non-virtual函数:你不希望derivedclass重新定义(override,覆盖)它 virtual函数:你希望derivedclass重新定义(override,覆盖)它,且已有默认定义 pure virtual函数:你希望derivedclass重新定义(override,覆盖)它,对它没有默认定义二、例子1class Shape {public: virtual void draw() const = 0; // pure virtual ...原创 2020-06-30 12:11:37 · 116 阅读 · 0 评论 -
[008]C/C++系列 | Class之间关系(三)-Inheritance(继承)
一、底层构造struct _List_node_base{ _List_node_base* _M_next; _List_node_base* _M_prev;};template<typename _Tp>struct _List_node : public _List_node_base{ _Tp _M_data;};二、析构&构造注意:baseclass的构造函数必须是virtual,否则出现und...原创 2020-06-29 15:13:05 · 143 阅读 · 0 评论 -
[008]C/C++系列 | Class之间关系(二)-Delegation(委托)
// file String.hppclass StringRep;class String {public: String(); String(const char* s); String(const String& s); String &operator=(const String& s); ~String(); ...private: StringRep* rep; // pimpl}//..原创 2020-06-29 14:58:16 · 176 阅读 · 0 评论 -
[008]C/C++系列 | Class之间关系(一)-Composition(复合)
一、简述某某class声明里面存在某class;例如下面:queue里面含dequeAdaptertemplate <class T, class Sequence = deque<T> >class queue { ...protected: Sequence c; // 底层容器public: // 以下完全利用C操作函数完成 bool empty() const { return c.empty(); } ...原创 2020-06-29 14:02:20 · 423 阅读 · 0 评论 -
[008]C/C++系列 | namespace几种用法
一、usingdirective#include <iostream.h>using namespace std;int main(){ cin>>...; cout<<...; return 0;}二、using declaration#include <iostream.h>using std::cout;int main(){ std::cin<<...; cout.原创 2020-06-29 10:55:35 · 127 阅读 · 0 评论 -
[008]C/C++ | cout 源码
一、源码class _IO_ostream_withassign : public ostream { ...};extern __IO_ostream_withassign cout;cout主要是继承ostream!!!二、ostream源码class ostream : virtual ios{ public: ostream& operator<<(char c); ostream&原创 2020-06-29 10:43:06 · 980 阅读 · 0 评论 -
[008]C/C++系列 | Static
class complex{public: double real() const { return this->re; }private: double re, im;};一、non-static & static区别1.1non-staticcomplex c1,c2,c3;cout<<c1.real();cout<<c2.real();会被编译器自动转化为:complex c1,..原创 2020-06-29 10:32:53 · 112 阅读 · 0 评论 -
[008]C/C++系列 | Array New & Array Delete
一、背景ArrayNew一定要搭配ArrayDelete!!!二、原因String* pc = new String[3]; // 调用三次构造...delete[] pc; // 调用三次析构String* pc = new String[3];...delete pc; // 调用一次析构注意:上面两段代码区别:delete[]和 delete备注:第二段代码会造成内存泄漏!!!...原创 2020-06-22 12:42:54 · 136 阅读 · 0 评论 -
[008]C/C++系列 | new & delete 内部实现
一、背景Complex* pc = new Complex(1, 2);String* ps = new String("hello");...delete ps;上面是一个普遍的new和delete 语句,到底new是如何实现的呢?二、原理2.1NewComplex* pc;void* mem = operator new( sizeof(Complex) ); // 分配内存pc = static_cast<Complex*>(mem...原创 2020-06-22 12:29:44 · 287 阅读 · 0 评论 -
[008]C/C++系列 | Stack Object & Static Local Object & Global Object & Heap Object 生命周期
一、Stack Objectclass Complex { ... };...{ Complex c1(1,2);}备注:c1便是所谓的stackobject,其生命在作用域(scope)结束之后结束。这种作用域内的object,又称为autoobject,因为会被自动清理二、Static Local Objectclass Complex { ... };...{ static Complex c2(1,2);}备注:c2便是所谓...原创 2020-06-22 12:10:13 · 222 阅读 · 0 评论 -
[008]C/C++系列 | stack & heap
一、背景Stack:存在某一个作用域(scope)的一个内存空间(Memoryspace)。例如:当你调用函数,函数本身即会形成一个stack用来放置它所接收的参数,以及返回地址Heap:指由操作系统提供的一个global内存空间,程序可能会动态分配(dynamic allocated)二、例子class Complex { ... };...{ Complex c1(1,2); // Stack Complex* p = new Complex(3.原创 2020-06-22 11:25:10 · 134 阅读 · 0 评论 -
[008]C/C++系列 | String总结
一、背景在此之前请先阅读:《[008]C/C++系列 | String三个特殊函数(一)析构函数》 《[008]C/C++系列 | String三个特殊函数(二)拷贝构造》 《[008]C/C++系列 | String三个特殊函数(三)拷贝赋值》二、打印#include <iostream.h>ostream& operator<<(ostream& os, const String& str){ os<<st原创 2020-06-22 11:11:46 · 90 阅读 · 0 评论 -
[008]C/C++系列 | String三个特殊函数(三)拷贝赋值
备注:直接使用编译器提供的String会存在这种情况(浅拷贝):这种情况是我们需要避免的!!!class String{public: String(const char* cstr = 0); String(const String& str); // 拷贝构造 String& operator=(const String& str); // 拷贝赋值 ~String(); char* get_c_str() ..原创 2020-06-21 09:58:12 · 149 阅读 · 0 评论 -
[008]C/C++系列 | String三个特殊函数(二)拷贝构造
class String{public: String(const char* cstr = 0); String(const String& str); // 拷贝构造 String& operator=(const String& str); // 拷贝赋值 ~String(); char* get_c_str() const { return m_data; } private: char* m_data;};.原创 2020-06-21 09:15:27 · 122 阅读 · 0 评论 -
[008]C/C++系列 | String三个特殊函数(一)析构函数
class String{public: String(const char* cstr = 0); String(const String& str); // 拷贝构造 String& operator=(const String& str); // 拷贝赋值 ~String(); char* get_c_str() const { return m_data; }private: char* m_data;};i.原创 2020-06-21 09:07:52 · 168 阅读 · 0 评论 -
[008]C/C++系列 | 总结
在看此之前请先阅读:《[008]C/C++系列 | 构造函数initialization list 高级用法》 《[008]C/C++系列 | overloading(重载)》 《[008]C/C++系列 | constructor可以放在private中?》 《[008]C/C++系列 | const member function(常量成员函数)》 《[008]C/C++系列 | pass by value vs. pass by reference(to const)》 《[00...原创 2020-06-21 08:45:54 · 137 阅读 · 0 评论 -
[008]C/C++系列 | operator
#include <iostream.h>ostream&operator << (ostream& os, const complex& x){ return os << '(' << real(x) << ',' << imag(x) << ')';}complex c1(9,8);cout<<c1;cout<<<c1<endl;.原创 2020-06-21 08:28:30 · 112 阅读 · 0 评论 -
[008]C/C++系列 | friend友元
class complex{public: complex (double r = 0, double i = 0) : re (r), im (i) // 初始化高级用法 { } complex& operator += (const complex&); double real () const { return re; } double imag () const { return im; }private: d.原创 2020-06-20 08:06:48 · 133 阅读 · 0 评论 -
[008]C/C++系列 | pass by value vs. pass by reference(to const)
class complex{public: complex (double r = 0, double i = 0) : re (r), im (i) // 初始化高级用法 { } complex& operator += (const complex&); double real () const { return re; } double imag () const { return im; }private: d.原创 2020-06-19 21:36:45 · 125 阅读 · 0 评论 -
[008]C/C++系列 | const member function(常量成员函数)
class complex{public: complex (double r = 0, double i = 0) : re (r), im (i) // 初始化高级用法 { } complex& operator += (const complex&); double real () const { return re; } double imag () const { return im; }private: d.原创 2020-06-19 21:24:56 · 191 阅读 · 0 评论 -
[008]C/C++系列 | constructor可以放在private中?
class complex{public: complex& operator += (const complex&); double real () const { return re; } double imag () const { return im; }private: double re, im; complex (double r = 0, double i = 0) : re (r), im (i) // 初始.原创 2020-06-19 21:13:55 · 253 阅读 · 0 评论 -
[008]C/C++系列 | overloading(重载)
重载注意事项class complex{public: complex (double r = 0, double i = 0) : re (r), im (i) // 初始化高级用法 { } complex& operator += (const complex&); double real () const { return re; } double imag () const { return im; }priva.原创 2020-06-19 21:06:57 · 118 阅读 · 0 评论 -
[008]C/C++系列 | 构造函数initialization list 高级用法
高级用法:initialization listclass complex{public: complex (double r = 0, double i = 0) : re (r), im (i) // 初始化高级用法 { } complex& operator += (const complex&); double real () const { return re; } double imag () const { .原创 2020-06-19 20:54:26 · 171 阅读 · 0 评论 -
[008]C/C++系列 | 推荐几个学习Make文档
文档 链接 What is a Makefile and how does it work? https://opensource.com/article/18/8/what-how-makefile GNUmake https://www.gnu.org/software/make/manual/make.html#Overview makefile介绍 https://seisman.github.io/how-to-.原创 2020-06-19 15:22:50 · 338 阅读 · 0 评论