???????????
????????? C ??????д?????????????????????????——??????????????????????????????????……???????? C ??????Щ?????????????????????????????????????????? C ????????????????
??????????????? malloc ????????????????????????????????????? NULL????????????????????????????м?? malloc ?????????? NULL??????????????????? malloc ??????? NULL ?????????ɡ???????????????????????????????????????????????е?????????????????????н?????????й??????????????????????棬???????? malloc ????? NULL ???????????棬??????????????????????????λ??????????????? malloc ??????? NULL ????????????????????????????????????? valgrind??
??????????ж? malloc ????????м??????????籾???????? @???? ???????Щ????????????????????
#include
#include
#define SAFE_MALLOC(n) safe_malloc(n)
void * safe_malloc(size_t n) {
void *p = malloc(n);
if (p) {
return p;
} else {
printf("?????治?????????????n");
exit(-1);
}
}
int main(void) {
int *p = SAFE_MALLOC(sizeof(int));
... ... ...
return 0;
}
??????????????????????????? malloc ?????????? NULL???????????????????????????????????????м???????????? NULL??NULL ????????????????????????????????????????????????????????????????????? NULL ???????б???????????????????????????????????????????????????????????????? NULL??????????????????????????????????????????????????????????????????????????????? NULL……????????????????????????????磺
????void foo(int *p)
????{
????if(!p) {
????printf("You passed a NULL pointer!n");
????exit(-1);
????}
????... ... ...
????}
????int main(void)
????{
????int *p;
????foo(p);
????return 0;
????}
????????????δ???????????? foo ???????foo ??????????????????κ????á?
?????????????????? foo ??????????????? p ???????? NULL????????Щ????????????????????????????????????????????????????????????????????????????????????????foo ???????????????Ч???????????ο??????????????????——????????? NULL????????????δ????????????????????????????????????????????? NULL?????????????????? foo ?е? NULL ???????Ч??????????????????????????????????????????????????????????????????????????????????????????????????????????????????Щ?????????????????????????????????????????
????????????????????????????????????????????????????????????????????????????????¥????????????????????и?????????????????????? NULL ?????????????????????????????????? C ?????У?????????? NULL ??????????????????????????????????????????????Ч??????????ó?????????????
??????????????????? NULL ?й?????????????????????????????????????????? NULL??????????????????????????????? NULL????????????????????——???????к?????????????????????????????????????????????????????????????????????????????????????????????ζ???????????????????????????????????????????? NULL ?????????????????????λ?????????????????????????????????????????????????????????????
?????????????????????????????????????
#include
#include
#define SAFE_FREE(p) safe_free((void **)(&(p)))
void safe_free(void **p) {
if(*p) {
free(*p);
*p = NULL;
} else {
printf("??????????????????n");
}
}
int main(void) {
int *p = malloc(sizeof(int));
for(int i = 0; i
?????????????????汻????????????????? NULL??????????Щ?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? NULL????????????????????????????????????????????ü?????????????????????????ü?????????????Щ???????????????????μ??????????????????????????ü???? 0 ??????????????????????????????д???????? unref???????????????
?????????????????? NULL ????????????????? NULL ????????????????????????У???????????????е???????????? NULL ???ж?????????????????Щ??????????? NULL ??????????????????????????????????? NULL ??????????????
???????????????????????и?????????????ú??????? NULL????????????????????????С????????????????????????? Java ?????????????????????????????? C ???????????? C ?п????? setjmp/longjmp ??????????????? NULL ???á???Щ???ζ??????????????????????ú??????? NULL ???к?????????????????????????????????????? C ???????????????????????????????????? 40 ???????裬????С?????????????????????????????? NULL??????????????? NULL ??????á??????????? NULL ??????????С??????????????????????????????????????????????
????????????????????????????????Ч???????????????????????????????????Ч???????????????顣??????????????????????п?????? NULL ????????? NULL ??飬???????к????????????е? ATM ????????????????????????????????????????????????? ATM ?3?????????????????????·???????к?????????????????????????????????????????????????????????????????????????????
???????????????????????? C ???????????в?????????????????????????????????????????????????????? NULL???????????Ч????????????????????????????????????????????????????????????????????????????????£?????????????????????????????????????????????????????????????????????????????????б???????????????????????????????????????У??????????????????????????? bug??????????е???????????????????????????????????顣??????? NULL ?????岻????????????壬???ó?????????? NULL ????????к????????????????????????д????
????????????????????????? NULL ?????????????……?????????
????if(p == NULL) {
????... ... ...
????}
???????????
????if(!p) {
????... ... ...
????}
?????
???????????3??????????????????????ж????????????????????Щ C ???????????????????У?NULL ?????????? 0???????????????? C99 ???? C ????????????????? C99 ?????????????C99 ????? 6.3.2.3 ???????????????????? 0????????Щ?? C ??????????????????????????????????????ж?????
?????? C ???????????????????????????????????????? C ?????????????????????????????????????У????????в??????????????????????????????????????????????3??????????????????????????????????????????????????????Щ???????????????????????????????????????????????????????????????????????????????????????????£????μ???????????ЩС???????????????????????′?????????????????????ü???????????????? Java ??? GC???????????????????????????????????????????
?????????????ζ
?????????????????????????????????????μ???????????????????????ζ????????????????Щ?ζ?????????????
??????????ζ?????????????????????????????????????????????????????????????κ??????????????????鶼????н??μ?????? ???? = ????? + ???????????????? Pascal ??? Nicklaus Wirth ??????????????????????????? ??? = ???? + ???????????????????????ζ?????????Щ??????????????????????????????????????????????????????????????????????????Щ??????????????????????????????????????????????????????????????????????????????????3???
???????????????????????????????????????????????????ì?????????????????У?????????????????????????????????????????????????????κ????????????????????????????????κ?????????????????????????????????????ε????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
??????????ζ??????????????????????????????????????????????????????????????????????????????????????????????л???????δ?????????????????????·?????????????С???????????????????????????д??????????????飬???? C ????????е???????????????Щ??ζ??????????????????????д??Щ??????????????????????????????????????????Щ?????????????????????????ЩС????????????????????Щ??Ρ???????????????????Щ??ξ???????????????????????Ρ?
???????????????????????????????κ?????? C ??????и?????????????????????????????????????????????C ?????????? Dannis Ritche ???A language that doesn’t have everything is actually easier to program in than some that do. ??????????????????????е???????????????Щ????????е???????????????
????????????£?????д???????????????????????????????????????????Ч——?????????????????????????????????????????ú?????????????????????????????????????????????????????????????????????д????????????????????????????????????????????????????????????????????????????????????????????????????????Щ??????????????????á??????????????????????κα??????????????????????????????????????????????????????????κ????????????????κα????????????????????????ν??棨GUI???????????????????????????????ζ?????????????????????????????????п????????????????Щ??????????????????????????????????д?????????????д??????С???????????????????????????????????????????????
???????????ζ????????????????????????????Ч??????????????? (1) ???д?????????(2) ??????????μ????(3) ???д???????????? read the fucking source code. ???????????????????д???????????Ч???????????????£??????????????????д?? fucking source code???????????????衣??????????????д?????????????????????????????????????????·???Щ??????????????????? Linus ???????????????????????????????д?????????????????????????????й???в2?????????????????????????……?????????????????????????????????????????????????????????????????С?????????????????? Donald Knuth ????????y???????????????????????????????????????д??????д???????磬?????????????????????????????????????
????????????????????????????????????Щ?ζ?????????????? C ???????????????????????????????????????? Peter Norvig ??????????????????????????????????