Proper Way To Initialize Unsigned Char*

前端 未结 7 2029
渐次进展
渐次进展 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

    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 );
    
    0 讨论(0)
提交回复
热议问题