Proper Way To Initialize Unsigned Char*

前端 未结 7 2027
渐次进展
渐次进展 2021-02-04 06:03

What is the proper way to initialize unsigned char*? I am currently doing this:

unsigned char* tempBuffer;
tempBuffer = \"\";

<
7条回答
  •  盖世英雄少女心
    2021-02-04 06:47

    To "properly" initialize a pointer (unsigned char * as in your example), you need to do just a simple

    unsigned char *tempBuffer = NULL;
    

    If you want to initialize an array of unsigned chars, you can do either of following things:

    unsigned char *tempBuffer = new unsigned char[1024]();
    // and do not forget to delete it later
    delete[] tempBuffer;
    

    or

    unsigned char tempBuffer[1024] = {};
    

    I would also recommend to take a look at std::vector, which you can initialize like this:

    std::vector tempBuffer(1024, 0);
    

提交回复
热议问题