c++ vector is getting predefined

Waseefur Rahman 21 Reputation points
2022-02-07T16:57:13.567+00:00

I want to create a collection of int, so i used 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 attached image.

can anyone advice, while declaring the variable itself how the size of vector is automatically taken, even before adding any value to it?

171988-img-1.png

Developer technologies C++
{count} votes

Accepted answer
  1. WayneAKing 4,931 Reputation points
    2022-02-08T01:32:12.623+00:00

    As you both suggested the Debugger showing wrong information,
    i check i release mode, it shows the correct values.

    Your description seems to be the opposite of what you were told.

    When checking sizes, contents, etc. of variables and constants,
    or following execution paths, you need to use a Debug build.

    If using a Release build, addresses, contents and even the
    actual existence of object code may not match the source code.
    This usually happens because in a Release build optimizations
    are enabled by default. You can usually step through code and
    watch memory contents in a Release build IF you turn off all
    optimizations in the project properties.

    In a Debug build, optimizing is off by default so tracing
    code execution paths, memory contents, etc. should always
    work accurately.

    In short, a vector size and contents may not appear as
    expected when trying to debug a RELEASE build with
    optimizing enabled. But there should be no issues when
    checking a vector during debugging of a DEBUG build.

    Just wants to know how to rectify the debugger to show the
    correct information while debugging..

    it is automatically taking the size as some thing else,
    see the below

    172072-vector-debugging.jpg

    One issue that can affect the debugger's display of the
    vector even in a Debug build is trying to examine the
    vector before it has actually been constructed.

    This appears to be what you have done in the last image
    you posted. The caret at the left of the code lines in
    the IDE debugger window indicates that you have stopped
    ON THE LINE where the vector is declared.

    That line will not have been executed yet, so trying to
    examine the vector at that point will often show garbage.
    You MUST step over (execute) the line declaring the vector
    before trying to examine it in the debugger.

    • Wayne

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.