静态&动态分配问题 [英] static & dynamic allocation problem

查看:81
本文介绍了静态&动态分配问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我对静态内存分配几乎没有什么困惑。动态

分配给一个cluss会员。


我有类似

班级酒吧{

公开:

显式栏(){

cout<<" bar default"<< endl;

}

显式Bar(int x){

cout<<" bar int"<< x<< endl;

}

~Bar(){

cout<<" bar dtor"<< endl;

}

};

class Foo {

私人:

酒吧;

公开:

显式Foo(){

cout<<"" foo default"<< endl;

}

显式Foo(int x):bar(Bar(10)){

cout<<"" foo int"<< x<< endl;

}

~Foo(){

//删除栏;

cout<<" foo dtor"<< endl;

}

};


写Foo类的替代方法,


class Foo {

private:

Bar * ba r;

public:

显式Foo():bar(new Bar()){

cout<<"" foo default" << endl;

}

显式Foo(int x):bar(新条形码(10)){

cout<< ;foo int<< x<< endl;

}

~Foo(){

删除栏;

cout<<"" foo dtor"<< endl;

}

};


哪个Foo类会用到什么时候?大多数GUI库(比如

wxWindows和Qt)我发现第二种是使用(.ie动态

分配)。

注意,在这种情况下,bar对象的生命周期与Foo'相同,即它在Foo构造函数和& ;;创建的
。在Foo的析构函数中被摧毁。我知道

如果Bar要初始化任何其他地方Foo's ctor&摧毁了除了Foo'dtor之外的
,第二种方法是唯一的方法。但如果不是这样的话,那么哪一个更好?第一次实现Bar的虚拟

函数有什么问题吗?

此外,

如果我初始化,

Foo foo = Foo(4);
第一个sizeof(foo)的
是1,而第二个是$ 4.为什么会这样?

另外内存如何第一个和第二个案例的布局有所不同吗?


提前感谢


abir

Hi,
I have little confusion about static memory allocation & dynamic
allocation for a cluss member.

I have class like
class Bar{
public:
explicit Bar(){
cout<<"bar default"<<endl;
}
explicit Bar(int x){
cout<<"bar int "<<x<<endl;
}
~Bar(){
cout<<"bar dtor"<<endl;
}
};
class Foo{
private:
Bar bar;
public:
explicit Foo(){
cout<<"foo default"<<endl;
}
explicit Foo(int x) : bar(Bar(10)){
cout<<"foo int "<<x<<endl;
}
~Foo(){
//delete bar;
cout<<"foo dtor"<<endl;
}
};

alternative way to write class Foo as,

class Foo{
private:
Bar* bar;
public:
explicit Foo() : bar(new Bar()){
cout<<"foo default"<<endl;
}
explicit Foo(int x) : bar(new Bar(10)){
cout<<"foo int "<<x<<endl;
}
~Foo(){
delete bar;
cout<<"foo dtor"<<endl;
}
};

which Foo class is to be used when? Most of the GUI library (like
wxWindows and Qt) I find the second kind is used (.i.e dynamic
allocation).
Note, in this case the bar objects lifetime is same as Foo''s, i.e its
created at Foo constructor & destroyed at Foo''s destructor. I know that
if Bar to be initializd any other place that Foo''s ctor & destroyed
other than Foo''s dtor, the second method is the only way. But if that
is not the case, which one is preferred? any problem wth the virtual
function of Bar for the first implementation?
moreover,
if I initialize,
Foo foo = Foo(4);
for the first sizeof(foo) is 1, while for second it is 4. why is so?
Also how the memory layout is different between first and second case?

thanks in advance

abir

推荐答案



toton ???é?????

toton ???é?????



我对静态内存分配几乎没有什么困惑。动态

分配给一个cluss会员。


我有类似

班级酒吧{

公开:

显式栏(){

cout<<" bar default"<< endl;

}

显式Bar(int x){

cout<<" bar int"<< x<< endl;

}

~Bar(){

cout<<" bar dtor"<< endl;

}

};


类Foo {

私人:

酒吧;

public:

显式Foo(){

cout<<"" foo default"<< endl;

}

显式Foo(int x):bar(Bar(10)){

cout<<"" foo int"<< x<< endl;

}

~Foo(){

//删除栏;

cout<<"" foo dtor"< < endl;

}

};


写Foo类的替代方法,


class Foo {

私人:

酒吧*酒吧;

公开:

显式Foo():bar(新Bar()){

cout<<" foo default"<< endl;

}

显式Foo(int x):bar(新栏(10)){

cout<<"" foo int"<< x<< endl;

}

~Foo(){

删除栏;

cout<<" foo dtor"<< endl;

}

};


Foo类何时使用?大多数GUI库(比如

wxWindows和Qt)我发现第二种是使用(.ie动态

分配)。

注意,在这种情况下,bar对象的生命周期与Foo'相同,即它在Foo构造函数和& ;;创建的
。在Foo的析构函数中被摧毁。我知道

