gpt4 book ai didi

c++ - 将 nullptr 传递给 C 函数是否安全?

转载 作者:行者123 更新时间:2023-12-04 12:23:16 25 4
gpt4 key购买 nike

我有一些用纯 C 编写的库。现在我正在创建一些单元测试,但测试库是用 C++ 而不是 C 编写的。当我将 NULL 传递给测试中的 API 函数时,CLion 提示我传递 nullptr 而不是 NULL。在这种情况下将 nullptr 传递给纯 C 函数是否安全?

最佳答案

Is it safe to pass nullptr to pure C function in this case?


我觉得是这样的。来自 the C++11 standard (4.10 Pointer conversions) :

A null pointer constant is an integral constant expression ([expr.const]) prvalue of integer type that evaluates to zero or a prvalue of type std::nullptr_t. A null pointer constant can be converted to a pointer type; the result is the null pointer value of that type and is distinguishable from every other value of object pointer or function pointer type.


一个 nullptr以及计算结果为 0 的积分常量表达式等价于此类转换。

关于c++ - 将 nullptr 传递给 C 函数是否安全?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65279075/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com