c++编程错误-运行时错误
C++ programming errror - Run time error
在执行下面的c++代码时出现了运行时错误。有人能帮我解释一下为什么我得到这个运行时错误吗?
#include<iostream>
using namespace std;
struct node
{
int data;
struct node *left;
struct node *right;
struct node *rlink;
};
struct qnode
{
struct node *lnode;
int level;
};
struct node* newnode (int n)
{
struct node *temp;
temp= new struct node;
temp->data=n;
temp->left=NULL;
temp->right=NULL;
temp->rlink=NULL;
return temp;
}
void link (struct node *n, int level)
{
struct qnode *qn;
struct qnode *current, *next;
struct qnode nextd;
next = &nextd;
(next->lnode)->data=5;
cout << (next->lnode)->data << endl;
}
int main()
{
struct node *root = newnode(10);
root->left = newnode(8);
root->right = newnode(2);
root->left->left = newnode(3);
link (root, 0);
return 0;
}
在您的link函数中,您这样做了:
next = &nextd;
nextd未初始化,并且在将其地址分配给next时包含垃圾值。接下来的行尝试访问它,这会导致访问冲突错误。
问题在link:
void link (struct node *n, int level)
{
struct qnode *qn;
struct qnode *current, *next;
struct qnode nextd;
// BUG 1 = nextd is uninitialized
// BUG 2 - nextd is a stack-based variable, becomes junk when function exits
next = &nextd;
// Becuase nextd is uninitialized, next->lnode points to garbage. Kaboom!
(next->lnode)->data=5;
cout << (next->lnode)->data << endl;
}
相关文章:
- Antlr cpp 运行时 任何错误的指针转换?
- swscanf_s - 运行时检查错误
- C++代码编译,但在 Zorin OS 上运行时显示错误
- 多线程中的 std::basicstring 的运行时分段错误C++点燃进程
- RPC:端口映射运行时超时错误
- 如何使用运行时检查错误来修复我的C 格斗游戏
- 特征 3:运行时"EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE"错误?
- 挂钩发送/WSASEND返回运行时检查错误#0
- 通过 occi 连接 oracle db 的 MFC 应用程序.但它在 64 位机器中运行时0xC000007b错误
- c ++,Visual Studio express得到奇怪的运行时前错误
- 为什么这段代码会出现运行时分段错误
- visualstudio2008解决方案发布版本运行时致命错误
- 运行时字符串错误C++
- 到达主函数结束后发生调试运行时堆栈错误
- 将代码C++到 MIPS 中,MIPS 中的代码运行时没有错误消息,但不显示结果
- 以下代码中存在运行时-sigsegv错误
- 带有代码:的线程在运行时阻止错误
- 运行时断言错误
- 在不同的操作系统中运行时出现错误
- {Qt5.0.2/QML/QtQuick2.0/ c++}示例项目运行时没有错误