473,811 Members | 3,290 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

about std::vector < int > - memory allocation

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.

Jul 22 '05 #1
3 2270
* 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?
Jul 22 '05 #2

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.

Jul 22 '05 #3
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.
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
7082
by: Stefan Höhne | last post by:
Hi, as I recon, std::vector::clear()'s semantics changed from MS VC++ 6.0 to MS' DOT.NET - compiler. In the 6.0 version the capacity() of the vector did not change with the call to clear(), in DOT.NET the capacity() is reduced to 0.
2
4783
by: Chris Thompson | last post by:
Hi I'm writing a p2p client for an existing protocol. I used a std::vector<char> as a buffer for messages read from the server. The message length is the first 4 bytes. The message code the second 4. The total message length is therefore 4 + message length. A number of messages work fine/as expected but there are consistant errors occuring. After a period
11
8887
by: Steve | last post by:
Hi, I'm using a std::vector to store a list of user defined objects. The vector may have well over 1000 elements, and I'm suffering a performance hit. If I use push_back I get a much worse perfomance than if I first define the vector of a given size, then write to the elements with myvec = However, I'm currently thinking that it isn't feasible to obtain the vector size, so really need to resize the vector dynamically as I go. Is...
11
1930
by: Lynn | last post by:
Hi, Is it better to use std::vector <type> with a pointer to the types being stored or the actual storage for the types ? In other words, is std::vector <mytype> or std::vector <mytype *> better ? I know that there are specific cases where it is dictated but in the general case, what do you think ? Thanks, Lynn McGuire
20
17848
by: Anonymous | last post by:
Is there a non-brute force method of doing this? transform() looked likely but had no predefined function object. std::vector<double> src; std::vector<int> dest; std::vector<double>::size_type size = src.size(); dest.reserve(size); for (std::vector<int>::size_type i = 0;
24
2940
by: simon | last post by:
Hi, First some background. I have a structure, struct sFileData { char*sSomeString1; char*sSomeString2;
4
20140
by: Bobrick | last post by:
Hi. I'm in the process of making a GUI for a function someone else wrote, and i've come across a type i'm unfamiliar with, namely "std::vector<unsigned char>". I need to get the contents of this variable into a form I can display in a text box, but i'm not sure what to expect inside of the variable, whether I can just treat it like an array e.t.c. Any help would be appreciated. Thanks,
1
2401
by: sergio311 | last post by:
Hi all, I'm new of c++ and i'm writing a class that create a matrix of data (of T type, it's a template class). The member variable m_data must stores the matrix's data and i prefer use heap so, i need to allocate the std::vector<std::vector<T> > member variable with new operator. How can i?
2
1982
by: Tim | last post by:
Dear All, std::vector seems using the copy constructor to allocate memory when needed. Is it possible to control the memory allocation, such as by using my own allocator? My question is as follow: I have a smart pointer my_ptr, and the copy constructor uses deep copy, however I want it to be shallow copy in std::vector when reallocate memory. How can I implement it?
8
3626
by: jacek.dziedzic | last post by:
Hi! I need to be able to track memory usage in a medium-sized application I'm developing. The only significant (memory-wise) non- local objects are of two types -- std::vector<and of a custom class simple_vector<that is a hand-rolled substitute for array<>. With the latter I have code that tracks all allocations and destructions, so I can account for all the memory. The question is about std::vector<-- how can I track memory usage
0
9726
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10647
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10130
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9204
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7667
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5553
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5692
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4338
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3865
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.