结构总是在堆栈上? [英] structs ALWAYS on the stack?

查看:85
本文介绍了结构总是在堆栈上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被告知,作为价值类型的结构总是存在于

堆栈中,因此不需要手动删除,但是简单的超出范围

在适当的时候。


我还读到所有C ++ * new *关键字必须与

最终* delete *结合才能再次释放memmory。


我的问题是,如果我使用新的

关键字创建结构的实例,上述哪一项是正确的?


示例:


struct tree {

struct tree * parent;

struct tree * child;

String itemData;

};


tree * rootNode = new tree;

tree * subNode =新树;


rootNode-> child = subNode;

subNode-> parent = rootNode;

.. ..etc ...


提前致谢,

Casper

解决方案

< blockquote>

" Casper" < CA **** @ jbr.dk>在消息中写道

news:sx ******************** @ wagner.videotron.net ..。

我被告知,作为价值类型的结构总是存在于
堆栈中,因此不需要手动删除,而是在适当的时候简单地超出范围。
我还读过所有C ++ * new *关键字必须与
最终* delete *结合才能再次释放memmory。


这是正确的。

我的问题是,如果我使用新的
关键字创建结构的实例,以上是哪一个对吗?




你必须打电话给删除,否则你会发生内存泄漏。


-Sharad


Casper写道:

我被告知结构,值类型,
总是驻留在堆栈上,因此,
不需要手动删除,但很容易在适当的时候超出范围。

我还读过所有C ++ * new *关键字
必须与最终*删除相结合*再次释放memmory。

我的问题是,如果我使用新的
关键字创建结构的实例,上述哪一项是正确的?
示例:

struct tree {
struct tree * parent;
struct tree * child;
String itemData;
};

tree * prootNod e =新树;
树* psubNode =新树;

prootNode-> child = psubNode;
psubNode-> parent = prootNode;
// ...等...




struct tree rootNode;

struct tree subNode;


rootNode.child =& subNode;

subNode.parent =& rootNode;


rootNode和subNode都会被自动销毁

当执行的线程超出范围时

,其中定义了rootNode和subNode。


E. Robert Tisdale写道:

struct tree rootNode;
struct tree subNode;

rootNode.child =& subNode;
subNode。 parent =& rootNode;

当执行的线程超出范围时,rootNode和subNode都会被自动销毁
,其中定义了rootNode和subNode。




如果我的rootNode是全局定义的,那么我有一个算法

,它递归地将节点挂载到我的rootNode。这是否意味着所有其他的

比我的rootNode在从我的mount-method返回之后存在时才会存在?


我想要做的是创建一个我可以从

辅助数据结构引用节点的树。在我看来,我不需要方法,所以结构似乎最有效。


问候,

Casper


I''ve been told that structs, being value types, always reside on the
stack and thus need not be deleted manually but simple go out of scope
in due time.

I have also read that all C++ *new* keywords must be coupled with an
eventual *delete* to free the memmory again.

My question then is, if I create instances of a struct with the new
keyword, which of the above is correct?

Example:

struct tree {
struct tree *parent;
struct tree *child;
String itemData;
};

tree* rootNode = new tree;
tree* subNode = new tree;

rootNode->child = subNode;
subNode->parent = rootNode;
....etc...

Thanks in advance,
Casper

解决方案


"Casper" <ca****@jbr.dk> wrote in message
news:sx********************@wagner.videotron.net.. .

I''ve been told that structs, being value types, always reside on the
stack and thus need not be deleted manually but simple go out of scope
in due time. I have also read that all C++ *new* keywords must be coupled with an
eventual *delete* to free the memmory again.
This is correct.
My question then is, if I create instances of a struct with the new
keyword, which of the above is correct?



You have to call delete else you do a memory leak.

-Sharad


Casper wrote:

I''ve been told that structs, being value types,
always reside on the stack and, thus,
need not be deleted manually but simple go out of scope in due time.

I have also read that all C++ *new* keywords
must be coupled with an eventual *delete* to free the memmory again.

My question then is, if I create instances of a struct with the new
keyword, which of the above is correct?

Example:

struct tree {
struct tree *parent;
struct tree *child;
String itemData;
};

tree* prootNode = new tree;
tree* psubNode = new tree;

prootNode->child = psubNode;
psubNode->parent = prootNode;
// ...etc...



struct tree rootNode;
struct tree subNode;

rootNode.child = &subNode;
subNode.parent = &rootNode;

Both rootNode and subNode are automatically destroyed
when the thread of execution passes out of the scope
where rootNode and subNode were defined.


E. Robert Tisdale wrote:

struct tree rootNode;
struct tree subNode;

rootNode.child = &subNode;
subNode.parent = &rootNode;

Both rootNode and subNode are automatically destroyed
when the thread of execution passes out of the scope
where rootNode and subNode were defined.



What then if my rootNode is defined globally and I have an algorithm
which recursively mounts nodes to my rootNode. Does this mean all other
than my rootNode seizes to exist after returning from my "mount-method"?

What I want to do is create a tree which I can reference nodes to from a
secondary data structure. I need not methods so structs seemed most
effecient in my view.

Regards,
Casper


这篇关于结构总是在堆栈上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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