nullptr和nullptr_t有什么区别(深度理解)

nullptrnullptr_t 是 C++11 引入的两个概念,它们之间有一些重要的区别。

nullptr

nullptr 是一个关键字,用来表示空指针常量。在 C++11 之前,NULL 被用来表示空指针,但由于 NULL 通常被定义为整数 0,可能会引发一些类型相关的问题。例如:

void f(int);
void f(char*);

f(NULL);  // 调用 f(int),而不是 f(char*),可能会产生歧义

为了解决这个问题,C++11 引入了 nullptrnullptr 是一种特殊的常量,表示空指针,其类型是 std::nullptr_t。使用 nullptr 可以避免上述类型歧义的问题:

void f(int);
void f(char*);

f(nullptr);  // 明确调用 f(char*)

nullptr_t

nullptr_t 是表示 nullptr 的类型的类型名。它是一个特殊的类型,可以用来声明和定义接受或返回 nullptr 的变量或函数。例如:

std::nullptr_t nptr;  // 声明一个 nullptr_t 类型的变量
nptr = nullptr;  // 赋值 nullptr 给这个变量

std::nullptr_t getNull() {
    return nullptr;
}

区别总结

  • nullptr 是一个表示空指针的关键字,其类型是 std::nullptr_t
  • std::nullptr_tnullptr 的类型。
    #include <iostream>
    #include <type_traits>
    
    void f(int) {
        std::cout << "f(int)" << std::endl;
    }
    
    void f(char*) {
        std::cout << "f(char*)" << std::endl;
    }
    
    int main() {
        std::nullptr_t nptr = nullptr;  // nullptr_t 类型的变量
        f(nullptr);  // 调用 f(char*)
        f(nptr);  // 也是调用 f(char*)
        
        // 输出类型信息
        std::cout << "Type of nullptr: " << typeid(nullptr).name() << std::endl;
        std::cout << "Type of nullptr_t: " << typeid(std::nullptr_t).name() << std::endl;
    
        return 0;
    }
    

    运行上述代码将输出:

    f(char*)
    f(char*)
    Type of nullptr: nullptr_t
    Type of nullptr_t: nullptr_t
    

    从输出中可以看出,nullptr 的类型是 std::nullptr_t

int main(int argc, char** argv) { cudaSetDevice(DEVICE); std::string wts_name = ""; std::string engine_name = ""; bool is_p6 = false; float gd = 0.0f, gw = 0.0f; std::string img_dir; if (!parse_args(argc, argv, wts_name, engine_name, is_p6, gd, gw, img_dir)) { std::cerr << "arguments not right!" << std::endl; std::cerr << "./yolov5_det -s [.wts] [.engine] [n/s/m/l/x/n6/s6/m6/l6/x6 or c/c6 gd gw] // serialize model to plan file" << std::endl; std::cerr << "./yolov5_det -d [.engine] ../samples // deserialize plan file and run inference" << std::endl; return -1; } // create a model using the API directly and serialize it to a stream if (!wts_name.empty()) { IHostMemory* modelStream{ nullptr }; APIToModel(BATCH_SIZE, &modelStream, is_p6, gd, gw, wts_name); assert(modelStream != nullptr); std::ofstream p(engine_name, std::ios::binary); if (!p) { std::cerr << "could not open plan output file" << std::endl; return -1; } p.write(reinterpret_cast<const char*>(modelStream->data()), modelStream->size()); modelStream->destroy(); return 0; } // deserialize the .engine and run inference std::ifstream file(engine_name, std::ios::binary); if (!file.good()) { std::cerr << "read " << engine_name << " error!" << std::endl; return -1; } char *trtModelStream = nullptr; size_t size = 0; file.seekg(0, file.end); size = file.tellg(); file.seekg(0, file.beg); trtModelStream = new char[size]; assert(trtModelStream); file.read(trtModelStream, size); file.close(); std::vector<std::string> file_names; // if (read_files_in_dir(img_dir.c_str(), file_names) < 0) { // std::cerr << "read_files_in_dir failed." << std::endl; // return -1; // } static float prob[BATCH_SIZE * OUTPUT_SIZE]; IRuntime* runtime = createInferRuntime(gLogger); assert(runtime != nullptr); ICudaEngine* engine = runtime->deserializeCudaEngine(trtModelStream, size); assert(engine != nullptr); IExecutionContext* context = engine->createExecutionContext(); assert(context != nullptr); delete[] trtModelStream; assert(engine->getNbBindings() == 2); float* buffers[2]; // In order to bind the buffers, we need to know the names of the input and output tensors. // Note that indices are guaranteed to be less than IEngine::getNbBindings() const int inputIndex = engine->getBindingIndex(INPUT_BLOB_NAME); const int outputIndex = engine->getBindingIndex(OUTPUT_BLOB_NAME); assert(inputIndex == 0); assert(outputIndex == 1); // Create GPU buffers on device CUDA_CHECK(cudaMalloc((void**)&buffers[inputIndex], BATCH_SIZE * 3 * INPUT_H * INPUT_W * sizeof(float))); CUDA_CHECK(cudaMalloc((void**)&buffers[outputIndex], BATCH_SIZE * OUTPUT_SIZE * sizeof(float))); // Create stream cudaStream_t stream; CUDA_CHECK(cudaStreamCreate(&stream)); uint8_t* img_host = nullptr; uint8_t* img_device = nullptr; // prepare input data cache in pinned memory CUDA_CHECK(cudaMallocHost((void**)&img_host, MAX_IMAGE_INPUT_SIZE_THRESH * 3));你现在是深度学习c++工程师,目标是修改一段代码使得这段代码可以直接检测视频,限制是不要啰嗦,代码尽量简单易懂
最新发布
03-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

理工科小理

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值