473,796 Members | 2,603 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

vector push_back question

Consider the source snippet

int main()
{
std::vector<dou ble> vec_push( 0x10 );
size_t const SZ = vec_push.size() ;
std::cout << vec_push.size() << std::endl;

if ( SZ != 0x10 ) {
cout << " something's amiss " << endl;
return 0;
}

vec_push.reserv e(0x10000); // for test ..
for (size_t jdx(0); jdx < SZ; ++jdx)
{
vec_push.push_b ack( jdx );
std::cout << " JDX " << jdx << std::endl;
std::cout << " vec_push.size() " << vec_push.size() <<
std::endl;
}
std::cout << vec_push.size() << std::endl;
}

------ The results:
16
JDX 0
vec_push.size() 17
JDX 1
vec_push.size() 18
JDX 2
vec_push.size() 19
JDX 3
vec_push.size() 20
JDX 4
vec_push.size() 21
JDX 5
vec_push.size() 22
JDX 6
vec_push.size() 23
JDX 7
vec_push.size() 24
JDX 8
vec_push.size() 25
JDX 9
vec_push.size() 26
JDX 10
vec_push.size() 27
JDX 11
vec_push.size() 28
JDX 12
vec_push.size() 29
JDX 13
vec_push.size() 30
JDX 14
vec_push.size() 31
JDX 15
vec_push.size() 32
32
-------
At issue: The results from the program suggest that - with each call
to push_back the vector re-allocates. A bit of a mystery since I've
already allocated enough space with reserve

