迭代器析构函数问题 [英] Iterator destructor problem

查看:91
本文介绍了迭代器析构函数问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有以下课程


class nodeInfo;


class childInfo

{

public:

int relativeMeshId;

std :: map< const std :: string ,nodeInfo> :: iterator iChild;

childInfo(){};

childInfo(const childInfo& c);

childInfo& ; operator =(const childInfo&);

};


childInfo :: childInfo(const childInfo& c)

{

relativeMeshId = c.relativeMeshId;

iChild = c.iChild;

}


childInfo &安培; childInfo :: operator =(const childInfo& c)

{

if(this!=& c)

{

relativeMeshId = c.relativeMeshId;

iChild = c.iChild;

}

返回* this;

}

这是使用此类的代码片段:


struct childInfo c;

c。 relativeMeshId = atoi(meshId.substr(meshId.length(),1).c_str());

c.iChild = iNode;

(* pNode).second .children.push_back((c));


当''c'超出范围时,我会遇到分段错误。由于没有分配动态内存,因此没有声明显式析构函数。

但这就是seg错误发生的地方。可以做什么来

防止这个错误。


提前谢谢,

vivekian

Hi,

I have this following class

class nodeInfo ;

class childInfo
{
public:
int relativeMeshId ;
std::map <const std::string, nodeInfo >::iterator iChild ;
childInfo () {};
childInfo ( const childInfo & c ) ;
childInfo & operator= (const childInfo&) ;
};

childInfo::childInfo ( const childInfo & c )
{
relativeMeshId = c.relativeMeshId ;
iChild = c.iChild ;
}

childInfo & childInfo::operator= (const childInfo & c)
{
if ( this != &c )
{
relativeMeshId = c.relativeMeshId ;
iChild = c.iChild ;
}
return *this ;
}
This is a snippet of code which uses this class :

struct childInfo c ;
c.relativeMeshId = atoi (meshId.substr(meshId.length(),1).c_str()) ;
c.iChild = iNode ;
(*pNode).second.children.push_back ((c)) ;

When ''c'' goes out of scope , i get a segmentation fault. There is no
explicit destructor declared since no dynamic memory was allocated.
But that is the point the seg fault occurs. What can be done to
prevent this error.

Thanks in advance,
vivekian

推荐答案

2007年6月19日星期二12:34:31 -0000 in comp.lang.c ++,vivekian

< vi * *******@gmail.comwrote,
On Tue, 19 Jun 2007 12:34:31 -0000 in comp.lang.c++, vivekian
<vi********@gmail.comwrote,

struct childInfo c;
c.relativeMeshId = atoi(meshId.substr(meshId.length( ),1).c_str());
c.iChild = iNode;
(* pNode).second.children.push_back((c));

何时' 'c'超出范围,我得到分段错误。由于没有分配动态内存,因此没有声明显式析构函数。
但这就是seg错误发生的重点。可以做些什么来防止这个错误。
struct childInfo c ;
c.relativeMeshId = atoi (meshId.substr(meshId.length(),1).c_str()) ;
c.iChild = iNode ;
(*pNode).second.children.push_back ((c)) ;

When ''c'' goes out of scope , i get a segmentation fault. There is no
explicit destructor declared since no dynamic memory was allocated.
But that is the point the seg fault occurs. What can be done to
prevent this error.



我会在某些调试器中单步调试代码,看看

实际发生了什么。如果childInfo只包含一个int和

一个迭代器,那么实际上应该没有执行任何代码来销毁

一个。也许在该范围的最后发生了其他事情

而不仅仅是c的结尾。


如果内存被解除分配(可能由于某种原因无关) c)

通常可能是由于其他地方的某些代码导致的堆损坏而变得明显的点。

I would be stepping through the code in some debugger, looking to see
what is actually happening there. If childInfo contains only an int and
an iterator then there should actually be no code executed to destroy
one. Perhaps there are other things happening at the end of that scope
than just the end of c.

If memory is being deallocated (perhaps for some reason unrelated to c)
that can often be the point where damage to the heap, that may have been
caused by some code elsewhere, becomes apparent.


2007年6月19日星期二12:34:31 +0000,vivekian写道:
On Tue, 19 Jun 2007 12:34:31 +0000, vivekian wrote:




我有以下课程
Hi,

I have this following class



[snip]

[snip]


>


这是使用此类的代码片段:


struct childInfo c;

c.relativeMeshId = atoi (meshId.substr(meshId.length(),1).c_str());

c.iChild = iNode;

(* pNode).second.children。 push_back((c));
>

This is a snippet of code which uses this class :

struct childInfo c ;
c.relativeMeshId = atoi (meshId.substr(meshId.length(),1).c_str()) ;
c.iChild = iNode ;
(*pNode).second.children.push_back ((c)) ;



我猜你只是从更大的上下文中删除了这几行,因为

既没有声明''iNode'也没有'''pNode''。


-

令人讨厌的用户

I''d guess you just cut this few lines out of a bigger context, since
neither ''iNode'' nor ''pNode'' are declared.

--
Obnoxious User


6月19日上午8:22,令人讨厌的用户< O ... @ 127.0.0.1wrote:
On Jun 19, 8:22 am, Obnoxious User <O...@127.0.0.1wrote:

On Tue ,2007年6月19日12:34:31 +0000,vivekian写道:
On Tue, 19 Jun 2007 12:34:31 +0000, vivekian wrote:


Hi,


我有以下课程
I have this following class



[snip]

[snip]


这是一段代码,使用此类:
This is a snippet of code which uses this class :


struct childInfo c;

c.relativeMeshId = atoi(meshId.substr(meshId.length( ),1).c_str());

c.iChild = iNode;

(* pNode).second.children.push_back((c));
struct childInfo c ;
c.relativeMeshId = atoi (meshId.substr(meshId.length(),1).c_str()) ;
c.iChild = iNode ;
(*pNode).second.children.push_back ((c)) ;



我猜你只是从更大的背景中删除了这几行,因为

既不是iNode也不是'声明'pNode''。


-

令人讨厌的用户


I''d guess you just cut this few lines out of a bigger context, since
neither ''iNode'' nor ''pNode'' are declared.

--
Obnoxious User



它''最好发布一个可编辑的代码,以帮助人们给你一个快速的回复。

It''s always better to post a compilable code to help folks give you a
quick reply.


这篇关于迭代器析构函数问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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