How to get the address of the std::vector buffer start most elegantly?

后端 未结 11 2217
-上瘾入骨i
-上瘾入骨i 2020-12-03 18:12

I want to use std::vector for dynamically allocating memory. The scenario is:

int neededLength = computeLength(); // some logic here

// this will allocate t         


        
11条回答
  •  醉话见心
    2020-12-03 18:41

    It's really odd that nobody know this!!! in C++11 you could use:

    buffer.data()
    

    it could get the address of the vector I have test it:

    vectorbuffer;
    buffer.push_back('w');
    buffer.push_back('h');
    buffer.push_back('a');
    buffer.push_back('t');
    buffer.push_back('\0');
    char buf2[10];
    memcpy(buf2,buffer.data(),10);
    

    Specification here.

提交回复
热议问题