关于C语言指针的用法

1910阅读 3评论2012-04-22 android_bsp
分类:C/C++

近来有些时间,在开发过程中调试bug的时候,经常会遇到指针乱用或者,用了不free的现象,我这里写了个例子,把我的个人看法给有同样疑惑的朋友共勉,如果有错的地方,请指正,以一个人person为对象,展开进行的一些描述,需要仔细体会多级指针怎么分配内存的,可能会发现指针的使用还是比较有意思的,如果有什么错误请指出,经过测试,运行能通过,例子如下(如果你看过linux的内核代码,那么你对结构体的指针用法一定不会陌生,这里用的结构体指针有些面向对象的思想):

点击(此处)折叠或打开

  1. /*
  2. * This instance is doing a pointer usage
  3. * I list a person detail to explain the usage.
  4. */
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #define NUMBER_GIRLFIREDN 1
  8. #define ANNUAL_INCOMING 10
  9. #define LENGHT 171
  10. struct basic_info
  11. {
  12. char *name;
  13. int lenght;
  14. char *birthday;
  15. char *qq;
  16. char *address;
  17. char *major;
  18. char *married;
  19. };
  20. struct company
  21. {
  22. char *company_name;
  23. int fundation_time;
  24. char *product;
  25. };
  26. struct work_exp
  27. {
  28. struct company *com;
  29. int num_company;
  30. };
  31. struct university
  32. {
  33. char *university_name;
  34. int history_time;
  35. int num_std;
  36. };
  37. struct study_exp
  38. {
  39. struct university *unt;
  40. int num_university;
  41. };
  42. struct person
  43. {
  44. struct basic_info *bs_info;
  45. struct work_exp *wk_info;
  46. struct study_exp *std_info;
  47. int num_gf;
  48. int incoming;
  49. };//注意看定义结构体指针时候的层次关系
  50. static void print_personal_inforamtion(struct person *p)
  51. {
  52. printf("The detail inforamtin as following:\n\
  53. Name:     %s\n\
  54. Lenght:     %d\n\
  55. Birthday:     %s\n\
  56. QQ:     %s\n\
  57. Address:     %s\n\
  58. Major:     %s\n\
  59. Married:     %s\n\
  60. Girl friend(inclued ex):     %d\n\
  61. Annaul incoming(so poor):     %d\n\
  62. Work company toal number:     %d\n\
  63. The fist company name:     %s\n\
  64. The company fundation time:     %d\n\
  65. The company's product:     %s\n\
  66. The university number:     %d\n\
  67. The university name:     %s\n\
  68. The university history:     %d\n\
  69. The university student number:     %d\n",
  70. p->bs_info->name,
  71. p->bs_info->lenght,
  72. p->bs_info->birthday,
  73. p->bs_info->qq,
  74. p->bs_info->address,
  75. p->bs_info->major,
  76. p->bs_info->married,
  77. p->num_gf,
  78. p->incoming,
  79. p->wk_info->num_company,
  80. p->wk_info->com->company_name,
  81. p->wk_info->com->fundation_time,
  82. p->wk_info->com->product,
  83. p->std_info->num_university,
  84. p->std_info->unt->university_name,
  85. p->std_info->unt->history_time,
  86. p->std_info->unt->num_std);
  87. printf("Upper is my information, if you are have some problem to discuss, please add my QQ.\n");
  88. }
  89. static int person_information_free(struct person *p)//注意看free时候的顺序关系
  90. {
  91. if(p != NULL) {
  92. if(p->bs_info != NULL) {
  93. free(p->bs_info->name);
  94. p->bs_info->name = NULL;
  95. free(p->bs_info->birthday);
  96. p->bs_info->birthday = NULL;
  97. free(p->bs_info->qq);
  98. p->bs_info->qq = NULL;
  99. free(p->bs_info->address);
  100. p->bs_info->address = NULL;
  101. free(p->bs_info->married);
  102. p->bs_info->married = NULL;
  103. free(p->bs_info);
  104. p->bs_info = NULL;
  105. }
  106. if(p->wk_info != NULL) {
  107. if(p->wk_info->com != NULL) {
  108. free(p->wk_info->com->company_name);
  109. p->wk_info->com->company_name = NULL;
  110. free(p->wk_info->com->product);
  111. p->wk_info->com->product = NULL;
  112. free(p->wk_info->com);
  113. p->wk_info->com = NULL;
  114. }
  115. free(p->wk_info);
  116. p->wk_info = NULL;
  117. }
  118. if(p->std_info != NULL) {
  119. if(p->std_info->unt != NULL) {
  120. free(p->std_info->unt->university_name);
  121. p->std_info->unt->university_name = NULL;
  122. free(p->std_info->unt);
  123. p->std_info->unt = NULL;
  124. }
  125. free(p->std_info);
  126. p->std_info = NULL;
  127. }
  128. free(p);
  129. p = NULL;
  130. } else {
  131. printf("The student information have freed\n");
  132. }
  133. return 0;
  134. }
  135. /*Starting.........*/
  136. int main(int argc, char *argv[])
  137. {
  138. struct person *fuxin;
  139. fuxin = malloc(sizeof(struct person));// 这之后会用malloc分配很多内存,注意看层次关系
  140. if (!fuxin) {
  141. printf("Malloc the data for fuxin fail.\n");
  142. goto malloc_fail;
  143. }
  144. fuxin->num_gf = NUMBER_GIRLFIREDN;
  145. fuxin->incoming = ANNUAL_INCOMING;
  146. fuxin->bs_info = malloc(sizeof(struct basic_info));
  147. if (!fuxin->bs_info) {
  148. printf("Malloc the basic infomation for fuxin fail.\n");
  149. goto malloc_fail;
  150. }
  151. fuxin->bs_info->lenght = LENGHT;
  152. fuxin->bs_info->name = malloc(sizeof(char) * 10);
  153. if (!fuxin->bs_info->name) {
  154. printf("Malloc the name for fuxin fail.\n");
  155. goto malloc_fail;
  156. }
  157. strcpy(fuxin->bs_info->name, "Who am I");

  158. fuxin->bs_info->birthday = malloc(sizeof(char) * 10);
  159. if (!fuxin->bs_info->birthday) {
  160. printf("Malloc the birthday information for fuxin fail.\n");
  161. goto malloc_fail;
  162. }
  163. strcpy(fuxin->bs_info->birthday, "1983-8-8");
  164. fuxin->bs_info->qq = malloc(sizeof(char) * 13);
  165. if (!fuxin->bs_info->birthday) {
  166. printf("Malloc the qq information for fuxin fail.\n");
  167. goto malloc_fail;
  168. }
  169. strcpy(fuxin->bs_info->qq, "1320332906");
  170. fuxin->bs_info->address = malloc(sizeof(char) * 100);
  171. if (!fuxin->bs_info->address) {
  172. printf("Malloc the address information for fuxin fail.\n");
  173. goto malloc_fail;
  174. }
  175. strcpy(fuxin->bs_info->address, "Wangjing east road, chao yang, BeiJing");
  176. fuxin->bs_info->major = malloc(sizeof(char) * 10);
  177. if (!fuxin->bs_info->major) {
  178. printf("Malloc the birthday information for fuxin fail.\n");
  179. goto malloc_fail;
  180. }
  181. strcpy(fuxin->bs_info->major, "Computer");
  182. fuxin->bs_info->married = malloc(sizeof(char) * 10);
  183. if (!fuxin->bs_info->married) {
  184. printf("Malloc the married information for fuxin fail.\n");
  185. goto malloc_fail;
  186. }
  187. strcpy(fuxin->bs_info->married, "No married");
  188. fuxin->wk_info = malloc(sizeof(struct work_exp));
  189. if (!fuxin->wk_info) {
  190. printf("Malloc the work information for fuxin fail.\n");
  191. goto malloc_fail;
  192. }
  193. fuxin->wk_info->num_company = 1;
  194. fuxin->wk_info->com = malloc(sizeof(struct company));
  195. if (!fuxin->wk_info->com) {
  196. printf("Malloc the work company information for fuxin fail.\n");
  197. goto malloc_fail;
  198. }
  199. fuxin->wk_info->com->fundation_time = 25;
  200. fuxin->wk_info->com->company_name = malloc(sizeof(char) * 20);
  201. if (!fuxin->wk_info->com->company_name) {
  202. printf("Malloc the work company name information for fuxin fail.\n");
  203. goto malloc_fail;
  204. }
  205. strcpy(fuxin->wk_info->com->company_name, "Motorola INC.");
  206. fuxin->wk_info->com->product = malloc(sizeof(char) * 40);
  207. if (!fuxin->wk_info->com->product) {
  208. printf("Malloc the work company product information for fuxin fail.\n");
  209. goto malloc_fail;
  210. }
  211. strcpy(fuxin->wk_info->com->product, "Mobile phone XT910, MT917");
  212. fuxin->std_info = malloc(sizeof(struct study_exp));
  213. if (!fuxin->std_info) {
  214. printf("Malloc the study exp information for fuxin fail.\n");
  215. goto malloc_fail;
  216. }
  217. fuxin->std_info->num_university = 1;
  218. fuxin->std_info->unt = malloc(sizeof(struct university));
  219. if (!fuxin->std_info->unt) {
  220. printf("Malloc the study university information for fuxin fail.\n");
  221. goto malloc_fail;
  222. }
  223. fuxin->std_info->unt->history_time = 100;
  224. fuxin->std_info->unt->num_std = 10000;
  225. fuxin->std_info->unt->university_name = malloc(sizeof(char) * 20);
  226. if (!fuxin->std_info->unt->university_name) {
  227. printf("Malloc the study university name information for fuxin fail.\n");
  228. goto malloc_fail;
  229. }
  230. strcpy(fuxin->std_info->unt->university_name, "My UNIVERSITY");
  231. malloc_success:
  232. printf("setup personal information success, as following......\n");
  233. printf("=========================================================================================\n");
  234. print_personal_inforamtion(fuxin);
  235. printf("=========================================================================================\n");
  236. malloc_fail:
  237. person_information_free(fuxin); //不用内存的时候,一定要记得free
  238. return 0;
  239. }
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跟踪一下内存,那样理解将会更加深刻。

上一篇:C语言复杂指针说明
下一篇:arm体系

文章评论