Proper Way To Initialize Unsigned Char*

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

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

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

<
7条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-04 06:47

    If the plan is for it to be a buffer and you want to move it later to point to something, then initialise it to NULL until it really points somewhere to which you want to write, not an empty string.

    unsigned char * tempBuffer = NULL;
    std::vector< unsigned char > realBuffer( 1024 );
    tempBuffer = &realBuffer[0]; // now it really points to writable memory
    memcpy( tempBuffer, someStuff, someSizeThatFits );
    

提交回复
热议问题