This compiler ( which unfortunately I'm stuck with ) is gcc 2.96 -
which I suspect is sub par. The question then becomes: From the looks
of it, I suspect what it amounts to is - the implementation 'is what it
is' and my choice the becomes a different container - perhaps or use
operator []?

i.e.
vec_push[ jdx ] = jdx;

In that regard, the result now becomes:

16
JDX 0
vec_push.size() 16
JDX 1
vec_push.size() 16
JDX 2
vec_push.size() 16
JDX 3
vec_push.size() 16
JDX 4
vec_push.size() 16
JDX 5
vec_push.size() 16
JDX 6
vec_push.size() 16
JDX 7
vec_push.size() 16
JDX 8
vec_push.size() 16
JDX 9
vec_push.size() 16
JDX 10
vec_push.size() 16
JDX 11
vec_push.size() 16
JDX 12
vec_push.size() 16
JDX 13
vec_push.size() 16
JDX 14
vec_push.size() 16
JDX 15
vec_push.size() 16
16

Jan 9 '06 #1
8 1829
On 8 Jan 2006 06:43:47 -0800, "ma740988" <ma******@gmail .com> wrote:
Consider the source snippet

int main()
{
std::vector<dou ble> vec_push( 0x10 );
size_t const SZ = vec_push.size() ;
std::cout << vec_push.size() << std::endl;

if ( SZ != 0x10 ) {
cout << " something's amiss " << endl;
return 0;
}

vec_push.reserv e(0x10000); // for test ..
for (size_t jdx(0); jdx < SZ; ++jdx)
{
vec_push.push_b ack( jdx );
std::cout << " JDX " << jdx << std::endl;
std::cout << " vec_push.size() " << vec_push.size() <<
std::endl;
}
std::cout << vec_push.size() << std::endl;
}
<snip>
Size and capacity are not the same. Capacity is the actual number of
objects available before another resize is necessary, while size is
the actual number of objects already assigned.

Look at it it this other way, and compare results:
int main()
{
std::vector<dou ble> vec_push( 0x10 );
size_t const SZ = vec_push.size() ;
std::cout << vec_push.size() << std::endl;

if ( SZ != 0x10 ) {
cout << " something's amiss " << endl;
return 0;
}

vec_push.reserv e(0x10000); // for test ..
for (size_t jdx(0); jdx < SZ; ++jdx)
{
vec_push.push_b ack( jdx );
std::cout << " JDX " << jdx << std::endl;
std::cout << " vec_push.size() " << vec_push.size() <<
std::endl; /* added */
std::cout << " vec_push.capaci ty() " << vec_push.capaci ty() <<
std::endl;
/* end of additions */ }
std::cout << vec_push.size() << std::endl;
}

Jan 9 '06 #2
"ma740988" <ma******@gmail .com> schrieb im Newsbeitrag
news:11******** **************@ g43g2000cwa.goo glegroups.com.. .
Consider the source snippet

int main()
{
std::vector<dou ble> vec_push( 0x10 );
size_t const SZ = vec_push.size() ;
std::cout << vec_push.size() << std::endl;

if ( SZ != 0x10 ) {
cout << " something's amiss " << endl;
return 0;
}

vec_push.reserv e(0x10000); // for test ..
for (size_t jdx(0); jdx < SZ; ++jdx)
{
vec_push.push_b ack( jdx );
std::cout << " JDX " << jdx << std::endl;
std::cout << " vec_push.size() " << vec_push.size() <<
std::endl;
}
std::cout << vec_push.size() << std::endl;
}

------ The results:
16
JDX 0
vec_push.size() 17 .... -------
At issue: The results from the program suggest that - with each call
to push_back the vector re-allocates. A bit of a mystery since I've
already allocated enough space with reserve
This question suggest that you didn't read the docs properly. size returns
the number of elements ACTUALLY USED while reserve allocates space for
elements that CAN BE USED. If you want to know how muc memory has been
reserved for a vector, you should use its capacity function. And you should
also keep in mind that those constructors of std::vector, that require an
element count, create vectors with the specified number of elements, not
just with enough space for them.
This compiler ( which unfortunately I'm stuck with ) is gcc 2.96 -
which I suspect is sub par. The question then becomes: From the looks
of it, I suspect what it amounts to is - the implementation 'is what it
is' and my choice the becomes a different container - perhaps or use
operator []?

i.e.
vec_push[ jdx ] = jdx;

In that regard, the result now becomes:

16
JDX 0
vec_push.size() 16
JDX 1
vec_push.size() 16


Using operator[] you can only access existing elements of a vector. It
cannot create new ones. So it does not surprise me the the number of
elements remains constant.

HTH
Heinz
Jan 9 '06 #3
In article <11************ **********@g43g 2000cwa.googleg roups.com>,
"ma740988" <ma******@gmail .com> wrote:
Consider the source snippet

int main()
{
std::vector<dou ble> vec_push( 0x10 );
size_t const SZ = vec_push.size() ;
std::cout << vec_push.size() << std::endl;

if ( SZ != 0x10 ) {
cout << " something's amiss " << endl;
return 0;
}

vec_push.reserv e(0x10000); // for test ..
for (size_t jdx(0); jdx < SZ; ++jdx)
{
vec_push.push_b ack( jdx );
std::cout << " JDX " << jdx << std::endl;
std::cout << " vec_push.size() " << vec_push.size() <<
std::endl;
std::cout << "vec location: " << &vec_push[0] << std::endl;
}
std::cout << vec_push.size() << std::endl;
} At issue: The results from the program suggest that - with each call
to push_back the vector re-allocates. A bit of a mystery since I've
already allocated enough space with reserve


On my system each call does not re-allocate the space in your program.
You aren't outputing enough information to make the determination on
your system. Try adding the line I placed in your program and see what
the output is...

You will likely notice that the address of the first element of the
vector does not change, which proves that the block of memory was not
moved.
Jan 9 '06 #4

|| Size and capacity are not the same. Capacity is the actual number of

|| objects available before another resize is necessary, while size
is
|| the actual number of objects already assigned.

Never mind. I caught my error. I've already made room for 16 objects.
So of course each call to push_back will create 16 more. I already
understood the distinction between size and capacity. This is one of
those items in source, where the construct threw me for a loop. One
of those things where you might have wanted ++i as opposed to i++ :)

Jan 9 '06 #5

Zara wrote:
/* added */
std::cout << " vec_push.capaci ty() " << vec_push.capaci ty() <<
std::endl;
/* end of additions */
}
std::cout << vec_push.size() << std::endl;
}


You could also output static_cast<voi d *>( &vec_push[0] ) (as long as
vec_push is not empty which it never is).

That will show you the address of the first element in your vector and
you will be able to see whether or not it moves.

Jan 9 '06 #6

