为什么我们要检查 temp == 是否为空?
Why are we checking if temp == null?
此代码用于实现链表。
node *single_llist::create_node(int value)
{
struct node *temp, *s;
temp = new(struct node);
if (temp == NULL)
{
cout<<"Memory not allocated "<<endl;
return 0;
}
else
{
temp->info = value;
temp->next = NULL;
return temp;
}
}
在这里,为什么我们要检查 temp == NULL。我想不出任何可能发生这种情况的情况
同样要退出 if,为什么我们返回 0,因为返回类型是 node?
- 正如消息中明确指出的那样,这是为了防止分配内存的请求失败。 (确切地说,这种情况是如何发生的是无关紧要的;这是可能的,所以代码应该处理它。
- 作者假设
NULL==0
,这通常是正确的,但不一定如此,并且(正如我们似乎都认为的那样(是一个糟糕的假设。
相关文章:
- 在提升multi_index容器中,是否定义了"default index"?
- 在C++STL中是否有Polyval(Matlab函数)等价物?
- 检查输入是否不是整数或数字
- 是否可以初始化不可复制类型的成员变量(或基类)
- 在C++中,是否可以基于给定的标识符创建基类的新实例,反之亦然
- 是否可以通过C++扩展强制多个python进程共享同一内存
- 此代码是否违反一个定义规则
- 是否需要删除包含对象的"pair"?
- 是否可以从int转换为enum类类型
- 无论条件是否为true,if总是在c++中执行
- 如何找到大小'x'数组是否完全填充,在C++?
- 检查值是否在集合p1和p2中,但不在p3中
- 是否可以在编译时初始化数组,以便在运行时不会花费时间?
- 检查 std::shared_ptr<> 的当前底层类型是否为 T
- 在c++中检查长方体是否尽可能快地重叠(无迭代)
- GL_SHADERSTORAGE_BUFFER位置是否与其他着色器位置冲突
- 子目录是否继承属性,例如add_definitions,include_directories和父Cmakelist.t
- 标准是否使用多余的大括号(例如 T{{{10}}})定义列表初始化?
- 为什么我们要检查 temp == 是否为空?
- 将文件存储在TEMP目录中是否足够安全?