jdscardoso
jdscardoso

Reputation: 291

Struct keep values

I am trying to implement a struct to keep binary data. Just like this:

struct Buffer {
    int size_;
    int capacity = 1000000;
    int beg_index, end_index;
    char data_[1000000];
} buffer_audio[3];

And a function to write the binary data in the buffer:

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;
}

But the problem is.. Every time I run this routine the values of beg_index and end_index are deleted. And the memcpy will overwrite. The routine:

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

}

Upvotes: 0

Views: 64

Answers (1)

rozina
rozina

Reputation: 4232

int writing_bufer(Buffer buffers, const char *data, int nbytes)

should be

int writing_bufer(Buffer& buffers, const char *data, int nbytes)

You copied buffers into the function and filled the local buffers and then destroyed them.

Upvotes: 6

Related Questions