You will likely notice that the address of the first element of the
vector does not change, which proves that the block of memory was not
moved.


That doesn't neccessarily prove that the vector was not re-allocated. It's
_possible_ that memory could be allocated at the same address.

-Howard


Jan 9 '06 #7
In article <uH************ ********@bgtnsc 04-news.ops.worldn et.att.net>,
"Howard" <al*****@hotmai l.com> wrote:

You will likely notice that the address of the first element of the
vector does not change, which proves that the block of memory was not
moved.


That doesn't neccessarily prove that the vector was not re-allocated. It's
_possible_ that memory could be allocated at the same address.

-Howard


True, but simply printing the capacity doesn't work either. It's
_possible_ that the vector was re-allocated but made the same size.

Come to think about it, even knowing the capacity *and* the location of
the first element isn't enough, it's _possible_ that the system
allocated the memory in another place then put it back to the original
location.
Jan 10 '06 #8
Daniel T. wrote:
True, but simply printing the capacity doesn't work either. It's
_possible_ that the vector was re-allocated but made the same size.

Not in the case of push_back. If v.size() < v.capacity(), then
v.push_back(... ) must NOT allocate. Otherwise v.reserve(...) would be
meaningless.

-shez-

Jan 10 '06 #9

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

Similar topics

10
7079
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.
9
2976
by: luigi | last post by:
Hi, I am trying to speed up the perfomance of stl vector by allocating/deallocating blocks of memory manually. one version of the code crashes when I try to free the memory. The other version seem to work. I would appreciate someone to comment on this. Version 1 (crashes on deallocating) #include <iostream>
4
3102
by: Venn Syii | last post by:
I've searched all the forums but cannot find an answer to this question. I do the following: vector<MyClass*> myClassList; Later in the program I try to add to myClassList with a .push_back(...) I get an "out of memory" runtime error. I know I'm not out of memory because normal vectors such as vector<int> a, still work, and still work fine.
3
6736
by: Daniel J Watkins | last post by:
Hi, Some runtime memory exceptions are being exhibited with some code I've written. Can you clarify the following with you to see if my understanding of the principles under question are correct. I'm trying to create a data structure that will allow me to have a dynamic number of columns and a dynamic number of rows in each column (there may be ten rows in column one and twenty rows in column two for example). i. I can declare...
9
15906
by: Jeff | last post by:
Hello- Ive never used a vector or vectors in C++ so I have a question for you all. I know I can dynamically create the size I need upfront, but is it possible to create them on the fly dynamically? That is, for a 2 dim array, I want to create the first element and then push_back the columns after that:
6
4002
by: Jia | last post by:
Hi all, I have a class foo which has a static vector of pointers of type base class, and a static function to set this vector. #include <iostream> #include <vector> using namespace std; class super{ protected:
32
4042
by: T. Crane | last post by:
Hi, I'm struggling with how to initialize a vector<vector<double>> object. I'm pulling data out of a file and storing it in the vector<vector<double>object. Because any given file will have a large amount of data, that I read off using an ifstream object, I don't want to use the push_back method because this grows the vector<vector<double>dynamically, and that will kill my execution time. So, I want to reserve space first, using, of...
6
11617
by: jmsanchezdiaz | last post by:
CPP question: if i had a struct like "struct str { int a; int b };" and a vector "std::vector < str test;" and wanted to push_back a struct, would i have to define the struct, fill it, and then push_back it, or could i pushback the two ints directly somehow? Thanks for all.
13
1931
by: prasadmpatil | last post by:
I am new STL programming. I have a query regarding vectors. If I am iterating over a vector using a iterator, but do some operations that modify the size of the vector. Will the iterator recognize this? I wrote the following program to test this out. #include <fstream> #include <iostream> #include <string>
6
7387
by: Mr. K.V.B.L. | last post by:
I want to start a map with keys but an empty vector<string>. Not sure what the syntax is here. Something like: map<string, vector<string MapVector; MapVector.insert(make_pair("string1", new vector<string>)); MapVector.insert(make_pair("string2", new vector<string>)); MapVector.insert(make_pair("string3", new vector<string>));
0
10242
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10200
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10021
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
9061
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
7558
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
6800
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4127
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
3744
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.