如果Bar要初始化任何其他地方Foo's ctor&摧毁了除了Foo'dtor之外的
,第二种方法是唯一的方法。但如果不是这样的话,那么哪一个更好?第一次实现Bar的虚拟

函数有什么问题吗?

此外,

如果我初始化,

Foo foo = Foo(4);
第一个sizeof(foo)的
是1,而第二个是$ 4.为什么会这样?

另外内存如何第一和第二种情况的布局不同?


提前感谢


abir
Hi,
I have little confusion about static memory allocation & dynamic
allocation for a cluss member.

I have class like
class Bar{
public:
explicit Bar(){
cout<<"bar default"<<endl;
}
explicit Bar(int x){
cout<<"bar int "<<x<<endl;
}
~Bar(){
cout<<"bar dtor"<<endl;
}
};
class Foo{
private:
Bar bar;
public:
explicit Foo(){
cout<<"foo default"<<endl;
}
explicit Foo(int x) : bar(Bar(10)){
cout<<"foo int "<<x<<endl;
}
~Foo(){
//delete bar;
cout<<"foo dtor"<<endl;
}
};

alternative way to write class Foo as,

class Foo{
private:
Bar* bar;
public:
explicit Foo() : bar(new Bar()){
cout<<"foo default"<<endl;
}
explicit Foo(int x) : bar(new Bar(10)){
cout<<"foo int "<<x<<endl;
}
~Foo(){
delete bar;
cout<<"foo dtor"<<endl;
}
};

which Foo class is to be used when? Most of the GUI library (like
wxWindows and Qt) I find the second kind is used (.i.e dynamic
allocation).
Note, in this case the bar objects lifetime is same as Foo''s, i.e its
created at Foo constructor & destroyed at Foo''s destructor. I know that
if Bar to be initializd any other place that Foo''s ctor & destroyed
other than Foo''s dtor, the second method is the only way. But if that
is not the case, which one is preferred? any problem wth the virtual
function of Bar for the first implementation?
moreover,
if I initialize,
Foo foo = Foo(4);
for the first sizeof(foo) is 1, while for second it is 4. why is so?
Also how the memory layout is different between first and second case?

thanks in advance

abir



您可以尝试:

class temp

{

public:

temp(void){}

~temp(void){}

};


sizeof(temp)= 1;


class temp1

{

public:

temp1(void){}

~ temp1(void){}

private:

temp t;

};


sizeof (temp1)= 1;


class temp2

{

public:

temp2(void ){}

~temp2(void){}

private:

temp * t;

};


sizeof(temp2)= 4;

You can try:
class temp
{
public:
temp(void) {}
~temp(void) {}
};

sizeof(temp) = 1;

class temp1
{
public:
temp1(void) {}
~temp1(void) {}
private:
temp t;
};

sizeof(temp1) = 1;

class temp2
{
public:
temp2(void) {}
~temp2(void) {}
private:
temp *t;
};

sizeof(temp2) = 4;


"如果我初始化,

Foo foo = Foo(4);

为第一个sizeof(foo) )是1,而第二个是4.为什么会这样?


第一个sizeof(foo)== 1,因为出于某种原因,当c ++编译

遇到没有成员变量的类时,意味着sizeof (myClass)== 0,它将
伪装成myClass,使其大于0.即

你将永远不会得到sizeof(myClass)=我想我已经从C ++ Primier读过这个




下一个sizeof(foo)实际上是Bar *的大小。

toton写道:
"if I initialize,
Foo foo = Foo(4);
for the first sizeof(foo) is 1, while for second it is 4. why is so? "

The first sizeof(foo) == 1, because for some reason, when c++ compile
meet a class with no member variable, means sizeof(myClass) == 0, it
will fake something into myClass, to make it larger than 0. That is,
You will never get a sizeof(myClass) == 0. I think I have read this
from C++ Primier.

The next sizeof(foo) is actually the size of Bar*.
toton wrote:



我对静态内存分配和放大器几乎没什么困惑。动态

分配给一个cluss会员。


我有类似

班级酒吧{

公开:

显式栏(){

cout<<" bar default"<< endl;

}

显式Bar(int x){

cout<<" bar int"<< x<< endl;

}

~Bar(){

cout<<" bar dtor"<< endl;

}

};
Hi,
I have little confusion about static memory allocation & dynamic
allocation for a cluss member.

I have class like
class Bar{
public:
explicit Bar(){
cout<<"bar default"<<endl;
}
explicit Bar(int x){
cout<<"bar int "<<x<<endl;
}
~Bar(){
cout<<"bar dtor"<<endl;
}
};



我们***** @ gmail .com 写道:

toton ???é?????
toton ???é?????



我对静态内存分配没什么困惑。动态

分配给一个cluss会员。


我有类似

