C++记录1
C的头文件为math.h
C++的为 cmath
using编译指令 namespace 区分不同产品的函数。Mics::cout Linux::cout
cout << 即是位运算 也是流处理
存储位置,内存空间,存储单位为bit,每一位两个存储位,因此8位int存储2^7。
#define 预编译常量 const建议使用
结构存储不同类型, 指针将存储位置变量告诉计算机
结构体:
struct inflatable
{
char name[20];
std::string name;
float volume;
}
inflatable guests[2]=
{
{“Bambi“, 0.5},
{“Go", 0.9}
}
寄存器:
struct torgle_register
{
unsigned int SN:4; 四个bit
}
共用体 union: 可以存多个类型的变量,但一次只能存一个变量,其他变量丢失,用于节省内存。
C++记录2
指针:
指针是一个变量,存储值的地址,而非值本身。
&获得地址 0x40 0x44 相差4位 为int值,使用new 请求新的内存位置。
* &home = int; *用于解除引用
int home = 5
int * pt =&home
pt = &home pt为指向int的指针
指针被初始化时,初始化的是指针的地址,而没有为改指针的值分配地址,因此在创建指针时,指定值。 int *p=5
int *p = new int
*p =5
值被存在栈, new被存在堆。
delete p 释放指针指向的内存,而非指针本身。
数组等价于指针,
double *p3 = new double [10]
p3[0]=0.1
p3[1]=0.5
p3=p3+1 指针向后移动一位
cout << p3[0] 输出0.5
p3=p3-1 复位指针才能释放
delete [] p3
int p4[10] ={1,2}
int *p5 = p4 值为0
int *p6 = &p4[9] 值为9
C++记录3
字符串指针:
char f[10] = 'rose'
cout f
f为数组地址初始指针,输出时依次移动指针。""也被看做是内存地址。
char animal[20] = "bear"
const char *bird = "wear" 字符串默认地址
char *ps
cout animal ,bird 等效输入指针地址,依次读取指针内存。
ps = new char[strlen(animal)+1]
strcpy(ps,animal) 将字符串从一个位置复制到另一个位置。
cout (int *) animal (int *)ps 输出内存地址,而不是顺着指针出字符。
指针与结构体(与类相似):
struct inflatable
{
char name[20],
float age
}
inflatable *ps = new inflatable
ps->name=""
(*ps).age=10.0
创建指针复用池,通过临时指针记录存储位置,释放并重用内存:
char * getname()
{
char temp[20] = ""
char *pn = new char[strlen(temp)+1]
strcpy(pn, temp)
reture pn
方法结束时,声明创建的内存temp,自动释放。
}
char *name
name = getname()
delete [] name
name = getname()
delete [] name