程序崩溃的原因
Why the program crashes?
此代码编译正确,但在启动时崩溃。你能告诉我为什么吗?
#include <iostream>
using namespace std;
int main()
{
struct test{
int a;
int b;
};
test* xyz;
xyz->a = 5;
cout<< xyz->a;
}
xyz只是一个指针,但它不指向任何东西。在使用其值之前,必须先实例化它。你基本上有两个选择:
-
通过在堆上创建一个新的测试对象来实例化xyz。
//generate new test object. xyz represents a pointer to an object of type test. test* xyz = new test(); //perform operations on xyz //deletes xyz from the heap delete xyz;
-
在堆栈上创建一个测试对象,而不使用指针语法。
//defines xyz as an object of class test (instead of a pointer to a test object). test xyz; //perform operations on xyz, no need to delete it this time
我鼓励您阅读更多关于C++中指针的内容。您可以从以下视频开始:C++中指针的介绍
祝你好运!
相关文章:
- 提升 ASIO Async_receive崩溃程序
- 类指针方法崩溃程序
- get_body来自 IHTMLDocument2 崩溃程序
- 是否有可能存在不会崩溃程序的内存问题
- 调试运行时出现奇怪的崩溃程序(Eclipse C++)
- 儿童对话框 - setWindowTexta或sendmessagea崩溃程序-MFC
- 顶点阵列GLFW崩溃C 程序
- 将值分配给Float数据类型崩溃程序
- OpenCV-将变量添加到类成员崩溃程序
- boost::interprocess::managed_shared_memory 崩溃程序
- 将枚举值分配给整数崩溃程序
- SDL 1.2 -> SDL 2.0(崩溃程序)
- 空析构函数崩溃程序:C++
- 冒泡排序崩溃程序c++
- Ofstream关闭崩溃程序
- 删除字符* 崩溃程序
- c++矢量擦除崩溃程序
- Lua_getglobal崩溃程序
- 通过Copy Constructor创建的e2打印崩溃程序
- RNG崩溃c++程序