近来有些时间,在开发过程中调试bug的时候,经常会遇到指针乱用或者,用了不free的现象,我这里写了个例子,把我的个人看法给有同样疑惑的朋友共勉,如果有错的地方,请指正,以一个人person为对象,展开进行的一些描述,需要仔细体会多级指针怎么分配内存的,可能会发现指针的使用还是比较有意思的,如果有什么错误请指出,经过测试,运行能通过,例子如下(如果你看过linux的内核代码,那么你对结构体的指针用法一定不会陌生,这里用的结构体指针有些面向对象的思想):
点击(此处)折叠或打开
- /*
- * This instance is doing a pointer usage
- * I list a person detail to explain the usage.
- */
- #include <stdlib.h>
- #include <stdio.h>
- #define NUMBER_GIRLFIREDN 1
- #define ANNUAL_INCOMING 10
- #define LENGHT 171
- struct basic_info
- {
- char *name;
- int lenght;
- char *birthday;
- char *qq;
- char *address;
- char *major;
- char *married;
- };
- struct company
- {
- char *company_name;
- int fundation_time;
- char *product;
- };
- struct work_exp
- {
- struct company *com;
- int num_company;
- };
- struct university
- {
- char *university_name;
- int history_time;
- int num_std;
- };
- struct study_exp
- {
- struct university *unt;
- int num_university;
- };
- struct person
- {
- struct basic_info *bs_info;
- struct work_exp *wk_info;
- struct study_exp *std_info;
- int num_gf;
- int incoming;
- };//注意看定义结构体指针时候的层次关系
- static void print_personal_inforamtion(struct person *p)
- {
- printf("The detail inforamtin as following:\n\
- Name: %s\n\
- Lenght: %d\n\
- Birthday: %s\n\
- QQ: %s\n\
- Address: %s\n\
- Major: %s\n\
- Married: %s\n\
- Girl friend(inclued ex): %d\n\
- Annaul incoming(so poor): %d\n\
- Work company toal number: %d\n\
- The fist company name: %s\n\
- The company fundation time: %d\n\
- The company's product: %s\n\
- The university number: %d\n\
- The university name: %s\n\
- The university history: %d\n\
- The university student number: %d\n",
- p->bs_info->name,
- p->bs_info->lenght,
- p->bs_info->birthday,
- p->bs_info->qq,
- p->bs_info->address,
- p->bs_info->major,
- p->bs_info->married,
- p->num_gf,
- p->incoming,
- p->wk_info->num_company,
- p->wk_info->com->company_name,
- p->wk_info->com->fundation_time,
- p->wk_info->com->product,
- p->std_info->num_university,
- p->std_info->unt->university_name,
- p->std_info->unt->history_time,
- p->std_info->unt->num_std);
- printf("Upper is my information, if you are have some problem to discuss, please add my QQ.\n");
- }
- static int person_information_free(struct person *p)//注意看free时候的顺序关系
- {
- if(p != NULL) {
- if(p->bs_info != NULL) {
- free(p->bs_info->name);
- p->bs_info->name = NULL;
- free(p->bs_info->birthday);
- p->bs_info->birthday = NULL;
- free(p->bs_info->qq);
- p->bs_info->qq = NULL;
- free(p->bs_info->address);
- p->bs_info->address = NULL;
- free(p->bs_info->married);
- p->bs_info->married = NULL;
- free(p->bs_info);
- p->bs_info = NULL;
- }
- if(p->wk_info != NULL) {
- if(p->wk_info->com != NULL) {
- free(p->wk_info->com->company_name);
- p->wk_info->com->company_name = NULL;
- free(p->wk_info->com->product);
- p->wk_info->com->product = NULL;
- free(p->wk_info->com);
- p->wk_info->com = NULL;
- }
- free(p->wk_info);
- p->wk_info = NULL;
- }
- if(p->std_info != NULL) {
- if(p->std_info->unt != NULL) {
- free(p->std_info->unt->university_name);
- p->std_info->unt->university_name = NULL;
- free(p->std_info->unt);
- p->std_info->unt = NULL;
- }
- free(p->std_info);
- p->std_info = NULL;
- }
- free(p);
- p = NULL;
- } else {
- printf("The student information have freed\n");
- }
- return 0;
- }
- /*Starting.........*/
- int main(int argc, char *argv[])
- {
- struct person *fuxin;
- fuxin = malloc(sizeof(struct person));// 这之后会用malloc分配很多内存,注意看层次关系
- if (!fuxin) {
- printf("Malloc the data for fuxin fail.\n");
- goto malloc_fail;
- }
- fuxin->num_gf = NUMBER_GIRLFIREDN;
- fuxin->incoming = ANNUAL_INCOMING;
- fuxin->bs_info = malloc(sizeof(struct basic_info));
- if (!fuxin->bs_info) {
- printf("Malloc the basic infomation for fuxin fail.\n");
- goto malloc_fail;
- }
- fuxin->bs_info->lenght = LENGHT;
- fuxin->bs_info->name = malloc(sizeof(char) * 10);
- if (!fuxin->bs_info->name) {
- printf("Malloc the name for fuxin fail.\n");
- goto malloc_fail;
- }
- strcpy(fuxin->bs_info->name, "Who am I");
- fuxin->bs_info->birthday = malloc(sizeof(char) * 10);
- if (!fuxin->bs_info->birthday) {
- printf("Malloc the birthday information for fuxin fail.\n");
- goto malloc_fail;
- }
- strcpy(fuxin->bs_info->birthday, "1983-8-8");
- fuxin->bs_info->qq = malloc(sizeof(char) * 13);
- if (!fuxin->bs_info->birthday) {
- printf("Malloc the qq information for fuxin fail.\n");
- goto malloc_fail;
- }
- strcpy(fuxin->bs_info->qq, "1320332906");
- fuxin->bs_info->address = malloc(sizeof(char) * 100);
- if (!fuxin->bs_info->address) {
- printf("Malloc the address information for fuxin fail.\n");
- goto malloc_fail;
- }
- strcpy(fuxin->bs_info->address, "Wangjing east road, chao yang, BeiJing");
- fuxin->bs_info->major = malloc(sizeof(char) * 10);
- if (!fuxin->bs_info->major) {
- printf("Malloc the birthday information for fuxin fail.\n");
- goto malloc_fail;
- }
- strcpy(fuxin->bs_info->major, "Computer");
- fuxin->bs_info->married = malloc(sizeof(char) * 10);
- if (!fuxin->bs_info->married) {
- printf("Malloc the married information for fuxin fail.\n");
- goto malloc_fail;
- }
- strcpy(fuxin->bs_info->married, "No married");
- fuxin->wk_info = malloc(sizeof(struct work_exp));
- if (!fuxin->wk_info) {
- printf("Malloc the work information for fuxin fail.\n");
- goto malloc_fail;
- }
- fuxin->wk_info->num_company = 1;
- fuxin->wk_info->com = malloc(sizeof(struct company));
- if (!fuxin->wk_info->com) {
- printf("Malloc the work company information for fuxin fail.\n");
- goto malloc_fail;
- }
- fuxin->wk_info->com->fundation_time = 25;
- fuxin->wk_info->com->company_name = malloc(sizeof(char) * 20);
- if (!fuxin->wk_info->com->company_name) {
- printf("Malloc the work company name information for fuxin fail.\n");
- goto malloc_fail;
- }
- strcpy(fuxin->wk_info->com->company_name, "Motorola INC.");
- fuxin->wk_info->com->product = malloc(sizeof(char) * 40);
- if (!fuxin->wk_info->com->product) {
- printf("Malloc the work company product information for fuxin fail.\n");
- goto malloc_fail;
- }
- strcpy(fuxin->wk_info->com->product, "Mobile phone XT910, MT917");
- fuxin->std_info = malloc(sizeof(struct study_exp));
- if (!fuxin->std_info) {
- printf("Malloc the study exp information for fuxin fail.\n");
- goto malloc_fail;
- }
- fuxin->std_info->num_university = 1;
- fuxin->std_info->unt = malloc(sizeof(struct university));
- if (!fuxin->std_info->unt) {
- printf("Malloc the study university information for fuxin fail.\n");
- goto malloc_fail;
- }
- fuxin->std_info->unt->history_time = 100;
- fuxin->std_info->unt->num_std = 10000;
- fuxin->std_info->unt->university_name = malloc(sizeof(char) * 20);
- if (!fuxin->std_info->unt->university_name) {
- printf("Malloc the study university name information for fuxin fail.\n");
- goto malloc_fail;
- }
- strcpy(fuxin->std_info->unt->university_name, "My UNIVERSITY");
- malloc_success:
- printf("setup personal information success, as following......\n");
- printf("=========================================================================================\n");
- print_personal_inforamtion(fuxin);
- printf("=========================================================================================\n");
- malloc_fail:
- person_information_free(fuxin); //不用内存的时候,一定要记得free
- return 0;
- }
run the program as below:
gcc pointer.c
./a.out
运行结果:
=========================================================================================
The detail inforamtin as following:
Name: Who am I
Lenght: 171
Birthday: 1983-8-8
QQ: 1320332906
Address: Wangjing east road, chao yang, BeiJing
Major: Computer
Married: No married
Girl friend(inclued ex): 1
Annaul incoming(so poor): 10
Work company toal number: 1
The fist company name: Motorola INC.
The company fundation time: 25
The company's product: Mobile phone XT910, MT917
The university number: 1
The university name: My UNIVERSITY
The university history: 100
The university student number: 10000
Upper is my information, if you are have some problem to discuss, please add my QQ
=========================================================================================
通过上面这个简单的例子,可以看到其实指针使用不算复杂, 一个原则是不用的时候就free,对于结构体嵌套的情况,要先free内层结构的指针,再free外层结构的指针,虽然例子简单,但是对于看内核代码还是有好处的,如果有条件的话,最好连接上trace32跟踪一下内存,那样理解将会更加深刻。