Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- //void freePointer(int** pointer);
- void saferFree(void** ptr);
- int main()
- {
- int* pointer = (int*)malloc(sizeof(int));
- *pointer = 10;
- printf("pointer 的數值 = %p\n", pointer);
- printf("對 pointer 取值 = %d\n", *pointer);
- printf("====== 釋放 pointer ======\n");
- saferFree((void**)&pointer);
- printf("pointer 的數值 = %p\n", pointer);
- printf("對 pointer 取值 = %d\n", *pointer);
- return 0;
- }
- /*
- void freePointer(int** pointer)
- {
- free(*pointer);
- *pointer = NULL;
- }
- */
- void saferFree(void** ptr)
- {
- if ( ptr != NULL && *ptr != NULL )
- {
- free(*ptr);
- *ptr = NULL;
- }
- }
- /***************************** 執行結果 ******************************
- pointer 的數值 = 007111F8
- 對 pointer 取值 = 10
- ====== 釋放 pointer ======
- pointer 的數值 = 00000000
- Process returned -1073741819 (0xC0000005) execution time : 4.992 s
- Press any key to continue.
- *********************************************************************/
Add Comment
Please, Sign In to add comment