Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Best structure for a "data buffer".

Reply
Thread Tools

Best structure for a "data buffer".

 
 
Barzo
Guest
Posts: n/a
 
      08-25-2009
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.
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      08-25-2009
Barzo wrote:
> 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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
 
 
 
mzdude
Guest
Posts: n/a
 
      08-25-2009
On Aug 25, 12:41*pm, Victor Bazarov <(E-Mail Removed)> wrote:
> Barzo wrote:
> > 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 );

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      08-25-2009
mzdude wrote:
> On Aug 25, 12:41 pm, Victor Bazarov <(E-Mail Removed)> wrote:
>> Barzo wrote:
>>> 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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
Barzo
Guest
Posts: n/a
 
      08-26-2009
On 25 Ago, 19:18, Victor Bazarov <(E-Mail Removed)> wrote:
> mzdude wrote:
>
> > 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference between c structure and c++ structure raghunandan_1081@yahoo.com C++ 9 11-11-2011 07:34 AM
How to read a Structure from a matlab file to a structure in Vc++ 2003 meisterbartsch C++ 2 06-12-2007 08:47 PM
Memory allocation in Structure to Structure pra_ramli@rediffmail.com C++ 2 03-09-2006 05:51 AM
Copy String structure "A" to string structure "B" Leo Nunez C Programming 3 02-09-2005 05:14 AM
Pointers to structure and array of structure. Excluded_Middle C Programming 4 10-26-2004 05:39 AM



Advertisments