我正在尝试实现一种结构以保留二进制数据。像这样:
struct Buffer {
int size_;
int capacity = 1000000;
int beg_index, end_index;
char data_[1000000];
} buffer_audio[3];
还有一个将二进制数据写入缓冲区的函数:
int writing_bufer(Buffer buffers, const char *data, int nbytes) {
if (nbytes == 0) return 0;
int capacity = buffers.capacity;
int bytes_to_write = std::min(nbytes, capacity - buffers.size_);
if (bytes_to_write <= capacity - buffers.end_index)
{
memcpy(buffers.data_ + buffers.end_index, data, bytes_to_write);
buffers.end_index += bytes_to_write;
if (buffers.end_index == capacity) buffers.end_index = 0;
}
else { return 0; }
buffers.size_ += bytes_to_write;
return bytes_to_write;
}
但是问题是..每次我运行此例程时,都会删除
beg_index
和end_index
的值。并且memcpy将覆盖。例程:
void buffering_mem(char* chunk,int size_chunk, int close_file, int client, int total_size){
int check_bytes = writing_bufer(buffer_audio[client], chunk, size_chunk);
//other code
}
最佳答案
int writing_bufer(Buffer buffers, const char *data, int nbytes)
应该
int writing_bufer(Buffer& buffers, const char *data, int nbytes)
您将缓冲区复制到函数中,并填充了本地缓冲区,然后销毁了它们。
关于c++ - 保持值(value),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32584298/