473,320 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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

Jan 25 '07 #1
18 1707
n.************@gmail.com 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.
Jan 25 '07 #2


On Jan 24, 7:15 pm, Ian Collins <ian-n...@hotmail.comwrote:
n.torrey.pi...@gmail.com 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.

Jan 25 '07 #3
n.************@gmail.com 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
Jan 25 '07 #4
<n.************@gmail.comwrote in message
news:11*********************@v33g2000cwv.googlegro ups.com...
>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?
Jan 25 '07 #5
Andrew Koenig wrote:
<n.************@gmail.comwrote in message
news:11*********************@v33g2000cwv.googlegro ups.com...
>>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.
Jan 25 '07 #6


On Jan 24, 8:54 pm, "Andrew Koenig" <a...@acm.orgwrote:
<n.torrey.pi...@gmail.comwrote in messagenews:11*********************@v33g2000cwv.go oglegroups.com...
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<intv;
v.push_back(1);
int* p = &(v[0]);
v.resize(1);
assert(*p == 1); // guaranteed by the standard or not?

Jan 25 '07 #7
n.************@gmail.com wrote:
>
On Jan 24, 8:54 pm, "Andrew Koenig" <a...@acm.orgwrote:
>><n.torrey.pi...@gmail.comwrote in messagenews:11*********************@v33g2000cwv.go oglegroups.com...

>>>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<intv;
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.
Jan 25 '07 #8


On Jan 24, 7:40 pm, "Noah Roberts" <roberts.n...@gmail.comwrote:
On Jan 24, 7:15 pm, Ian Collins <ian-n...@hotmail.comwrote:
n.torrey.pi...@gmail.com 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

Jan 25 '07 #9
n.************@gmail.com wrote:
>
On Jan 24, 8:54 pm, "Andrew Koenig" <a...@acm.orgwrote:
><n.torrey.pi...@gmail.comwrote in messagenews:11*********************@v33g2000cwv.go oglegroups.com...
>>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<intv;
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
cl*******@gmail.com
Jan 25 '07 #10


On Jan 24, 7:15 pm, Ian Collins <ian-n...@hotmail.comwrote:
n.torrey.pi...@gmail.com 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 '07 #11
lovecreatesbea...@gmail.com wrote:
>

On Jan 24, 7:15 pm, Ian Collins <ian-n...@hotmail.comwrote:
>n.torrey.pi...@gmail.com 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.

Jan 25 '07 #12


On Jan 25, 6:12 am, Rolf Magnus <ramag...@t-online.dewrote:
lovecreatesbea...@gmail.com 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 '07 #13
lovecreatesbea...@gmail.com a écrit :
>
On Jan 25, 6:12 am, Rolf Magnus <ramag...@t-online.dewrote:
>lovecreatesbea...@gmail.com 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
Jan 25 '07 #14
Noah Roberts wrote:
>
On Jan 24, 7:15 pm, Ian Collins <ian-n...@hotmail.comwrote:
>n.torrey.pi...@gmail.com 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.
Jan 25 '07 #15
lovecreatesbea...@gmail.com 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.
Jan 25 '07 #16
lovecreatesbea...@gmail.com wrote:
>

On Jan 25, 6:12 am, Rolf Magnus <ramag...@t-online.dewrote:
>lovecreatesbea...@gmail.com 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.
Jan 25 '07 #17
Rolf Magnus a écrit :
lovecreatesbea...@gmail.com 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
Jan 25 '07 #18
<n.************@gmail.comwrote in message
news:11**********************@j27g2000cwj.googlegr oups.com...
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<intv;
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.
Jan 25 '07 #19

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

Similar topics

1
by: Brendan | last post by:
My table resizes just once when there is a mouseover on any of the Update or delete buttons. This app is being developed in CF 6.1 with a fusebox 4.1 framework just an FYI I have put the two...
0
by: Mahadeva Swamy GS | last post by:
Hi, When windows form (C#) is designed in given resolution and large fonts. Form automatically resizes on compilation if font size of windows environment is changed. Any suggestions for the...
2
by: DaveO | last post by:
OK. Now I'm all googled out and still puzzled ! I admit I am very new to javascript, but think that it is the only solution to this. I have a web page with a table ( 2 columns, 1 row ) The...
1
by: Kalvin | last post by:
I am using a nested table to control different elements of my layout. The problem I have is that I create the outer table and set the size properties to what I want them to be, then, if I put...
5
by: Just D. | last post by:
How can we get the browser window size from C# codebehind? What's the easiest way to do that? Just D.
2
by: Dominique | last post by:
Hello, Im want to fix the width of a dashboard. I have a field which has a field (in this dashboard on the left part of my screen) which could be long and make the right side of the screen out...
2
by: sachjn | last post by:
Hi, I need to have a table along with other UI items in page which resizes horizontally and vertically with the browser window. That means table should accoupy the remaining available space in...
15
RedSon
by: RedSon | last post by:
Yea! Now after years of applications work I have finally bit the bullet and gotten myself a website playground. But I am running into a problem. I want to be able to make some money off this site...
2
by: =?Utf-8?B?VGVycnk=?= | last post by:
Hope someone has some ideas - this is driving me nuts, I am using VS2008 and have a tightly layed out form at 8 pts. The form has a font dialog and when the user changes the font size, everything...
1
robkeithd
by: robkeithd | last post by:
Greetings, In a nutshell: I've got a panel sitting on a form that is docked to full sitting next to a vertical scrollbar that is docked left. There is a picture control sitting in the panel that...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.