In the Go language, there is a function func (* Buffer) Next, which takes from the Buffer a slice (array) of n elements.

276 // Next returns a slice containing the next n bytes from the buffer, 277 // advancing the buffer as if the bytes had been returned by Read. 278 // If there are fewer than n bytes in the buffer, Next returns the entire buffer. 279 // The slice is only valid until the next call to a read or write method. 280 func (b *Buffer) Next(n int) []byte { 281 b.lastRead = opInvalid 282 m := b.Len() 283 if n > m { 284 n = m 285 } 286 data := b.buf[b.off : b.off+n] 287 b.off += n 288 if n > 0 { 289 b.lastRead = opRead 290 } 291 return data 292 } 

How to implement a similar thing using Poco / FIFOBuffer.h

 #include "Poco/FIFOBuffer.h" #include "Poco/Buffer.h" using Poco::FIFOBuffer; using Poco::Buffer; void main() { char* qr = ""; std::string str = "test"; FIFOBuffer buf(12); buf.write(str.c_str(), 4); std::cout << buf[0]; // t buf.read(qr, 1); // прекращена работа программы // std::memcpy(pBuffer, _buffer.begin() + _begin, length * sizeof(T)); Вот здесь вылетает. } 

    1 answer 1

    The call to buf.read(qr, 1) also calls memcpy :

     std::memcpy(pBuffer, _buffer.begin() + _begin, length * sizeof(T)); 

    Here you are trying to write to a constant , which causes an error. Speaking in terms of C ++, as stated in the note to clause 16 of section 2.13.5 of the C ++ standard, the effect when trying to change a string literal is not defined:

    The string literal is undefi ned.

    Working example:

     #include "Poco/FIFOBuffer.h" #include "Poco/Buffer.h" #include <iostream> #include <string> int main() { std::string str = "Hello world"; char arr[8]={},arr2; Poco::FIFOBuffer buf(12); buf.write(str.data(),str.size()); buf.read(arr,7); std::cout<<arr<<"\n"; buf.read(&arr2,1); std::cout<<arr2<<"\n"; }