dynamically allocated array and vector

Discussion in 'C++' started by subramanian100in@yahoo.com, India, Apr 12, 2010.

  1. , India

    , India Guest

    We have std::vector<T> container for an element type 'T' that
    satisfies the element requirements - like Copy Constructible,
    Assignable. We also have dynamically allocated arrays. When will we
    use std::vector and when will we use dynamically allocated arrays ?
    Kindly explain, if possible with sample program.

    Thanks
    V.Subramanian
     
    , India, Apr 12, 2010
    #1
    1. Advertising

  2. On Apr 11, 9:33 pm, ", India"
    <> wrote:
    > We have std::vector<T> container for an element type 'T' that
    > satisfies the element requirements - like Copy Constructible,
    > Assignable. We also have dynamically allocated arrays. When will we
    > use std::vector and when will we use dynamically allocated arrays ?
    > Kindly explain, if possible with sample program.
    >


    Most suggest to prefer vector over arrays whose sizes are specified at
    compile time. There is no reason to use dynamically allocated arrays
    over vector. Using new [] or malloc is way more tedious.

    - Anand
     
    Anand Hariharan, Apr 12, 2010
    #2
    1. Advertising

  3. * , India:
    > We have std::vector<T> container for an element type 'T' that
    > satisfies the element requirements - like Copy Constructible,
    > Assignable. We also have dynamically allocated arrays. When will we
    > use std::vector and when will we use dynamically allocated arrays ?
    > Kindly explain, if possible with sample program.


    Generally a std::vector is preferable to a directly dynamically allocated array,
    since std::vector takes care of deallocation automatically and has much useful
    funtionality, and is just as efficient.

    However, in some cases you need to use library code that takes ownership of an
    array and deallocates it in some special way (e.g. by using 'free').

    In such cases you need to allocate the array directly, using the form of
    allocation matching the expected deallocation (e.g., using 'malloc').

    Another reason to allocate an array yourself might be for optimization purposes,
    e.g. sub-allocating from some larger block that is deallocated in one operation.
    std::vector supports also such custom allocation. But it might be simpler to
    just do it yourself than to cajole std::vector into doing it.

    There might also be other reasons to go the DIY route, but offhand I can't think
    of any.


    Cheers & hth.,

    - Alf
     
    Alf P. Steinbach, Apr 12, 2010
    #3
  4. , India

    peter koch Guest

    On 12 Apr., 04:33, ", India"
    <> wrote:
    > We have std::vector<T> container for an element type 'T' that
    > satisfies the element requirements - like Copy Constructible,
    > Assignable. We also have dynamically allocated arrays. When will we
    > use std::vector and when will we use dynamically allocated arrays ?
    > Kindly explain, if possible with sample program.
    >


    If you want a dynamic size, you will probably always want to use
    std::vector. Dynamic allocation of arrays is there primarily because
    std:vector was not available in C++ before templates were available.
    If you need a fixed size array, you might need something like
    boost::array. Otherwise, I would still recommend std::vector.
    Alf Steinbach mentioned a few rare cases where you just have to use
    array new. I believe those cases are quite rare, and I myself have
    never needed to use array new.

    /Peter
     
    peter koch, Apr 12, 2010
    #4
    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. Jeff Williams

    Dynamically allocated array question

    Jeff Williams, Aug 18, 2003, in forum: C++
    Replies:
    6
    Views:
    520
    Alf P. Steinbach
    Aug 19, 2003
  2. Replies:
    5
    Views:
    667
    Matt Wharton
    Dec 9, 2004
  3. Replies:
    6
    Views:
    4,089
    Dave Thompson
    Feb 7, 2005
  4. Replies:
    2
    Views:
    311
    Rolf Magnus
    Jun 14, 2005
  5. Replies:
    8
    Views:
    1,999
    Csaba
    Feb 18, 2006
Loading...

Share This Page