Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > about std::vector < int > - memory allocation

Reply
Thread Tools

about std::vector < int > - memory allocation

 
 
Rakesh Sinha
Guest
Posts: n/a
 
      01-16-2005
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.

 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      01-16-2005
* 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?
 
Reply With Quote
 
 
 
 
Rakesh Sinha
Guest
Posts: n/a
 
      01-16-2005

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.

 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      01-16-2005
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.
 
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
static memory allocation versus dynamic memory allocation Ken C Programming 24 11-30-2006 12:37 AM
Difference between int i, j; and int i; int j; arun C Programming 8 07-31-2006 05:11 AM
What is the difference between dynamic memory allocation,and stack allocation ? chris C++ 6 10-28-2005 05:27 AM
int main(int argc, char *argv[] ) vs int main(int argc, char **argv ) Hal Styli C Programming 14 01-20-2004 10:00 PM
dirty stuff: f(int,int) cast to f(struct{int,int}) Schnoffos C Programming 2 06-27-2003 03:13 AM



Advertisments