c语言深度剖析

/*
 c语言深度剖析
 第五章 内存管理
 定义指针变量的同时最好初始化为NULL,用完之后也将指针设置成NULL;
 heap: 由malloc系列函数或new操作符分配的内存.其生命周期由free或delete决定.
 stack:效率高,但空间有限.保存局部变量.在函数内存在,函数结束,内容自动销毁.
 静态区:由编译器分配,整个程序的生命周期都存在.

 1结构体成员指针为初始化.
 struct strdent
 {
    char *name;
    int score;
}stu,*pstu;
int main()
{
strcpy(stu.name,"Jimy");//pstu = (struct student*)malloc(sizeof(struct student));  结构体指针的内存
//pstu->name = (char*)malloc(strlen("Jimy")+1);  成员指针的内存
strcpy(pstu->name,"Jimy");
stu.score = 99;
//free(pstu);
return 0;


}

 函数的入口校验
 函数入口使用assert(NULL != p);  assert 为宏 括号内为假,则终止程序,并提示错误信息

 为指针分配的内存大小: 初始化为NULL
 char* p2 = (char*)malloc(sizeof(char)*strlen(p1)+sizeof(char));
 数组初始化:
 int a[10] = {0};
 memset(a,0,sizeof(a));  //sizeof(a) = 40;


内存越界:
int a[10] ={0};
for(i=0;i<=10;i++){

    a[i] =i;  //a[10]  越界
}

内存泄漏:

由molloc列列函数和new操作符分配的内存用完没有即时释放,直到程序终止.
(void*)malloc(int size);  如果heap剩余内存块小于malloc 函数申请的连续空间,申请失败
内存释放:
free 斩断指针变量与这块内存的关系
free 之后要把指针设为NULL;

第6 章:
函数:
每一个函数都必须都有注释,即使函数只用几行
function name:
create time:
Author:
Description:
param:
return code:
global variable:
file static variable:
function static variable:
revison history:

变量定义与函数语句之间要加空行.

采用缩进.
多文件负责本身文件的全局变量,同时提供对外函数.
不要直接读写全局变量.
void str_copy(char* strDestination,const char* strSoutce);
char c;
c = getchar();  getchar()返回一个int
if (EOF == c)  EOF == -1;
{
    ...
}

void fun(int i)
{
    if(i>0)
    {
        fun(i/2);
     }
     printf("%d/n",i);
}

第七章 文件结构
file Name:
copyright:
module Name:

第一章 关键字:
定义:编译器创建一个对象,为这个对象分配一块内存,
并给它取上名字.
regiser:请求尽量将变量存放在内部寄存器;不能用&来获取地址,不在内存中

static: 修饰变量  在内存的静态区  作用域本文件
static: 修饰函数 在本地文件可用

命名规则:
global variable      g
flie static variable  n
function static variable  f
auto variable   a
global function      g
static funtion    a

bit     bit
b   boolean
char  c_variable
int   i;
short   s
long   l
unsigned   u
double  d
float   f
printer   p
void   v
enum  struct   union    st
function point   p

sizeof:
int a[100];sizeof(a)=400;

if((fTestvl >= -EPSION)&&(ftestval <=EPSION)

长循环在最内层,效率高
int a[col][row]={0};
for(col=0;col<5;col++)
    for(row=0;row<100;row++)
    sum=sum+a[col][row];
半开半闭:
    for(n=0;n<10;n++)
闭区间
    for(no=0;n<=10;n++)

const int N=5;
编译器通常不会为普通的const只读变量分配内存
声明外部变量:
extern  int i;

编译器认为任何一种数据类型都有大小,用它来定义变量能够分配足够大小的空间



static int j;
void fun1(void)
{
    static int i=0;
    i++;
}

void fun2(void)
{
    j=0;
    j++;
}
int main()
{
    int k=0;
    for(k=0;k<10;k++)
    {
        fun1();
        fun2();
}
return 0;
}
  i ==10; j==0;



  typedef int a[10]  相当于数组类型a
  a b[10]  ==  int  b[10][10]


 1 编译后注释会被空格替换
   y=x/ *p

   符号:
   0x01 << 2+3  =32

   q = a/b;
   r=a%b;

   第三章  预处理
   #error  error-message

   #define SQR(x) printf("The square of "#x" is %d \n",((x)*(x)))

   int* p=NULL  和*p=NULL

   *p=NULL;  p指向内存的赋值为NULL
   int i=10;
   int *p = (int*)0x12ff7c;    p----->0x12ff7c
   *p =NULL;                   *p----->10--->NULL
    p=NULL;


    数组:
    int a[5] = {1,2,3,4,5};
    &a+1  取数组a的首地址加sizeof(a)即  a+ 5*sizeof(int)




#include <stdio.h>
#include <assert.h>
#include <stdlib.h>

typedef struct st_type{
    int i;
    int a[];

}type_a;

int my_strlen(const char* strDest)
{
    assert(NULL != strDest);
    if('\0' == *strDest)   //记得加*
    {
            return 0;

    }
    else
    {
        return (1+my_strlen(++strDest));

    }

}

int my_strlen(const char* strDest)
{
    assert(strDest);
    return ('\0' != *strDest)?(1+my_strlen(strDest+1)):0;

}
*/

#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
int main()
{

//type_a* p =(type_a*)malloc(sizeof(type_a)+100*sizeof(int));

    char a[5]={'A','B','C','D','E'};
    char (*p3)[10] = &a;   //整个数组的首地址
    char (*p4)[10] = a;   //a 单个字符的指针
    printf("%c\n",*(p3+1));
    printf("%c\n",*(p4+1));
    return 0;


   // register a=10;
   // printf("%p",&a);
    const char *str = "hello!";
    int cData = my_strlen(str);
    fprintf(stdout,"%d\n",cData);

   //fprintf(stdout,"%d\n",EOF);  EOF =-1;
    char *p=(char*)malloc(10);
    printf("%p\n",p);
    fprintf(stdout,"%s\n",p);
    strcpy(p,"hello");
    fprintf(stdout,"%s\n",p);
    free(p);
   // fprintf(stdout,"%s\n",p);    //错误操作
   // int a=9;
    int b[10]={0};
    fprintf(stdout,"%d\n",sizeof(b));

  //  assert(NULL != p);

    printf("Hello World!\n");
    return 0;


}


posted @ 2019-05-25 15:07  countryboy666  阅读(283)  评论(0编辑  收藏  举报