无法理解此 return 语句的功能,没有它就会发生运行时错误
Not able to understand the function of this return statement, without it a runtime error occurs
在下面bstcreate()
函数的if
语句中,如果我删除return
语句,则会发生运行时错误。 为什么?
即使没有return
语句,进程也不应该转到while
语句,因为所有语句都会被执行吗?
下面是代码:
struct Node{
struct Node *lchild;
int data;
struct Node *rchild;
}*root=NULL;
void bstcreate(int key)
{
struct Node*t=root;
struct Node*p,*r=NULL;
if(root == NULL)
{
p = (struct Node*)malloc(sizeof(struct Node));
p->data = key;
p->lchild = p->rchild = NULL;
root = p;
return;
}
while(t)
{
r=t;
if(key<t->data)
{
t=t->lchild;
}
else
{
t=t->rchild;
}
}
p = (struct Node*)malloc(sizeof(struct Node));
p->data = key;
p->lchild = p->rchild = NULL;
if(r->data>key)
{
r->lchild=p;
}
else
{
r->rchild=p;
}
}
如果列表为空,bstcreate()
root
设置为新Node
,然后由于没有其他事情可做,因此应立即退出。return
语句执行该退出。 由于该函数被声明为返回void
,因此不需要提供return
的值。
t
初始化为root
,r
初始化为NULL
。 如果删除return
语句,并且在进入时root
NULL
,则最初t
NULL
导致跳过while
循环,然后在访问r
的成员时发生崩溃,因为r
仍然NULL
,因为循环未将r
分配给指向任何位置。
相关文章:
- 删除指向指针的指针是运行时错误吗
- c++中的指针和运行时错误
- 无法理解此 return 语句的功能,没有它就会发生运行时错误
- 在同一模拟中使用静脉和静脉_ inet内容时出现运行时错误
- 对单向链表进行排序时出现运行时错误
- 为什么此代码存在运行时错误?
- 你能解释一下什么运行时错误是如何解决它的吗?
- 为什么会出现 gettnig 运行时错误:加载类型为"_Bit_type"(stl_bvector.h) 的空指针?
- 为什么程序在 c++ 中迭代 emtpy 向量时会抛出运行时错误
- 运行时错误:引用绑定到类型为"int"的空指针
- 为什么当 vector 为空时会显示运行时错误?
- C++运行时错误与快速排序算法抛出堆栈转储错误
- 运行时错误:矢量下标超出范围:正在检查空集
- 分配给gslice_array会导致运行时错误
- cout 新创建的对象引发运行时错误
- C++在使用std::multimap时出现运行时错误的几率很小
- 运行时错误:引用绑定到类型"int"的未对齐地址0xbebebebebebebec6,这需要 4 个字节对齐 (stl_vector.h)
- 试图找出为什么我会收到运行时错误?
- 从成员变量更新类变量或调用类功能是给出运行时错误
- 自定义char*插入功能多次运行时会产生运行时错误