writing directly to std::string internal buffers

前端 未结 9 1154
猫巷女王i
猫巷女王i 2020-11-29 07:22

I was looking for a way to stuff some data into a string across a DLL boundary. Because we use different compilers, all our dll interfaces are simple char*.

Is ther

9条回答
  •  我在风中等你
    2020-11-29 07:41

    In C++98 you should not alter the buffers returned by string::c_str() and string::data(). Also, as explained in the other answers, you should not use the string::operator[] to get a pointer to a sequence of characters - it should only be used for single characters.

    Starting with C++11 the strings use contiguous memory, so you could use &string[0] to access the internal buffer.

提交回复
热议问题