Best structure for a "data buffer".

B

Barzo

Ok, the question for sure will be stupid and the answer should be
simply: "it depends" but, anyway, I want to ask.

I have some data readed from binary files (In my case are audio file)
and I have to store in a temp "buffer" the data readed with a function
like:

File->ReadFileChunk(T buffer_ptr*, const int items_to_read);

I'm thinking to use a structure like an std::vector<T> because I think
it is more simple to manage instead of a simple T buffer[] array;

But, is there a way to store a T* buffer into a vector (and the
opposite) in only-one "copy" operation?

Thanks for any suggestions.
Daniele.
 
V

Victor Bazarov

Barzo said:
Ok, the question for sure will be stupid and the answer should be
simply: "it depends" but, anyway, I want to ask.

I have some data readed from binary files (In my case are audio file)
and I have to store in a temp "buffer" the data readed with a function
like:

File->ReadFileChunk(T buffer_ptr*, const int items_to_read);

I'm thinking to use a structure like an std::vector<T> because I think
it is more simple to manage instead of a simple T buffer[] array;

But, is there a way to store a T* buffer into a vector (and the
opposite) in only-one "copy" operation?

// given that 'buffer' is
T *buffer;
...
// and you know what the number of elements in it is
int items_read = ... ;

// then you can construct a vector of those like so
std::vector<T> my_vector(buffer, buffer + items_read);

V
 
M

mzdude

Barzo said:
I have some data readed from binary files (In my case are audio file)
and I have to store in a temp "buffer" the data readed with a function
like:
File->ReadFileChunk(T buffer_ptr*, const int items_to_read);
I'm thinking to use a structure like an std::vector<T> because I think
it is more simple to manage instead of a simple T buffer[] array;
But, is there a way to store a T* buffer into a vector (and the
opposite) in only-one "copy" operation?

     // given that 'buffer' is
     T *buffer;
     ...
     // and you know what the number of elements in it is
     int items_read = ... ;

     // then you can construct a vector of those like so
     std::vector<T> my_vector(buffer, buffer + items_read);
Why not read directly into the vector?
std::vector<T> myVector(items_to_read); // Create space
File->ReadChunk( &myVector[0], items_to_read );
 
V

Victor Bazarov

mzdude said:
Barzo said:
I have some data readed from binary files (In my case are audio file)
and I have to store in a temp "buffer" the data readed with a function
like:
File->ReadFileChunk(T buffer_ptr*, const int items_to_read);
I'm thinking to use a structure like an std::vector<T> because I think
it is more simple to manage instead of a simple T buffer[] array;
But, is there a way to store a T* buffer into a vector (and the
opposite) in only-one "copy" operation?
// given that 'buffer' is
T *buffer;
...
// and you know what the number of elements in it is
int items_read = ... ;

// then you can construct a vector of those like so
std::vector<T> my_vector(buffer, buffer + items_read);
Why not read directly into the vector?
std::vector<T> myVector(items_to_read); // Create space
File->ReadChunk( &myVector[0], items_to_read );

That's fine too, if 'T' has a default c-tor.

V
 
B

Barzo

mzdude said:
Why not read directly into the vector?
std::vector<T> myVector(items_to_read);  // Create space
File->ReadChunk( &myVector[0], items_to_read );

That's fine too, if 'T' has a default c-tor.


Thanks a lot.
This is exactly what I need.
I didn't thought that I could use a vector in this manner.

PS. Yes, T could be an int, short, float or double.

Daniele.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top