指针 null 检查的计算结果为 true,即使它没有被分配 null 值
Pointer null check evaluates to true even though it is not assigned a null value
这是创建二叉树的代码,运行完美。我不明白的是,在我看来,insert(string)
函数中 if()
语句的 temp->left==NULL
或 temp->right==NULL
条件如何被评估为 TRUE
,它应该抛出分段错误,因为我没有将节点的 left
或 right
指针初始化为 NULL
.
#include<iostream>
using namespace std;
class node
{
public:
string data;
node *left,*right;
};
class btree
{
private:
node *root;
public:
btree()
{
root=NULL;
}
void create();
void insert(string);
};
void btree:: create()
{
cout<<"\n\nEnter the no. of nodes you want to create: ";
int n;
cin>>n;
cin.ignore();
for(int i=0;i<n;i++)
{
cout<<"\nEnter data for node "<<i+1<<": ";
string input;
getline(cin,input);
insert(input);
}
}
void btree:: insert(string str)
{
if(root==NULL)
{
root=new node;
root->data=str;
return;
}
node *temp=root;
while(temp!=NULL)
{
cout<<"\n\nDo you want to enter the node in the left subtree or the right subtree of "<<temp->data<<"?(l/r): ";
char dir;
cin>>dir;
cin.ignore();
if(dir=='l')
{
if(temp->left==NULL)
{
temp->left=new node;
temp->left->data=str;
return;
}
else
temp=temp->left;
}
if(dir=='r')
{
if(temp->right==NULL)
{
temp->right=new node;
temp->right->data=str;
return;
}
else
temp=temp->right;
}
}
}
int main()
{
btree bt;
bt.create();
return 0;
}
这是输出:
Enter the no. of nodes you want to create: 3
Enter data for node 1: 5
Enter data for node 2: 1
Do you want to enter the node in the left subtree or the right subtree of 5?(l/r): l
Enter data for node 3: 9
Do you want to enter the node in the left subtree or the right subtree of 5?(l/r): r
要检查这是否确实只是 UB 或 new
运算符的效果,我 运行 测试代码:
#include<iostream>
using namespace std;
class node
{
public:
int data;
node *left,*right;
};
int main()
{
node *root=new node;
root=NULL;
cout<<"\nroot = "<<root; //This cout is executed since root has been initialized to NULL
std::cout.flush();
cout<<"\nroot->left = "<<root->left; //This line immediately throws segmentation fault
std::cout.flush();
return 0;
}
测试代码的输出:
root = 0
Segmentation fault (core dumped)
所以测试代码似乎可以识别 UB 并抛出段错误。为什么不是第一个代码?
我尝试搜索类似的问题,但没有找到。
您应该显式将left
和right
初始化为NULL
、0
或nullptr
(最好 nullptr
)。正如其他评论者所提到的,当指针未初始化时,很可能会遇到未定义的行为。
最佳实践(恕我直言):
class node
{
public:
string data;
node *left = nullptr;
node *right = nullptr;
};
这是创建二叉树的代码,运行完美。我不明白的是,在我看来,insert(string)
函数中 if()
语句的 temp->left==NULL
或 temp->right==NULL
条件如何被评估为 TRUE
,它应该抛出分段错误,因为我没有将节点的 left
或 right
指针初始化为 NULL
.
#include<iostream>
using namespace std;
class node
{
public:
string data;
node *left,*right;
};
class btree
{
private:
node *root;
public:
btree()
{
root=NULL;
}
void create();
void insert(string);
};
void btree:: create()
{
cout<<"\n\nEnter the no. of nodes you want to create: ";
int n;
cin>>n;
cin.ignore();
for(int i=0;i<n;i++)
{
cout<<"\nEnter data for node "<<i+1<<": ";
string input;
getline(cin,input);
insert(input);
}
}
void btree:: insert(string str)
{
if(root==NULL)
{
root=new node;
root->data=str;
return;
}
node *temp=root;
while(temp!=NULL)
{
cout<<"\n\nDo you want to enter the node in the left subtree or the right subtree of "<<temp->data<<"?(l/r): ";
char dir;
cin>>dir;
cin.ignore();
if(dir=='l')
{
if(temp->left==NULL)
{
temp->left=new node;
temp->left->data=str;
return;
}
else
temp=temp->left;
}
if(dir=='r')
{
if(temp->right==NULL)
{
temp->right=new node;
temp->right->data=str;
return;
}
else
temp=temp->right;
}
}
}
int main()
{
btree bt;
bt.create();
return 0;
}
这是输出:
Enter the no. of nodes you want to create: 3
Enter data for node 1: 5
Enter data for node 2: 1
Do you want to enter the node in the left subtree or the right subtree of 5?(l/r): l
Enter data for node 3: 9
Do you want to enter the node in the left subtree or the right subtree of 5?(l/r): r
要检查这是否确实只是 UB 或 new
运算符的效果,我 运行 测试代码:
#include<iostream>
using namespace std;
class node
{
public:
int data;
node *left,*right;
};
int main()
{
node *root=new node;
root=NULL;
cout<<"\nroot = "<<root; //This cout is executed since root has been initialized to NULL
std::cout.flush();
cout<<"\nroot->left = "<<root->left; //This line immediately throws segmentation fault
std::cout.flush();
return 0;
}
测试代码的输出:
root = 0
Segmentation fault (core dumped)
所以测试代码似乎可以识别 UB 并抛出段错误。为什么不是第一个代码?
我尝试搜索类似的问题,但没有找到。
您应该显式将left
和right
初始化为NULL
、0
或nullptr
(最好 nullptr
)。正如其他评论者所提到的,当指针未初始化时,很可能会遇到未定义的行为。
最佳实践(恕我直言):
class node
{
public:
string data;
node *left = nullptr;
node *right = nullptr;
};