if(x.size() != s) x.resize(s);

Discussion in 'C++' started by n.torrey.pines@gmail.com, Jan 25, 2007.

  1. Guest

    I wonder if resize() is required to check whether a container is
    already of the desired size.
     
    , Jan 25, 2007
    #1
    1. Advertising

  2. Ian Collins Guest

    wrote:
    > I wonder if resize() is required to check whether a container is
    > already of the desired size.
    >

    Yes, the logic specified in the standard is

    if( newSize > size())
    extend
    else if( newSize < size())
    erase
    else
    do nothing.

    --
    Ian Collins.
     
    Ian Collins, Jan 25, 2007
    #2
    1. Advertising

  3. Noah Roberts Guest

    On Jan 24, 7:15 pm, Ian Collins <> wrote:
    > wrote:
    > > I wonder if resize() is required to check whether a container is
    > > already of the desired size.Yes, the logic specified in the standard is

    >
    > if( newSize > size())
    > extend
    > else if( newSize < size())
    > erase


    Note that you're not guaranteed to get any memory back...especially if
    the container is a vector.
     
    Noah Roberts, Jan 25, 2007
    #3
  4. Alan Johnson Guest

    wrote:
    > I wonder if resize() is required to check whether a container is
    > already of the desired size.
    >


    It depends on the what behavior you want. If you want the size to be s
    no matter what, then x.resize(s) is all you need. This might add
    elements, remove elements, or do nothing, depending on the current size.

    --
    Alan Johnson
     
    Alan Johnson, Jan 25, 2007
    #4
  5. <> wrote in message
    news:...
    >I wonder if resize() is required to check whether a container is
    > already of the desired size.


    Resize potentially changes the size, rather than checking.

    Exactly what are you trying to accomplish?
     
    Andrew Koenig, Jan 25, 2007
    #5
  6. Ian Collins Guest

    Andrew Koenig wrote:
    > <> wrote in message
    > news:...
    >
    >>I wonder if resize() is required to check whether a container is
    >>already of the desired size.

    >
    >
    > Resize potentially changes the size, rather than checking.
    >

    Only if the new size != size().

    --
    Ian Collins.
     
    Ian Collins, Jan 25, 2007
    #6
  7. Guest

    On Jan 24, 8:54 pm, "Andrew Koenig" <> wrote:
    > <> wrote in messagenews:...
    >
    > >I wonder if resize() is required to check whether a container is
    > > already of the desired size.Resize potentially changes the size, rather than checking.

    >
    > Exactly what are you trying to accomplish?


    I'm up to no good, as usual.

    I need to make sure a container is of a certain size. In 99.9% of cases
    it already is. So, it makes sense to insert a test if(x.size() !=s) ,
    unless it's redundant.

    To rephrase the quesion in a language-lawyer-friendly format:

    std::vector<int> v;
    v.push_back(1);
    int* p = &(v[0]);
    v.resize(1);
    assert(*p == 1); // guaranteed by the standard or not?
     
    , Jan 25, 2007
    #7
  8. Ian Collins Guest

    wrote:
    >
    > On Jan 24, 8:54 pm, "Andrew Koenig" <> wrote:
    >
    >><> wrote in messagenews:...
    >>
    >>
    >>>I wonder if resize() is required to check whether a container is
    >>>already of the desired size.Resize potentially changes the size, rather than checking.

    >>
    >>Exactly what are you trying to accomplish?

    >
    >
    > I'm up to no good, as usual.
    >
    > I need to make sure a container is of a certain size. In 99.9% of cases
    > it already is. So, it makes sense to insert a test if(x.size() !=s) ,
    > unless it's redundant.
    >
    > To rephrase the quesion in a language-lawyer-friendly format:
    >
    > std::vector<int> v;
    > v.push_back(1);
    > int* p = &(v[0]);
    > v.resize(1);
    > assert(*p == 1); // guaranteed by the standard or not?
    >

    From the wording, yes.

    --
    Ian Collins.
     
    Ian Collins, Jan 25, 2007
    #8
  9. Greg Guest

    On Jan 24, 7:40 pm, "Noah Roberts" <> wrote:
    > On Jan 24, 7:15 pm, Ian Collins <> wrote:
    >
    > > wrote:
    > > > I wonder if resize() is required to check whether a container is
    > > > already of the desired size.Yes, the logic specified in the standard is

    >
    > > if( newSize > size())
    > > extend
    > > else if( newSize < size())
    > > eraseNote that you're not guaranteed to get any memory back...especially if

    > the container is a vector.


    Actually, you are guaranteed that you will not get any memory back from
    the vector by resizing it to a smaller size.

    Greg
     
    Greg, Jan 25, 2007
    #9
  10. wrote:
    >
    > On Jan 24, 8:54 pm, "Andrew Koenig" <> wrote:
    >> <> wrote in messagenews:...
    >>
    >>> I wonder if resize() is required to check whether a container is
    >>> already of the desired size.Resize potentially changes the size, rather than checking.

    >> Exactly what are you trying to accomplish?

    >
    > I'm up to no good, as usual.
    >
    > I need to make sure a container is of a certain size. In 99.9% of cases
    > it already is. So, it makes sense to insert a test if(x.size() !=s) ,
    > unless it's redundant.


    It's redundant.

    >
    > To rephrase the quesion in a language-lawyer-friendly format:
    >
    > std::vector<int> v;
    > v.push_back(1);
    > int* p = &(v[0]);
    > v.resize(1);
    > assert(*p == 1); // guaranteed by the standard or not?


    Yes.


    --
    Clark S. Cox III
     
    Clark S. Cox III, Jan 25, 2007
    #10
  11. Guest

    On Jan 24, 7:15 pm, Ian Collins <> wrote:
    > wrote:
    > > I wonder if resize() is required to check whether a container is
    > > already of the desired size.Yes, the logic specified in the standard is

    >
    > if( newSize > size())
    > extend


    What does this newSize mean?

    For example, ten elements are pushed into a newly created vector. Does
    the size mean the numbers of elements in the vector or the memory that
    the vector possesses? Does the size of the vector (memory?) increase
    automatically to hold all elements?

    > else if( newSize < size())
    > erase


    I read from another post and it says, erase() used upon vector or other
    library utilities doesn't release the memory that is possessed by the
    vector. One people mentioned that the swap() idiom can release the
    memory.

    If I push_back elements into the vector continuously and only call
    erase() after use it, and I don't use that complex swap(), will this
    vector eat up memory? Thank you.

    > else
    > do nothing.
     
    , Jan 25, 2007
    #11
  12. Rolf Magnus Guest

    wrote:

    >
    >
    > On Jan 24, 7:15 pm, Ian Collins <> wrote:
    >> wrote:
    >> > I wonder if resize() is required to check whether a container is
    >> > already of the desired size.Yes, the logic specified in the standard is

    >>
    >> if( newSize > size())
    >> extend

    >
    > What does this newSize mean?


    The size that you give as argument to resize().

    > For example, ten elements are pushed into a newly created vector. Does
    > the size mean the numbers of elements in the vector or the memory that
    > the vector possesses?


    In the context of C++ standard containers, 'size' always means the number of
    elements. The amount of memory (in multiples of the size of the element
    type) is called 'capacity'.

    > Does the size of the vector (memory?) increase automatically to hold all
    > elements?


    The capacity will automatically increase.

    >> else if( newSize < size())
    >> erase

    >
    > I read from another post and it says, erase() used upon vector or other
    > library utilities doesn't release the memory that is possessed by the
    > vector.


    That's right. The size will be reduced, but not the capacity.

    > One people mentioned that the swap() idiom can release the
    > memory.


    I'm not sure if that's guaranteed by the standard, but it's commonly used.

    > If I push_back elements into the vector continuously and only call
    > erase() after use it, and I don't use that complex swap(), will this
    > vector eat up memory? Thank you.


    Yes.
     
    Rolf Magnus, Jan 25, 2007
    #12
  13. Guest

    On Jan 25, 6:12 am, Rolf Magnus <> wrote:

    > wrote:
    >> If I push_back elements into the vector continuously and only call
    >> erase() after use it, and I don't use that complex swap(), will this
    >> vector eat up memory? Thank you.

    >
    >Yes.


    and uses up all memory, and the system crash eventually?
     
    , Jan 25, 2007
    #13
  14. a écrit :
    >
    > On Jan 25, 6:12 am, Rolf Magnus <> wrote:
    >
    >> wrote:
    >>> If I push_back elements into the vector continuously and only call
    >>> erase() after use it, and I don't use that complex swap(), will this
    >>> vector eat up memory? Thank you.

    >> Yes.

    >
    > and uses up all memory, and the system crash eventually?
    >


    It is not a memory leak. The vector only keeps the maximum reserve it
    has allocated.

    The swap trick is useful only if you insert a lot of data in a vector
    and then reduce the size drastically without wanting to make it grow
    further.


    Michael
     
    Michael DOUBEZ, Jan 25, 2007
    #14
  15. Ron Natalie Guest

    Noah Roberts wrote:
    >
    > On Jan 24, 7:15 pm, Ian Collins <> wrote:
    >> wrote:
    >>> I wonder if resize() is required to check whether a container is
    >>> already of the desired size.Yes, the logic specified in the standard is

    >> if( newSize > size())
    >> extend
    >> else if( newSize < size())
    >> erase

    >
    > Note that you're not guaranteed to get any memory back...especially if
    > the container is a vector.
    >

    Specifically, if the container is a vector, you are guaranteed NOT to
    get any memory back.
     
    Ron Natalie, Jan 25, 2007
    #15
  16. Ron Natalie Guest

    wrote:

    >
    > For example, ten elements are pushed into a newly created vector. Does
    > the size mean the numbers of elements in the vector or the memory that
    > the vector possesses? Does the size of the vector (memory?) increase
    > automatically to hold all elements?
    >

    It means the size that the vector is.

    If the requested new size is less than the capacity of the vector,
    then the capacity must also be increased. Otherwise the internal
    size is increased and the elements between the old end and the new
    end are filled with the second (defaulted in this case) argument
    to resize.
     
    Ron Natalie, Jan 25, 2007
    #16
  17. Rolf Magnus Guest

    wrote:

    >
    >
    > On Jan 25, 6:12 am, Rolf Magnus <> wrote:
    >
    >> wrote:
    >>> If I push_back elements into the vector continuously and only call
    >>> erase() after use it, and I don't use that complex swap(), will this
    >>> vector eat up memory? Thank you.

    >>
    >>Yes.

    >
    > and uses up all memory, and the system crash eventually?


    I think you're mis-understanding something here. push_back will increase the
    capacity only if needed. So if you have 100 elements in your vector, size()
    is 100. Let's assume capacity() is also 100. If you erase 90 elements, the
    capacity will stay at 100, but the size is reduced to 10. If you
    push_back() 90 elements afterwards, your size is back to 100, and the
    capacity is 100, too. There is no continuous growth unless you continuously
    increase the number of elements stored.
     
    Rolf Magnus, Jan 25, 2007
    #17
  18. Rolf Magnus a écrit :
    > wrote:
    >>[snip]
    >> One people mentioned that the swap() idiom can release the
    >> memory.

    >
    > I'm not sure if that's guaranteed by the standard, but it's commonly used.


    The standard specifies that swap() "should have constant complexity"
    which usually means that the memory holding is passed from one object to
    the other - which is what we want.

    What you are not garanteed is that all excess memory is released (i.e.
    there is no garanty that max_capacity()==size()). The rational is that
    some implementation don't allocate memory under a specific threshold.

    Michael
     
    Michael DOUBEZ, Jan 25, 2007
    #18
  19. <> wrote in message
    news:...

    > I'm up to no good, as usual.


    > I need to make sure a container is of a certain size. In 99.9% of cases
    > it already is. So, it makes sense to insert a test if(x.size() !=s) ,
    > unless it's redundant.


    I still don't understand what you mean by "make sure a container is of a
    certain size."

    Do you mean that you want to force it to be that size if it isn't already?

    Or do you mean that you want to check whether it is that size and sound an
    alarm (e.g. throw an exception) if it isn't?

    If the former, then resize does exactly what you want. If the latter, I
    would suggest doing something like

    assert(v.size() == n);


    > To rephrase the quesion in a language-lawyer-friendly format:
    >
    > std::vector<int> v;
    > v.push_back(1);
    > int* p = &(v[0]);
    > v.resize(1);
    > assert(*p == 1); // guaranteed by the standard or not?


    Yes, because resize has no effect if the size is already what you are
    requesting.

    However, if you intend to rely on *p not changing, then if for some reason
    the size is *not* what you had in mind, you might be in trouble. That's
    part of why I asked exactly what you're trying to do.
     
    Andrew Koenig, Jan 25, 2007
    #19
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SirThanxALot

    Swing size/resize issue

    SirThanxALot, Dec 8, 2003, in forum: Java
    Replies:
    2
    Views:
    9,355
    SirThanxALot
    Dec 9, 2003
  2. Boki
    Replies:
    5
    Views:
    551
  3. Jason Cavett

    Preferred Size, Minimum Size, Size

    Jason Cavett, May 23, 2008, in forum: Java
    Replies:
    5
    Views:
    12,586
    Michael Jung
    May 25, 2008
  4. Philip

    XHTML - Resize Table to Windows Size

    Philip, Apr 5, 2006, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    113
    Philip
    Apr 5, 2006
  5. Replies:
    2
    Views:
    1,266
    Adrienne
    Feb 13, 2005
Loading...

Share This Page