分类 CPP articles

进程的创建

进程的创建 进程的启动过程 打开目标exe的映象文件 创建进程内核对象 映射NTDLL到内存 创建线程内核对象 通知子系统对进程进行管理 启动线程初始化空间和加载其他DLL 跳转到入口处开始执行 CreateProcess api BOOL CreateProcess( LPCTSTR lpApplicationName, // name of executable module LPTSTR lpCommandLine, // command line string LPSECURITY_ATTRIBUTES lpProcessAttributes, // SD LPSECURITY_ATTRIBUTES lpThreadAttributes, // SD BOOL bInheritHandles, // handle inheritance option DWORD dwCreationFlags, // creation flags LPVOID lpEnvironment, // new environment block LPCTSTR lpCurrentDirectory, // current directory name LPSTARTUPINFO lpStartupInfo, //……

Continue reading

进程和线程

进程和线程 什么是进程? 进程和普通计算机程序的区别 进程 是运行中的程序 进程 活在内存中 有血有肉有灵魂 程序 死在 硬盘上的 有肉没血没灵魂 如果进程是空间的概念,那么这段空间里有什么? 如果进程是空间概念,那么进程是怎么工作的? 进程和线程的关系 进程: 提供了必要的资源和空间 线程: 使用进程中的资源完成……

Continue reading

Cpp9 模板

模板 下面是一个针对int的冒泡排序 // _20180301.cpp : Defines the entry point for the console application. // #include "stdafx.h" void Sort(int* arr,int nLength) { int i,k; for (i = 0;ix > base.x && this->y > base.y; } private: int x; int y; }; template void Sort(T* arr,int nLength) { int i,k; for (i = 0;iy) return x; else return y; } M min() {……

Continue reading

Cpp8 运算符重载和深浅拷贝

深浅拷贝 相同类型间可以直接拷贝 // _20180212.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include #include class A { private: int* a; public: A() { a = new int[10]; } virtual ~A() { delete a; printf("析构 A \n"); } }; int main(int argc,char* argv[]) { A a1; A a2; a1 = a2; return 0; } //反汇编 27: A a1; 0040108D lea ecx,[ebp-14h] 00401090 call @ILT+15(B::B) (00401014) 00401095 mov dword ptr [ebp-4],0 28: A a2; 0040109C lea ecx,[ebp-1Ch] 0040109F call @ILT+15(B::B) (00401014) 004010A4 mov byte ptr [ebp-4],1 29: a1 = a2; 004010A8 lea eax,[ebp-1Ch] 004010AB push eax //a2 作为参数传递 004010AC lea ecx,[ebp-14h] //a1……

Continue reading

Cpp7 C++的多态实现 — 虚表

多态的实现原理 #include "stdafx.h" #include #include class A { public: int x; virtual void Test() { printf("A \n"); } protected: private: }; class B:public A { public: int x; void Test() { printf("B \n"); } protected: private: }; void Fun(A* p) { p->Test(); } int main(int argc, char* argv[]) { A a; B b; Fun(&b); return 0; } //我们发现在这里 调用的test函数 是b的 因为fun方法传入的对象是b b继承自a 这里体现了多态 //反编译 31: void Fun(A* p) 32: { 00401050 push ebp 00401051 mov ebp,esp 00401053 sub esp,40h 00401056 push ebx 00401057 push esi 00401058 push edi 00401059……

Continue reading

Cpp6 封装、继承和多态

继承 子类从父类继承成员变量 子类从父类继承成员函数 #include "stdafx.h" class Person { public: int Age; int Sex; void Word() { printf("Person:Work"); } }; class Teacher:public Person { public: int Level; }; int main() { Teacher t; t.Age = -1; //合法但是不合理 t.Sex = 2; t.Level = 3; return 0; } 实现数据隐藏 为什么要隐藏数据成员 与前面比较,赋值的时候 合理不合法,手机的电路板也没有暴露在外面啊 根本的目的是可控 不要造相同的轮子 代码……

Continue reading

Cpp5 在堆中创建对象和引用类型

我们可以在什么地方创建对象? 全局变量区 Person p; 栈 void Max() { Person p; } 堆 new 和 delete //在堆中创建对象: Person* p = new Person(); //释放对象占用的内存 delete p; 在堆中创建对象: new``delete 在C语言中我们使用malloc申请堆空间 使用完毕后使用free释放空间 C++: class Person { private: int x; int y; public: Person() { printf("Person()……

Continue reading

Cpp4 类成员的访问控制

好的编程习惯 -定义和实现分开 代码会有更好的可读性 但不是必须的 在头文件中只留下声明代码 Test.h struct sclass { int x; int y; int Bigger(int x,int y); int Max(int x,int y,int z); }; Test.cpp int sclass::Bigger(int x,int y) { if(x>y) { return x; } else { return y; } } int sclass::Max(int x,int y,int z) { return Bigger(Bigger(x,y),z); } 使用: main.cpp #include "Test.h" #include #include int main(int argc,char* arhv[]) { sclass s; return 0; } 上面的实现方式没有问题,但是其实我们的Bigger函数是为了实现MAX函……

Continue reading

Cpp3 继承

什么是继承 struct Person { int age; int sex; }; struct Teacher { int age; int sex; int level; int classId; }; struct Teacher:Person { int level; int classId; }; 总结: 1、什么是继承? 继承就是数据的复制 2、为什么要用继承? 减少重复代码的编写 3、Person 称为父类或者基类 4、Teacher称为子类或者派生类 继承不仅仅局限于父类 struct X { int a; int b; }; struct Y:X { int c; int d; }; struct Z:Y //Z的成员……

Continue reading

Cpp2 构造函数和析构函数

什么是构造函数 #include "stdafx.h" #include struct Sclass { int a; int b; int c; int d; Sclass()//构造函数 { printf("观察这个函数 \n"); } Sclass(int a,int b,int c,int d)//构造函数 { this->a=a; this->b=b; this->c=c; this->d=d; printf("观察这个函数 2\n"); } int Plus() { return a+b+c+d; } }; int main(int argc, char* argv[]) { Sclass s; Sclass s2(1,2,3,4); return 0; } //反汇编: Sclass s; 0040D408 lea ecx,[ebp-10h] 0040D40B call @ILT+5(Sclass::Sclass) (0040100a) Sclass s2(1,2,3,4); 0040D770 push 4 0040D772 push 3 0040D774 push……

Continue reading