野指针
野指針
野指針指向一個已刪除的對象或未申請訪問受限內存區域的指針。
規避方法
空指針
1.NULL
NULL是一個宏,定義在C標準庫的<stddef.h>頭文件中。
在c中NULL是 (void* )0
#define NULL ((void *)0)在c++中,NULL就是整數0
#ifndef NULL #ifdef __cplusplus #define NULL 0 #else #define NULL ((void *)0) #endif #endif2.nullptr
c++ 關鍵字,其意義是 (void*)0
3.區別展示代碼
#include <iostream> using namespace std;void fun(int){cout << "hello \n"; }void fun(void*){cout << "world \n"; }int main(int argc, char *argv[]) {cout << "Hello World!" << endl;fun(0); //調用第一個函數fun(NULL); //調用第一個函數fun(nullptr); //調用第二個函數return 0; }總結
- 上一篇: 计算机科学与技术 美国 研究生 gpa3
- 下一篇: 正三角形c语言编程,三角形 (C语言代码