about std::vector < int > - memory allocation

Discussion in 'C++' started by Rakesh Sinha, Jan 16, 2005.

  1. Rakesh Sinha

    Rakesh Sinha Guest

    This is about the vector template defined in standard C++ .

    Suppose I want to create a *huge* vector , as follows.


    void f1() {
    vector < int > data(1024); //may be not very huge, but for
    //demonstration purposes

    //Process 'data'


    //'data' goes out of scope
    //and storage reclaimed automatically
    }

    void f2() {
    vector < int > * p = new vector <int>f1(1024); //

    //Process 'p'

    delete p; //Explicitly reclaim storage.
    }


    My question is, given that we want to create a huge vector , which one
    among the two is better ?

    * If this had been not been a container, then I would
    have chosen 'f1()' because it does not involve pointers.

    * If this had been not been a container but a *big object*,
    may be 'f2()' might be better.

    * Given that vector manages dynamic memory allocation internally, are
    there any important differences between 'f1()' and 'f2()' at all ?
    I am little bit confused here. Please let me know your comments.
     
    Rakesh Sinha, Jan 16, 2005
    #1
    1. Advertising

  2. * Rakesh Sinha:
    > This is about the vector template defined in standard C++ .
    >
    > Suppose I want to create a *huge* vector , as follows.
    >
    >
    > void f1() {
    > vector < int > data(1024); //may be not very huge, but for
    > //demonstration purposes
    >
    > //Process 'data'
    >
    >
    > //'data' goes out of scope
    > //and storage reclaimed automatically
    > }
    >
    > void f2() {
    > vector < int > * p = new vector <int>f1(1024); //
    >
    > //Process 'p'
    >
    > delete p; //Explicitly reclaim storage.
    > }
    >
    >
    > My question is, given that we want to create a huge vector , which one
    > among the two is better ?
    >
    > * If this had been not been a container, then I would
    > have chosen 'f1()' because it does not involve pointers.
    >
    > * If this had been not been a container but a *big object*,
    > may be 'f2()' might be better.
    >
    > * Given that vector manages dynamic memory allocation internally, are
    > there any important differences between 'f1()' and 'f2()' at all ?
    > I am little bit confused here. Please let me know your comments.


    f2 is less efficient, not exception safe, and a notational nightmare.

    Is this a homework question, or have you actually tried it (it should
    be impossible to be unaware of the drawbacks if you have tried it)?

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Jan 16, 2005
    #2
    1. Advertising

  3. Rakesh Sinha

    Rakesh Sinha Guest

    Alf P. Steinbach wrote:
    >
    > f2 is less efficient, not exception safe, and a notational nightmare.
    >
    > Is this a homework question,


    No - this is not.

    > or have you actually tried it (it should
    > be impossible to be unaware of the drawbacks if you have tried it)?


    Indeed I had tried it, but it was just a co-incidence that I did not
    get any exception thrown between new and delete . Thanks for letting me
    the information though.
     
    Rakesh Sinha, Jan 16, 2005
    #3
  4. Rakesh Sinha

    red floyd Guest

    Rakesh Sinha wrote:
    > This is about the vector template defined in standard C++ .
    >
    > Suppose I want to create a *huge* vector , as follows.
    >
    >
    > void f1() {
    > vector < int > data(1024); //may be not very huge, but for
    > //demonstration purposes
    >
    > //Process 'data'
    >
    >
    > //'data' goes out of scope
    > //and storage reclaimed automatically
    > }
    >
    > void f2() {
    > vector < int > * p = new vector <int>f1(1024); //
    >
    > //Process 'p'
    >
    > delete p; //Explicitly reclaim storage.
    > }
    >
    >
    > My question is, given that we want to create a huge vector , which one
    > among the two is better ?
    >
    > * If this had been not been a container, then I would
    > have chosen 'f1()' because it does not involve pointers.
    >
    > * If this had been not been a container but a *big object*,
    > may be 'f2()' might be better.
    >
    > * Given that vector manages dynamic memory allocation internally, are
    > there any important differences between 'f1()' and 'f2()' at all ?
    > I am little bit confused here. Please let me know your comments.
    >


    Since vector<> generally dynamically allocates its buffer anyway, what
    you get with the first is simply the bookkeeping portion of the vector
    allocated in automatic storage, and you get the automatic deletion when
    it goes out of scope.

    The second form is (as was noted) a notational nightmare, and you've got
    to keep track of the pointer, and delete it -- at this point, you might
    as well have dynamically allocated 1024 ints (with new int[1024]) and
    have been done with it.
     
    red floyd, Jan 16, 2005
    #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. Anonymous
    Replies:
    20
    Views:
    4,309
    Pete Becker
    Mar 30, 2005
  2. Ken
    Replies:
    24
    Views:
    3,876
    Ben Bacarisse
    Nov 30, 2006
  3. Replies:
    8
    Views:
    1,932
    Csaba
    Feb 18, 2006
  4. Replies:
    3
    Views:
    3,062
  5. Rune Allnor
    Replies:
    4
    Views:
    955
    Rune Allnor
    Dec 11, 2008
Loading...

Share This Page