班级酒吧{

公开:

显式栏(){

cout<<" bar default"<< endl;

}

显式Bar(int x){

cout<<" bar int"<< x<< endl;

}

~Bar(){

cout<<" bar dtor"<< endl;

}

};

class Foo {

私人:

酒吧;

公开:

显式Foo(){

cout<<"" foo default"<< endl;

}

显式Foo(int x):bar(Bar(10)){

cout<<"" foo int"<< x<< endl;

}

~Foo(){

//删除栏;

cout<<" foo dtor"< ;< endl;

}

};


写Foo类的替代方法,


class Foo {

private:

Bar * bar;

public:

explicit Foo():bar(新栏()){

cout<<"" foo default"<< endl;

}

explicit Foo(int x):bar(new Bar(10)){

cout<<"" foo int"<< x<< endl;

}

~Foo(){

删除栏;

cout<<"" foo dtor"<<结束;

}

};


哪个Foo类要用?大多数GUI库(比如

wxWindows和Qt)我发现第二种是使用(.ie动态

分配)。

注意,在这种情况下,bar对象的生命周期与Foo'相同,即它在Foo构造函数和& ;;创建的
。在Foo的析构函数中被摧毁。我知道

如果Bar要初始化任何其他地方Foo's ctor&摧毁了除了Foo'dtor之外的
,第二种方法是唯一的方法。但如果不是这样的话,那么哪一个更好?第一次实现Bar的虚拟

函数有什么问题吗?

此外,

如果我初始化,

Foo foo = Foo(4);
第一个sizeof(foo)的
是1,而第二个是$ 4.为什么会这样?

另外内存如何第一个和第二个案例的布局不同?


提前感谢


abir
Hi,
I have little confusion about static memory allocation & dynamic
allocation for a cluss member.

I have class like
class Bar{
public:
explicit Bar(){
cout<<"bar default"<<endl;
}
explicit Bar(int x){
cout<<"bar int "<<x<<endl;
}
~Bar(){
cout<<"bar dtor"<<endl;
}
};
class Foo{
private:
Bar bar;
public:
explicit Foo(){
cout<<"foo default"<<endl;
}
explicit Foo(int x) : bar(Bar(10)){
cout<<"foo int "<<x<<endl;
}
~Foo(){
//delete bar;
cout<<"foo dtor"<<endl;
}
};

alternative way to write class Foo as,

class Foo{
private:
Bar* bar;
public:
explicit Foo() : bar(new Bar()){
cout<<"foo default"<<endl;
}
explicit Foo(int x) : bar(new Bar(10)){
cout<<"foo int "<<x<<endl;
}
~Foo(){
delete bar;
cout<<"foo dtor"<<endl;
}
};

which Foo class is to be used when? Most of the GUI library (like
wxWindows and Qt) I find the second kind is used (.i.e dynamic
allocation).
Note, in this case the bar objects lifetime is same as Foo''s, i.e its
created at Foo constructor & destroyed at Foo''s destructor. I know that
if Bar to be initializd any other place that Foo''s ctor & destroyed
other than Foo''s dtor, the second method is the only way. But if that
is not the case, which one is preferred? any problem wth the virtual
function of Bar for the first implementation?
moreover,
if I initialize,
Foo foo = Foo(4);
for the first sizeof(foo) is 1, while for second it is 4. why is so?
Also how the memory layout is different between first and second case?

thanks in advance

abir



您可以尝试:

class temp

{

public:

temp(void){}

~temp(void){}

};


sizeof(temp)= 1;


class temp1

{

public:

temp1(void){}

~ temp1(void){}

private:

temp t;

};


sizeof (temp1)= 1;


class temp2

{

public:

temp2(void ){}

~temp2(void){}

private:

temp * t;

};


sizeof(temp2)= 4;


You can try:
class temp
{
public:
temp(void) {}
~temp(void) {}
};

sizeof(temp) = 1;

class temp1
{
public:
temp1(void) {}
~temp1(void) {}
private:
temp t;
};

sizeof(temp1) = 1;

class temp2
{
public:
temp2(void) {}
~temp2(void) {}
private:
temp *t;
};

sizeof(temp2) = 4;



我理解为什么sizeof(temp2)是4. 4字节是32

位平台的指针大小。我不明白为什么sizeof(temp1)是1.它

应为零!是不是只遵循规格sizeof(char)

总是1和最小尺寸的东西?


不止于此,我的问题是什么时候到使用哪种风格,即。何时使用

Bar * bar,当Bar bar作为类成员变量时。虚拟功能和内存分配的后果是什么?


谢谢

I understand why sizeof(temp2) is 4. 4 byte is the pointer size for 32
bit platform. What I don''t understand is why sizeof(temp1) is 1. It
should be zero! Is it just to follow the spec that sizeof(char) is
always 1 and minimum size for anything?

More than that, my question is when to use which style, ie. when to use
Bar* bar, and when Bar bar as a class member variable. and what are the
consequences for virtual function and memory allocation.

thanks


这篇关于静态&amp;动态分配问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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