当前目录...">

C/C++(1)

作者: BlueFishMan | 来源:发表于2020-06-21 11:54 被阅读0次

#include预处理

  • #include<标准头文件>->标准头目录
  • #include"自定义头文件"->当前目录->标准头目录
  • C .h

using

  • namespace 命名空间 区分不同库中同名的变量、函数、类等
//using编译指令
using namespace std;

//using声明
using std::cin;
using std::cout;
using std::endl;

std::cin;
std::cout;
std::endl;

sizeof运算符

返回类型和静态分配的对象、结构或数组所占的空间

int a[5] = {0};//初始化0
int b[] = {1,5,3,8};
int n = sizeof b / sizeof(int);//4

char c[] = "abc";
char *str = c;
const char *str = "abc";
sizeof str;//8

//函数的返回类型所占的空间大小,且函数的返回类型不能是void
double func(){
    return 0;
}
sizeof func();//8

结构体/共用体/枚举

struct Student {
    int name;//默认public
};
struct Student me;

typedef struct Student {
    int name;
} S;
struct Student {
    int name;
};
typedef struct Student S;
S me;

void Student() {};//不冲突
void S() {};//冲突
struct Student {
    int name;
};
Student me;

typedef struct Student {
    int name;
} S;
struct Student {
    int name;
};
typedef struct Student S;
S me;

void Student() {};//冲突
Student();
struct Student me;
void S() {}//冲突

内存对齐

struct s {
    int a[5];
    char b;
    double c;
};

union u {
    int a[5];
    char b;
    double c;
};

sizeof(s);//32
sizeof(u);//24

分配内存

典型内存空间布局
5种变量存储方式

自动存储

栈(stack,堆栈)

  • ulimit -s -> stack size = 8MB -> overflow

静态存储

数据段

  • 初始化0
  • 外部链接性 extern

动态存储

堆(heap)

  • malloc-free (库函数)
  • new/new[]-delete/delete[] (运算符)

调试

  • top 任务管理器
  • ps(preocess status) 快照

Valgrind 内存分析工具

  • Memcheck
  • Valid-Value表->bit向量->有效/已初始化
  • Valid-Address表->1 bit->读写
  • 内存泄漏(编程习惯/单元测试)

内联函数

inline

  • 宏定义 #define 文本替换

条件编译

#define _DEBUG_
#ifdef _DEBUG_
#else
#endif

引用

  • 传值
  • 传指针 *
  • 传引用 &

常引用

int a = 0;
const int &b = a;
a = 1;//yes
b = 1;//no

string funcl();
void func2(string &s);
void func3(const string &s);

func2("hello");//no
func2(funcl);//no
func3("hello");//yes
func3(funcl);//yes
//func1()和”hello”都将产生一个临时对象,而在C++中,这些临时对象都是const类型的.因此,上面的表达式就是试图将一个const类型的对象转化为非const类型,这是非法的

相关文章

网友评论

      本文标题:C/C++(1)

      本文链接:https://www.haomeiwen.com/subject/yiwoxktx.html