about std::vector < int > - memory allocation

R

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.
 
A

Alf P. Steinbach

* 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)?
 
R

Rakesh Sinha

Alf said:
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.
 
R

red floyd

Rakesh said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top