c++程序在读取NULL引用时崩溃
C++ program crashes upon reading NULL reference
这是c++中的一个链接结构。这里的所有变量和对象都应该有定义好的地址。但是,执行结果却不是这样。
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
struct LineData {
int ID;
char name[33];
LineData *next;
};
class Line {
private:
LineData *pointerHead;
LineData *pointerToTail;
int numObjects;
public:
Line();
~Line();
void enterLine(int ID, char *data);
void enterLine(LineData *lindat);
LineData *exitLine();
int count();
};
Line::Line() {
pointerHead = NULL;
pointerToTail = NULL;
numObjects = 0;
/*pointerHead = NULL;
pointerToTail = NULL;*/
}
/** Puts an item into the line. **/
void Line::enterLine(int ID, char *data) {
LineData *temp;
cout << "Inner sanctums called.n";
temp = new LineData;
temp->ID = ID;
strcpy(temp->name, data);
temp->next = NULL;
cout << "Temp created.nn";
//cout << "pointerHead is... ... " << pointerHead << "!n"; Not going to work!
/* Insert into the Line */
if(pointerHead != 0) { /* Insert as first in this Line/You shall not pass! Program will not let you pass! BOOM! Crash! */
cout << "If tried.n";
pointerToTail->next = temp;
pointerToTail = temp;
}
else /* Insert at Tail of the Line */
{
cout << "pointerHead == NULL. What are you going to do about it?n";
cout << temp->ID << "n";
pointerHead = temp;
pointerToTail = temp;
cout << "Inserted into Line!n";
}
}
/** ......
......
..........
..........
...... **/
正常编译。
int main() {
Line *GeorgiaCyclone;
Line *GiletteIce; // Shaved Ice.
Line *WoodenMagic;
Line *SteelCityNinja;
Line *Egbe; // Hawk.
GeorgiaCyclone->enterLine(4, "Preston");
Egbe->enterLine(2, "Felix");
return 181;
}
每一行的地址在创建时被设置为NULL。使用this
没有什么区别。怎么会发生这种事呢?如何缓解这场危机?
Line *GeorgiaCyclone;
创建一个指向Line
的指针,但没有创建一个Line
供它指向。您需要为它分配一个Line
的地址:
Line *GeorgiaCyclone = new Line
更好的是,您可以在main()
:
Line GeorgiaCyclone;
同样适用于您在main()
中声明的所有其他Line*
。
未初始化的非静态局部变量具有不确定的值,并且实际上看起来是随机的,并且很少等于空指针。
对未初始化的指针解引用,或者实际从中读取任何未初始化的变量,会导致未定义行为。
我所说的变量当然是你的main
函数中的变量。
在c++中,你不应该使用
NULL
作为空指针;使用nullptr
(首选,但仅适用于c++ 11和更高功能的编译器)或0
。在c++中,NULL
是一个宏,它只扩展到0
。
你应该总是初始化指针,否则行为是未定义的…如果你想让指针为NULL,你必须将它初始化为NULL。然而,解引用NULL指针仍然是未定义的行为,但至少您可以检查它是否为NULL。养成初始化所有东西的习惯是有益的。
Line *GeorgiaCyclone = NULL;
if(GeorgiaCyclone!=NULL)GeorgiaCyclone->enterLine(4, "Preston");
else{//some code that alerts you to the fact that you have null pointers, perhaps a message box?
}
如果你忘记分配一个指针,你的程序将不会崩溃。
相关文章:
- 为什么引用传递会导致此崩溃(C++)
- 对 const 引用参数使用默认值会导致崩溃
- Android (ART) 崩溃并显示错误 JNI 在应用程序中检测到错误:jstring 是无效的本地引用
- 取消引用会导致崩溃,即使对于非 NULL 指针也是如此,原因也是如此
- 为什么通过引用传递从 std::function 派生的对象会使程序崩溃?
- 当通过结构中的指针引用COM_PTR的向量时,向量大小未知时崩溃
- 为什么会崩溃?对函数中静态变量的c++引用无效
- 使用引用的项目时 C# 项目崩溃C++"System.IO.FileNotFoundException"
- C++:在重载 ostream >> 时引用 2D 数组元素时崩溃
- 类成员作为对另一个的引用:赋值运算符中的崩溃
- 子弹物理由于无效引用而崩溃
- 内核崩溃-无法处理000002c0处的内核NULL指针取消引用
- 我无法通过类 Artist 的公共方法修改任何私有字段。我尝试在函数名称之前添加引用(&),但崩溃了
- "Magic static" 在另一个翻译单元的静态销毁阶段引用时单例崩溃
- 通过不返回引用来防止下标操作符的赋值会导致运行时崩溃
- 为什么在使用空引用参数时崩溃,而不是在初始化参数时崩溃?
- c++程序在读取NULL引用时崩溃
- 访问对字符串的引用时崩溃
- Windows内CreateThread类函数调用,指针引用崩溃
- 取消引用 NULL 指针是否保证在 C/C++ 中使程序崩溃