storage area: code:function //CS指令指针
const: //can't written
static:global and static variable //属于函数,一次初始化
heap: //首地址记录大小;申请了没释放,释放了没归0
stack: //SS栈顶指针
stack:低地址扩展,空间小,速度快,系统控制
heap:高地址扩展,空间大,速度慢,程序员控制;通常由空闲链表管理(first fit, best fit, largest fit)
进栈顺序: 返回地址:调用函数下一条指令 //缺页中断
被调函数参数:从右至左
被调函数局部变量
variable scope:existence+visibility
local:函数内定义, block,里层mask外层,无法使用被mask的局部变量
auto:default
register:
static:属于函数,一次初始化;默认初始化0 //内部申明,外部初始化
global:函数外定义, 定义点到本文件结束,可用申明扩展 //first global then main
static:只能在本文件中使用,可用extern申明往上扩展 //declare:extern type name==extern name;
extern:default;只申明不定义
function:static:只能在本文件中调用,可用extern申明往上扩展 //declare:extern function_prototype=function_prototype
extern:default;可申明可定义
0: int i=0 //整型0
float i=0 //浮点型0.0
char c=0 //结束字符/0
int *p=0 //等价于NULL
NULL:#define NULL 0 //stddef.h
EOF:#define EOF -1 //stdio.h; eof=^d //stty
Preprocess
固有预定义宏:__LINE__:正在编译源文件的行号
__FILE__:正在编译源文件的文件名
__DATE__:"Mmm dd yyyy"
__TIME__:"hh:mm:ss"
__STDC__:1, 表示编译程序的实现符合STDC
include:#include <……> //库目录查找
#include "……" //工作目录查找
macrosubstitution:#define identifier token-sequence //简单替换;完全相同可重复
#define mkstr(s) #s
#define TEXT(quote) L##quote
#undef identifier //终止宏定义
conditional compilation:if,elif,else,endif
defined,!defined
/: 行连接
pragma: 较复杂,常见的如下
#Pragma message("消息文本")
#pragma once:编译一次;至少一次?只有一次?
#error token-sequence //预编译
static_assert(expression, string) //编译
assert(expression) //运行
Assertion failed: expression, file filename, line nnn //__FILE__,__LINE__
#define NDEBUG
#include <assert.h>
typedef:类型定义
datatype:点型: integer
char,wchar_t
double
enumerate
维型: str
function
array
pointer
构造类型:structure
union
3D:三维数组名
2D:二维数组名,指针函数名,指针数组名
1D:指针,一维数组名,函数名,函数指针
0D:常量,相对地址
operator:[]相对地址降维,()函数降维;&取址增维,*指向降维
type cast:(type)expression //创建临时变量,原来变量不动,不安全需保证内部实现相同
( (int []){1,2,3,4} )[1]==2 //挺变态,编译未通过
int:fixed-point number,two's complement
variable:short 2B,int 4B,long 4B
constant:oct 0,hex 0x //wrap overflow
char:ASCII:数字大小写分开差32 //uppercase, lowercase, case-sensitive
variable:
constant:0x0A=='/x0A'=='/n'
wchar_t:variable:typedef unsigned short wchar_t //WCHAR.H; char,wchar,tchar
constant:L"" //#define TEXT(quote) L##quote
均有相应的处理函数,例如cout的wchar_t版本为wcout
double:floating point number,IEEE754
variable:float 4B,double 8B,long double 10B
significant digit:float 6~7,double 15~16, //setprecision(n)<iomanip>
constant:decimal,exponent
enum:typedef:enum day{sun,mon,tue,wed,thu,fri,sat} //按定义顺序自动赋值,从0开始;也可指定
variabledef:day weekday;
assignment:weekday=mon或weekday=(day)1 //(day)1==将整型1强制类型转换为枚举整数1
也可当作常量
str:variable:字符数组名 //char a[]="auxiliary";点赋值,栈上存储,sizeof a==10
字符指针 //char *str="auxiliary";维赋值,栈上存储指针,常量存储区存储字符串,sizeof str==4
constant:constant storage area
array:点赋值;数组的大小必须是确定的值(当然形参不必)
initialize:int a[3]={1,2,3};
assignment:a[0]=1;
a[3]={1,2,3}; //这个可是大错特错,只有初始化时能这么赋值
1D array:a==number axis orgin
a[i]:x=i的点
*(a+x)
define a:int *pa
2D array:a==rectangular CSYS orgin //*a=y线
a[i]:x=i的线
a[i][j]:x=i,y=j的点
*(*(a+x)+y)==*(*a+x*v+y)(线溢出)
define a:int (*pa)[v] //pa的步长就是整个a[v]数组
3D array:a==space rectangular CSYS orgin //*a==yz面,**a==z线
a[i]:x=i的面
a[i][j]:x=i,y=j的线
a[i][j][k]:x=i,y=j,z=k的点
*(*(*(a+x)+y)+z)==*(**a+x*v*w+y*w+z)(线溢出)
define a:int (*pa)[v][w] //pa的步长就是整个a[v][w]数组
function
define:head:type name(formal parameter)
formal parameter不用赋值直接参与计算,自有调用函数传值
body:basic statement:semicolon;
compound statement:braces{}
return expression
call and stack:执行完返回 //cdecl, stdcall
declare:function prototype:type name(int&,int&) //省略了extern
value call:在stack storage area为形参分配存储空间,实参将值传递给形参,形参值变化不影响实参值
variable:为形参变量分配存储空间,实参变量将值传递给形参变量,形参变量值变化不影响实参变量值
pointer:为形参指针分配存储空间,实参指针将值传递给形参指针,形参指针值变化并不影响实参指针值
形参指针指向的变量和实参指针指向的变量重合,前者变化影响后者
reference==alias:必须初始化,只有点赋值
nested call:
recursion call==degenerative case+call
递归使复杂问题处理起来简单明了。但是,递归在每一次执行时都要为返回地址,被调函数参数和局部变量分配栈空间,空间复杂度大,运行效率低,限制了递归的深度。因此,使用递归设计,堆栈或递推实现。
main formal parameter:main(int argc,char *argv[]) //argument
argc:命令行参数个数
argv:字符指针数组,依次指向命令行字符串
pointer:
constant:str constant==char pointer
function name==global pointer constant
array name==pointer constant
variable:新建的指针默认指向一个cannot written的存储区,先维赋值,再点赋值,当然形参不必;初始化0
字符串常量所处常量存储区也是cannot written的,不能先维赋值p字符串常量,再点赋值
assignment:等维等类型赋值
point:降维指向
relative address:
operate:ra==ptr2-ptr1==length-1
ptr2==ptr1+ra==&ptr1[ra]
loop:ra=(ra+1)%SIZE
ptr2=ptr1+(ptr2-ptr1+1)%SEZE
length=(ptr2-ptr1+1+SIZE)%SIZE
三种特殊类型指针:
charptr:constant:str
variable:char *p
arrayptr:constant:a
variable:1D:int (*pa)
2D:int (*pa)[v]
3D:int (*pa)[v][w]
functionptr:globle constant:f //函数指针常量一定是全局的
variable:int (*pf)(int&)
函数指针变量专题:
定义:int (*pf)(); //只是一个定义形式,不符合规范
使用:(*p)()==p(); //p()符合规范
强制类型转换:(int (*)())pf //将pf强制类型转换为int (*)()型指针
int (*pf)(void);
int f(void){
pf=&f; //以下全部合法,且结果一样
pf=***f;
pf();
(****pf)();
(***************f)();
}
数据传递:能调用不返回 //响应哪申请哪释放;out in
调用传递:值调用
引用调用
返回传递:值返回
引用返回
全局变量:少用
返回:expression作为实参传递给无名形参:值返回copyct临时量 //关键词少
引用返回
destruct被调函数局部变量 //空引用:引用返回局部变量
调用表达式结束进行后处理:值返回destruct临时量 //延长临时量生存期:值返回赋值引用
引用返回
全局量引用返回普通赋值,临时量值返回赋值引用
空引用:warning C4172: returning address of local variable or temporary
延长临时量生存期:用临时量初始化
初始化分为两种,一种是普通赋值即右值放进左值,另一种是给临时量起个名,这种会延长临时量生存期,均调用copyctor
字符串数组专题:
二维字符数组char a[][4]={"sun","mon","tue","wed","thu","fri","sat"} //栈存储
指针数组 char *a[]={"sun","Mon","Tue","Wed","Thu","Fri","Sat"} //栈存储指针,常量存储区存储字符串
a=(char **)malloc(7*sizeof(char*)) //char **p,p也代表指针数组
a[0]="sun" //堆存储指针,常量存储区存储字符串
a[1]="mon"
…………
structure:
typedef:struct Type{}; //class private and private inheritance by default
variabledef:Type name; //struct public and public inheritance by defualt
operators and expression //precedence优先级associativity结合性
字符组合:从左到右尽可能多的将若干个字符组合
infix→postfix:高出高进,等高亦出;直进直出
arithmetic conversion
double ← float 转换成同一类型,横向必转,纵向下往上转
↑
long
↑ 原来变量类型未发生变化
unsigned
↑
int ← char,short
sizeof:int a[5]; sizeof a==20,sizeof a[0]==4;
int a[5][6]; sizeof a==120,size of a[0]=24
sizeof("hello")==6
char a[]="hello"; sizeof a==6,
char * str="hello"; sizeof str==4
subscript operator:a[i]==*(a+i)
arithmetic operator:++ -- //副作用
*/% +- //整型/整型向0取整
value of expression:算出多少就多少
assignment operator:integer=float //floor,ceiling,round
……………… //+= optimize compile
value of expression:赋值后的变量的值
relational operator:< <= > >= == != //常量在前
value of expression:false0,ture1
logic operator:! && ||
value of expression:false0,ture1 //短路错误->
comma operator:X,Y
value of expression:求解X,求解Y,表达式的值==Y
conditional expression:X?trueexp:falseexp
bitwise operation:& | ^ ~ << >> //无幂运算符,pow
应用: 异或取反
pow(2,x)=1<<x
内存对齐(n+7)&~7
PS: volatile不优化
three basic program structure:sequential,branch(if,conditional expression,switch),loop(for,while,do while)
if:else和上边最近的未配对if配对
conditional expression:X?trueexp:falseexp;
switch:case必须相异,否则报错
找到匹配的case,从此case往下执行(不再进行判断)
thus:每个case必须有break,执行完此case就跳出
每个case的大括号也可以省了
多个连续case可以共用最后一个case的执行语句
最后一个case/default可以不加break
for(X;Y;Z):可读性高 //nested for实现函数关系
while:
do{x}while(Y);==X;while(Y){X}==for(X,Y,X);==while(Y){X} //当Y初始为true时
break and continue:只跳一层 //少用,改写为等效for
for(x;y;z){ ==for(x;y&&!a;c) b;
if(a) break;
b;
}
for(x;y;z){ ==for(x;y;z){
if(a) continue; if(!a) b;
b; }
}
ps: label:a; //大跳
if(condition)
goto label;
sublime:iterate==递推&&递归 //induction
数学表达式:n!=1*2*3*……*(n-1)*n
iterate:f(n)=f(n-1)*n //iterate,reiterate
f(1)=1
递推:fn=1;
for(i=2,i<=n,i++) fn*=i;
递归:if(n==1)return 1;
else return factorial(n-1)*n
Fibonacci numbers:f(n)=f(n-2)+f(n-1)
f(1)=1;f(2)=1;
递推:fll=1;fl=1; //未考虑n=1,n=2的情况 优化 fll=1;fn=1;
for(i=3,i<=n,i++){fn=fll+fl;fl=fll;fl=fn} {mid=fn;fn=fn+fll;fll=mid}
递归:if(n==1||n==2) return 1;
else return Fibonacci(n)=Fibonacci(n-2)+Fibonacci(n-1);
three procedure
迭代变量:不断由旧值推出新值
迭代关系式:旧值→新值公式
上下界控制:结束条件
变量, 缓冲区与文件: 文本文件: enter->'/n'->"/r/n" //'/r'==^m; vim在内存中%s
二进制文件
程序开始时, 系统自动打开标准输入,标准输出,标准出错文件, 自动关联stdin,stdout,stderr指针
struct _iobuf
{
char *_ptr;
int _cnt;
char *_base;
int _flag;
int _file;
int _charbuf;
int _bufsiz;
char *_tmpfname;
};
typedef struct _iobuf file;
文件操作: file * stream=fopen("test.txt", "r");
if(stream){
for(int i=0; (str[i]=fgetc(stream))!=eof; i++); str[i]='/0';
fclose(stream);
}
int fseek(file *stream, long offset, int base) //seek_set 0, seek_cur 1, seek_end 2
void rewind(file *stream) //rewind,forward
long ftell(file *stream) //返回当前位置,即相对于seek_set
int fflush(file *stream)
int feof(file *stream)
int ferror(file *stream)
void clearerr(file *stream) //ferror()和feof()置0
int remove(char *filename) //成功返回0; 完整路径
int rename(char *oldname, char *newname)
char str[100];
gets(str);
{int i=strlen(str); str[i++]='/n'; str[i]='/0';} //{}限制变量生存期
FILE *stream=fopen("test.txt","a");
fputs(str,stream);
fclose(stream);
scanf:扫描完格式串或无法匹配格式字符时终止 //忽略多余参数
返回成功匹配并赋值的输入项数,如果到达文件末尾,返回EOF
下一次调用scanf将紧接上一次扫描缓冲区
scanf框架程序
for(int i=0; i<N; ){
puts("Please input N items:"); ///0=>/n
i=scanf(szFormat, ...); //&
fflush(stdin);
}
library function
math.h:mod(modulo)
stdio.h:escape character/
int fprintf(FILE *stream, const char* szFormat, ...) //返回输出字符数
int sprintf(char *str, const char *szFormat, ...)
int snprintf(char *str, size_t size, const char *format, ...) //size-1,结尾补0
int printf(const char * szFormat, ...) //Dec,heX,Char,Str,Float,Exponent; -左对齐
printf("%s",str)==cout<<string
int fscanf(FILE *stream, char *szFormat, ...) //返回输入项数
int sscanf(const char *str, const char *szFormat, ...)
int scanf(char *szFormat, ...) //space||enter||tab结束
scanf("%s",str)==cin>>string
int fgetc(FILE *stream) //返回所得字符,出错返回EOF; fgetc(stream)!=EOF
int getchar()
char *fgets(char *str, int size, FILE *stream) //返回str,出错返回NULL; size-1,结尾补0
char *gets(char *str)
int getw(FILE *stream) //word
int fread(char *str, unsigned size, unsigned n, FILE *stream) //返回数据项个数; 长度为size的n个数据项
int fputc(char c, FILE *stream) //返回输出字符
int putchar(char c)
int fputs(char * str, FILE *stream) //返回0
int puts(char * str) ///0->/n,返回换行符
int putw(int w, FILE *stream) //word
int fwrite(char *str, unsigned size, unsigned n, FILE *stream) //返回数据项个数
溢出: 用printf("%s",str)而不用printf(str);
stdlib.h:char *itoa(int value, char *str, int radix)
int atoi(const char *str)
malloc:void *malloc(unsigned bytes) //void *语言, HGLOBAL平台
realloc:void *realloc(void *p,unsigned size)
试图直接从现存数据后面获得附加的字节,如果能够满足,自然天下太平
如果没有足够的空间,分配新块,复制数据,释放旧块。返回mem_address指针,原来的指针失效
free:void free(void *p) //申请了没释放,释放了没归0
int system(const char *s) //调用OS命令
char *getenv(const char *name) //取得系统环境变量
void abort() //非正常终止
void exit(int status=EXIT_SUCCESS) //正常终止
int atexit(void (*pf)()) //登记pf,pf将在程序正常终止时调用
string.h:unsigned int strlen(char *str) //长度不包括/0
char *strcpy(char *dest, char *src) //copy,返回str1
char *strcat(char *dest, char *src) //catenate,concatenate;str1后的/0取消,返回首地址
int strcmp(char *str1,char *str2) //compare第一个不同字符;1>2,返回值>0,1=2,返回值=0,1<2,返回值<0
char *strchr(char *str,char chr) //返回指向chr的指针
char *strstr(char *str1,char *str2)
ctype.h:
setjump.h:
ploy==strategy+tatic
最新推荐文章于 2020-07-23 23:50:42 发布