Best structure for a "data buffer".

Discussion in 'C++' started by Barzo, Aug 25, 2009.

  1. Barzo

    Barzo Guest

    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.
     
    Barzo, Aug 25, 2009
    #1
    1. Advertising

  2. 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
     
    Victor Bazarov, Aug 25, 2009
    #2
    1. Advertising

  3. Barzo

    mzdude Guest

    On Aug 25, 12:41 pm, Victor Bazarov <> 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 );
     
    mzdude, Aug 25, 2009
    #3
  4. mzdude wrote:
    > On Aug 25, 12:41 pm, Victor Bazarov <> 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
     
    Victor Bazarov, Aug 25, 2009
    #4
  5. Barzo

    Barzo Guest

    On 25 Ago, 19:18, Victor Bazarov <> 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.
     
    Barzo, Aug 26, 2009
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Excluded_Middle

    Pointers to structure and array of structure.

    Excluded_Middle, Oct 24, 2004, in forum: C Programming
    Replies:
    4
    Views:
    791
    Martin Ambuhl
    Oct 26, 2004
  2. Leo Nunez
    Replies:
    3
    Views:
    1,292
    Neil Kurzman
    Feb 9, 2005
  3. Replies:
    2
    Views:
    641
  4. Replies:
    9
    Views:
    25,536
    Lal Bahadur Singh
    Nov 11, 2011
  5. meisterbartsch
    Replies:
    2
    Views:
    807
    meisterbartsch
    Jun 12, 2007
Loading...

Share This Page