vector :: resize()和内存分配 [英] vector::resize() and memory allocation

查看:348
本文介绍了vector :: resize()和内存分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

---------

#include< vector>

using namespace std;

struct Foo

{

Foo(int){}

};

int main()

{

vector< Foo> v1;

v1.resize(1);

v1 [0] = Foo(100);

//检查点

返回0;

}

---------


多少Foo实例是在检查点分配的内存?

似乎是2个坚持?

如果是真的,那就意味着使用了vector :: resize()是否需要分配额外的内存?

-

Alex Vinokur
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

解决方案

Alex Vinokur写道:

---------
#include< vector>
使用命名空间std;
struct Foo
{
Foo(int){}
};
int main()
{
vector< Foo> v1;
v1.resize(1);
v1 [0] = Foo(100);
//检查点
返回0;
}
---------

对于在检查点分配的内存有多少个Foo实例?
似乎是2个坚持?
如果是是真的是否意味着使用vector :: resize()涉及分配额外的内存?




该程序格式错误,因为''Foo''不合适要求

" Default-constructible"要求调整''调整大小''。它应该

不能编译。


现在,如果你错过了那个东西,实际上并不是要问

a技巧问题(据说''Foo''实际上有一个默认的c-tor),

然后未指定多少个对象''v1''实际上可以包含

请求调整大小后,因为它的初始容量为0

和''resize''_ will会导致重新分配以容纳您的对象。

分配的大小发生是未指定的(实现 -

定义,我相信)。


所以,在''Foo''给出一个默认的c-tor之后,回答到你的第一个

问题是未知或者实施定义,你的第二个问题的回答是我不明白,而你第三个问题的答案是b $ b ;是的,可能是。


Victor




" Victor Bazarov" <五******** @ comAcast.net>在消息新闻中写道:u7 *************** @ newsread1.dllstx09.us.to.ve rio.net ...

Alex Vinokur写道:< blockquote class =post_quotes> ---------
#include< vector>
使用命名空间std;
struct Foo
{
Foo (int){}
};
int main()
{
vector< Foo> v1;
v1.resize(1);
v1 [0] = Foo(100);
//检查点
返回0;
}
---------

对于在检查点分配的内存有多少个Foo实例?
似乎是2个坚持?
如果是是真的是否意味着使用vector :: resize()涉及分配额外的内存?



该程序格式不正确,因为''Foo''不符合要求
Default-constructible"要求调整''调整大小''。它应该不会编译。



[snip]


为了简化程序,我删除了Foo(){} ;错误。


这是更新的程序。

---------

#include< vector>

使用命名空间std;

struct Foo

{

Foo(){}

Foo(int){}

};

int main()

{

vector< ;富> v1;

v1.resize(1);

v1 [0] = Foo(100);

//检查点

返回0;

}

---------

-

Alex Vinokur
http://mathforum.org/library/查看/ 10978.html
http://sourceforge.net/users / alexvn



Victor Bazarov写道:


程序很糟糕因为''Foo''不符合要求而形成
Default-constructible要求调整''调整大小''。它应该不会编译。




编译器应该发出诊断信息。没有要求它不会编译代码。


-


Pete Becker
Dinkumware,Ltd。( http://www.dinkumware.com


---------
#include <vector>
using namespace std;
struct Foo
{
Foo (int) {}
};
int main ()
{
vector<Foo> v1;
v1.resize(1);
v1[0] = Foo (100);
// Check point
return 0;
}
---------

For how many Foo instances is memory allocated at "Check point"?
It seems that for 2 insistences?
If it is truth does it mean that use of vector::resize() involves allocating extra memory?
--
Alex Vinokur
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn


解决方案

Alex Vinokur wrote:

---------
#include <vector>
using namespace std;
struct Foo
{
Foo (int) {}
};
int main ()
{
vector<Foo> v1;
v1.resize(1);
v1[0] = Foo (100);
// Check point
return 0;
}
---------

For how many Foo instances is memory allocated at "Check point"?
It seems that for 2 insistences?
If it is truth does it mean that use of vector::resize() involves allocating extra memory?



The program is ill-formed because ''Foo'' does not fit the requirement
"Default-constructible" needed for the call to ''resize''. It should
not compile.

Now, if you just missed that thing, and actually didn''t mean to ask
a trick question (and supposedly ''Foo'' actually has a default c-tor),
then it is unspecified how many objects ''v1'' can actually contain
after you asked to resize it because the initial capacity of it is 0
and ''resize'' _will_ cause reallocation to accommodate your object.
To what size the allocation happens is unspecified (implementation-
defined, I believe).

So, after ''Foo'' is given a default c-tor, the answer to your first
question is "unknown" or "implementation-defined", the answer to your
second question is "I don''t understand", and the answer to your third
question is "yes, possibly".

Victor



"Victor Bazarov" <v.********@comAcast.net> wrote in message news:u7***************@newsread1.dllstx09.us.to.ve rio.net...

Alex Vinokur wrote:

---------
#include <vector>
using namespace std;
struct Foo
{
Foo (int) {}
};
int main ()
{
vector<Foo> v1;
v1.resize(1);
v1[0] = Foo (100);
// Check point
return 0;
}
---------

For how many Foo instances is memory allocated at "Check point"?
It seems that for 2 insistences?
If it is truth does it mean that use of vector::resize() involves allocating extra memory?



The program is ill-formed because ''Foo'' does not fit the requirement
"Default-constructible" needed for the call to ''resize''. It should
not compile.


[snip]

To simplify the program I removed "Foo() {}" by mistake.

Here is the updated program.
---------
#include <vector>
using namespace std;
struct Foo
{
Foo () {}
Foo (int) {}
};
int main ()
{
vector<Foo> v1;
v1.resize(1);
v1[0] = Foo (100);
// Check point
return 0;
}
---------
--
Alex Vinokur
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn



Victor Bazarov wrote:


The program is ill-formed because ''Foo'' does not fit the requirement
"Default-constructible" needed for the call to ''resize''. It should
not compile.



The compiler should issue a diagnostic. There is no requirement that it
not compile the code.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)


这篇关于vector :: resize()和内存分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