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

N

n.torrey.pines

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

Ian Collins

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.
 
N

Noah Roberts

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.
 
A

Alan Johnson

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.
 
A

Andrew Koenig

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?
 
N

n.torrey.pines

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?
 
I

Ian Collins

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?
[/QUOTE]
From the wording, yes.
 
C

Clark S. Cox III

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.
 
L

lovecreatesbea...

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.
 
R

Rolf Magnus

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.
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.
 
M

Michael DOUBEZ

(e-mail address removed) a écrit :
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
 
R

Ron Natalie

Noah said:
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.
 
R

Ron Natalie

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.
 
R

Rolf Magnus

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.
 
M

Michael DOUBEZ

Rolf Magnus a écrit :
[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
 
A

Andrew Koenig

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top