【C语言】10-动态内存分配

    技术2024-03-17  94

    此笔记由个人整理

    塞上苍鹰_fly

    课程来自:尚观C

    一、动态内存

    void *malloc(size_t size); void free(void *ptr); void *calloc(size_t nmemb,size_t size); void *realloc(void *ptr,size_t size);

    案例:申请空间与释放空间

    #include "stdio.h" #include "stdlib.h" int main() { int *p = NULL; p = (int *)malloc(sizeof(int));//测试环境为Windows //p = malloc(sizeof(int));//测试环境为Linux if(p == NULL) { printf("malloc() error!!\n"); exit(1); } *p = 10; printf("%p-->%d\n",*p,*p); free(p); *p = 123; printf("%p-->%d\n",*p,*p); exit(0); }

    free是通过之前使用的指针不能再去到之前所指向的地址

    free后不要在继续使用之前定义的空间,会产生野指针,容易出现不必要的错误

    结果(windows)

    案例:动态变长数组

    #include "stdio.h" #include "stdlib.h" int main() { int *p = NULL; int num = 5; int i; p = malloc(sizeof(int) * num); for(i = 0;i < num;i++) scanf("%d",&p[i]); for(i = 0;i < num;i++) printf("%d",p[i]); printf("\n"); exit(0); } 结果

    案例:传参避免内存泄露

    #include "stdio.h" #include "stdlib.h" void func(int **p,int n) { *p = malloc(n); if(*p == NULL) exit(1); return ; } int main() { int *p = NULL; int num = 100; func(&p,num); free(p);//谁申请谁释放 exit(0); } #include "stdio.h" #include "stdlib.h" void *func(int *p,int n) { p = malloc(n); if(p == NULL) exit(1); return p; } int main() { int *p = NULL; int num = 100; p = func(&p,num); free(p);//谁申请谁释放 exit(0); }

    二、优化学生信息管理系统

    替换结构体成员变量name为*name #include "stdio.h" #include "stdlib.h" #include "string.h" #define NAMEMAX 1024 struct student_st { int id; char *name; int math; int chinese; }; void menu(void) { printf("********************************\n"); printf("1 set\n2 change name\n3 show\n"); printf("********************************\n"); } void stu_set(struct student_st *p,const struct student_st *q) { p->id = q->id; p->name = malloc(strlen(q->name)+1); if(p->name == NULL) exit(1); strcpy(p->name,q->name); p->math = q->math; p->chinese = q->chinese; } void stu_show(struct student_st *p) { printf("%d %s %d %d\n",p->id,p->name,p->math,p->chinese); } void stu_changename(struct student_st *p,const char *newname) { free(p->name); p->name = malloc(strlen(newname)+1); strcpy(p->name,newname); } int main() { int choice; int ret; char newname[NAMEMAX]; struct student_st stu; struct student_st tmp; menu(); do { printf("\nPlease enter num(q for quit):"); ret = scanf("%d",&choice); if(ret != 1) break; switch(choice) { case 1: tmp.name = malloc(NAMEMAX); printf("Please enter for the stu[id name math chinese]:"); scanf("%d %s %d %d",&tmp.id,&tmp.name,&tmp.math,&tmp.chinese); stu_set(&stu,&tmp); break; case 2: printf("Please enter the newname:"); scanf("%s",newname); stu_changename(&stu,newname); break; case 3: stu_show(&stu); break; default: exit(1); } }while(1); exit(0); }

    三、typedef

    为已有的数据类型改名 typedef 已有的数据类型 新名字

    案例

    #include "stdio.h" #include "stdlib.h" typedef int INT; int main() { INT i = 100; printf("%d\n",i); exit(0); } 结果

    typedef用法

    #define INT int typedef int INT; INT i; --> int i; --------------------------------------- #define IP int * typedef int IP; IP p,q; --> int *p,q; IP p,q; --> int *p,*q; ---------------------------------------- typedef int ARR[6]; --> int [6] ->ARR; ARR a; --> int a[6]; ---------------------------------------- struct node_st{ }; typedef struct node_st NODE; NODE a; -> struct node_st a; NODE *P;-> struct node_st *p; typedef struct node_st *NODEP; NODEP p; -> struct node_st *p; ---------------------------------------- typedef struct { int i; float f; }NODE,*NODEP; ---------------------------------------- typedef int FUNC(int); --> int(int) FUNC; FUNC F; -> int f(int); typedef int *FUNCP(int); FUNC p; ---> int *p(int); typedef int *(*FUNCP)(int); FUNC p; --> int *(*p)(int);

    案例

    #include "stdio.h" #include "stdlib.h" #include "string.h" #define NAMEMAX 1024 typedef struct student_st { int id; char *name; int math; int chinese; }STU; void menu(void) { printf("********************************\n"); printf("1 set\n2 change name\n3 show\n"); printf("********************************\n"); } void stu_set(STU *p,const STU *q) { p->id = q->id; p->name = malloc(strlen(q->name)+1); if(p->name == NULL) exit(1); strcpy(p->name,q->name); p->math = q->math; p->chinese = q->chinese; } void stu_show(STU *p) { printf("%d %s %d %d\n",p->id,p->name,p->math,p->chinese); } void stu_changename(STU *p,const char *newname) { free(p->name); p->name = malloc(strlen(newname)+1); strcpy(p->name,newname); } int main() { int choice; int ret; char newname[NAMEMAX]; STU stu; STU tmp; menu(); do { printf("\nPlease enter num(q for quit):"); ret = scanf("%d",&choice); if(ret != 1) break; switch(choice) { case 1: tmp.name = malloc(NAMEMAX); printf("Please enter for the stu[id name math chinese]:"); scanf("%d %s %d %d",&tmp.id,&tmp.name,&tmp.math,&tmp.chinese); stu_set(&stu,&tmp); break; case 2: printf("Please enter the newname:"); scanf("%s",newname); stu_changename(&stu,newname); break; case 3: stu_show(&stu); break; default: exit(1); } }while(1); exit(0); }
    Processed: 0.022, SQL: 9