Click here to Skip to main content
15,867,895 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to create a collection of int, so I used

C++
Vector<int> arraych = {};

to store int values.

But while I am defining the vector, its size is automatically taken, even before I am adding any int value to it, which is shown in the following https://1drv.ms/u/s!AlmRNfwmzHbRaoejscwiJIEhnPg?e=I4PL5E image.

Can anyone please advise me why, while declaring the variable itself, how the size of vector is automatically taken even before adding any value to it?

What I have tried:

I tried arraych.clear(); but if I clear the vector, I wasn't able to add value to it using below line.

C++
arraych.push_back(W_name);
Posted
Updated 7-Feb-22 10:27am
v4
Comments
jeron1 7-Feb-22 12:22pm    
Just declare the vector, and add things to it;

std::vector<int> arraych; // Declare, after this point size should be zero
int theInt = 42;
arraych.push_back(theInt); // add as needed , size should be 1 after this
Richard MacCutchan 7-Feb-22 12:30pm    
Which compiler are you using? The initial size set in your vector is completely wrong.
was eef 7-Feb-22 12:34pm    
https://1drv.ms/u/s!AlmRNfwmzHbRaoejscwiJIEhnPg?e=I4PL5E

i tried adding variable, even after adding variable, it is still showing size much larger

i use VS2019
jeron1 7-Feb-22 12:50pm    
Sorry, I am not going to follow the link, can you post your current code and describe what it is doing that you believe is incorrect? You can edit your original post for this.

I think you are confusing a vector's capacity with its size. std::vector<t>.capacity() is the number of elements the vector can hold based on the current memory allocation. Whether that is the same thing as the capacity that is shown here is not clear.

If you know that you have a large number of items, say 10,000 or 1,000,000 to add to the vector, you can pre-allocate using one of the vector constructors:
C++
std::vector<int> arr(1000000); // declare a vector with initial storage for 1,000,000 ints
Note that this does not affect the size of arr, it will still be zero, but when adding elements to the array, the first 1,000,000 items will have already been allocated.
 
Share this answer
 
Comments
was eef 7-Feb-22 12:45pm    
https://1drv.ms/u/s!AlmRNfwmzHbRaoejscwiJIEhnPg?e=I4PL5E

can you please see the image on above link, which shows the error which i am getting
i don't know how many items will be added to arr, so i can't predefine it..
You are not seeing valid information. I have just reproduced your problem. You are trying to view an object in the debugger before it has been initialised. Step through your code until you get to that actual line of code and you will see the correct information.
 
Share this answer
 
Comments
was eef 7-Feb-22 13:20pm    
i tried adding single item to arraych, but the size is still larger.
see the image in this link - https://1drv.ms/u/s!AlmRNfwmzHbRa764NuRgwh8JLUU?e=SmigOg
Richard MacCutchan 7-Feb-22 15:08pm    
That looks exactly the same as the issue I mentioned above. You have two breakpoints in that code, but you are not stopped on either of them so it is not clear what is happening. You should put breakpoints on two locations:
1. The line where the arraych is defined, or
2. The line where you call arraych.push_back

Only when the debugger stops at one of those locations will you get a valid reading.
Thanks everyone for your guidance & Support

Actually the setting of Debugging for Optimization has been set as Optimization(Favor Speed)(/Ox)
once i changed the setting of Optimization to Disable, the debugger shows the correct Output.

See the Image on the Link https://www.dropbox.com/s/4br108xs6k8i3yh/Img%203.png?dl=0[^]
 
Share this answer
 
Comments
Richard MacCutchan 8-Feb-22 4:06am    
I have tested it with both those optimisation values and see exactly the same values in both cases.
was eef 8-Feb-22 4:51am    
Since my code is very large,
it might be due to the error didn't occur for you in a your sample test.
but after Disabling the Optimization, it worked properly for me.

Highly Appreciated for your time..
jeron1 8-Feb-22 13:34pm    
That's actually a scary thought, basic std::vector operation altered by the size and/or complexity of the project? Doesn't sound right to me.
Richard MacCutchan 9-Feb-22 5:14am    
Sounds totally wrong. And I still stand by my comments in Solution 2.
jeron1 9-Feb-22 9:58am    
Agreed.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900