By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,490 Members | 1,489 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,490 IT Pros & Developers. It's quick & easy.

Iterator destructor problem

P: n/a
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

Jun 19 '07 #1
Share this Question
Share on Google+
15 Replies


P: n/a
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)) ;

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.
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.
Jun 19 '07 #2

P: n/a
On Tue, 19 Jun 2007 12:34:31 +0000, vivekian wrote:
Hi,

I have this following class
[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)) ;
I'd guess you just cut this few lines out of a bigger context, since
neither 'iNode' nor 'pNode' are declared.

--
Obnoxious User
Jun 19 '07 #3

P: n/a
On Jun 19, 8:22 am, Obnoxious User <O...@127.0.0.1wrote:
On Tue, 19 Jun 2007 12:34:31 +0000, vivekian wrote:
Hi,
I have this following class
[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)) ;

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.

Jun 19 '07 #4

P: n/a
On Jun 19, 8:22 am, Obnoxious User <O...@127.0.0.1wrote:
On Tue, 19 Jun 2007 12:34:31 +0000, vivekian wrote:
Hi,
I have this following class
[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)) ;

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

--
Obnoxious User
This is the code :

#include <map>
#include <string>
#include <vector>
#include <cstdlib>
#include <iostream.h>
#include <algorithm>

class nodeInfo ;

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

class nodeInfo
{
public:
std::string meshId ;
std::vector<childInfochildren;
nodeInfo () {};
nodeInfo (const nodeInfo & n ) ;
nodeInfo & operator= (const nodeInfo&) ;
};

class NodeManager
{
private:
std::map <const std::string , struct nodeInfo _nodeList;
public:
NodeManager();
virtual ~NodeManager();

void addRoute (const std::string parentMacAddr,const std::string
childMacAddr,const std::string meshId);
} ;

NodeManager::NodeManager()
{
}

NodeManager::~NodeManager()
{
}

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 ;
}

nodeInfo::nodeInfo (const nodeInfo & n)
{
meshId = n.meshId ;
copy (n.children.begin(), n.children.end(), children.begin()) ;
}

nodeInfo & nodeInfo::operator= (const nodeInfo &n)
{
if (this != &n)
{
meshId = n.meshId;
copy (n.children.begin(), n.children.end(), children.begin()) ;
}
return *this ;
}

void NodeManager::addRoute(const std::string pMacAddr,const
std::string cMacAddr,const std::string meshId)
{
std::map <const std::string , struct nodeInfo>::iterator iNode =
_nodeList.find (cMacAddr) ;
if (iNode == _nodeList.end())
{
struct nodeInfo n;
n.meshId = meshId ;
_nodeList[cMacAddr] = n;
cout << "\nAdding " << n.meshId ;
}
else
{
//delete previous lineage
//deleteRoute ( iNode -first , iNode -second.meshId ) ;
iNode -second.meshId = meshId ;
}

std::map <const std::string , struct nodeInfo>::iterator pNode =
_nodeList.find (pMacAddr);
if (pNode != _nodeList.end())
{
cout << "\nAdding link to Parent " << pMacAddr ;
childInfo c ;
c.relativeMeshId = atoi (meshId.substr(meshId.length(),1).c_str()) ;
c.iChild = iNode ;
(*pNode).second.children.push_back ((c)) ;
}
}

Jun 20 '07 #5

P: n/a


On Jun 19, 9:21 pm, vivekian <vivekli...@gmail.comwrote:
On Jun 19, 8:22 am, Obnoxious User <O...@127.0.0.1wrote:
On Tue, 19 Jun 2007 12:34:31 +0000, vivekian wrote:
Hi,
I have this following class
[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)) ;
I'd guess you just cut this few lines out of a bigger context, since
neither 'iNode' nor 'pNode' are declared.
--
Obnoxious User

This is the code :

#include <map>
#include <string>
#include <vector>
#include <cstdlib>
#include <iostream.h>
#include <algorithm>

class nodeInfo ;

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

};

class nodeInfo
{
public:
std::string meshId ;
std::vector<childInfochildren;
nodeInfo () {};
nodeInfo (const nodeInfo & n ) ;
nodeInfo & operator= (const nodeInfo&) ;

};

class NodeManager
{
private:
std::map <const std::string , struct nodeInfo _nodeList;
public:
NodeManager();
virtual ~NodeManager();

void addRoute (const std::string parentMacAddr,const std::string
childMacAddr,const std::string meshId);

} ;

NodeManager::NodeManager()
{

}

NodeManager::~NodeManager()
{

}

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 ;

}

nodeInfo::nodeInfo (const nodeInfo & n)
{
meshId = n.meshId ;
copy (n.children.begin(), n.children.end(), children.begin()) ;

}

nodeInfo & nodeInfo::operator= (const nodeInfo &n)
{
if (this != &n)
{
meshId = n.meshId;
copy (n.children.begin(), n.children.end(), children.begin()) ;
}
return *this ;

}

void NodeManager::addRoute(const std::string pMacAddr,const
std::string cMacAddr,const std::string meshId)
{
std::map <const std::string , struct nodeInfo>::iteratoriNode =
_nodeList.find (cMacAddr) ;
if (iNode == _nodeList.end())
{
struct nodeInfo n;
n.meshId = meshId ;
_nodeList[cMacAddr] = n;
cout << "\nAdding " << n.meshId ;
}
else
{
//delete previous lineage
//deleteRoute ( iNode -first , iNode -second.meshId ) ;
iNode -second.meshId = meshId ;
}

std::map <const std::string , struct nodeInfo>::iteratorpNode =
_nodeList.find (pMacAddr);
if (pNode != _nodeList.end())
{
cout << "\nAdding link to Parent " << pMacAddr ;
childInfo c ;
c.relativeMeshId = atoi (meshId.substr(meshId.length(),1).c_str()) ;
c.iChild = iNode ;
(*pNode).second.children.push_back ((c)) ;
}

}
Can someone help me out on this one ?
Thanks in Advance,
Vivekian

Jun 25 '07 #6

P: n/a
Some corrections below

vivekian wrote:
vivekian wrote:
On Jun 19, 8:22 am, Obnoxious User <O...@127.0.0.1wrote:
>On Tue, 19 Jun 2007 12:34:31 +0000, vivekian wrote:
>>Hi,
I have this following class
[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)) ;
I'd guess you just cut this few lines out of a bigger context, since
neither 'iNode' nor 'pNode' are declared.

--
Obnoxious User

This is the code :

#include <map>
#include <string>
#include <vector>
#include <cstdlib>
#include <iostream.h>
#include <algorithm>

class nodeInfo ;

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

class nodeInfo
{
public:
std::string meshId ;
std::vector<childInfochildren;
nodeInfo () {};
nodeInfo (const nodeInfo & n ) ;
nodeInfo & operator= (const nodeInfo&) ;
};

class NodeManager
{
private:
std::map <const std::string , struct nodeInfo _nodeList;
public:
NodeManager();
virtual ~NodeManager();

void addRoute (const std::string parentMacAddr,const std::string
childMacAddr,const std::string meshId);
} ;

NodeManager::NodeManager()
{
}

NodeManager::~NodeManager()
{
}

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 ;
}

nodeInfo::nodeInfo (const nodeInfo & n)
{
meshId = n.meshId ;
copy (n.children.begin(), n.children.end(), children.begin()) ;
}

nodeInfo & nodeInfo::operator= (const nodeInfo &n)
{
if (this != &n)
{
meshId = n.meshId;
copy (n.children.begin(), n.children.end(), children.begin()) ;
}
return *this ;
}

void NodeManager::addRoute(const std::string pMacAddr,const
std::string cMacAddr,const std::string meshId)
{
std::map <const std::string , struct nodeInfo>::iterator iNode =
_nodeList.find (cMacAddr) ;
if (iNode == _nodeList.end())
{
struct nodeInfo n;
n.meshId = meshId ;
_nodeList[cMacAddr] = n;
cout << "\nAdding " << n.meshId ;
}
else
{
//delete previous lineage
//deleteRoute ( iNode -first , iNode -second.meshId ) ;
iNode -second.meshId = meshId ;
}

std::map <const std::string , struct nodeInfo>::iterator pNode =
_nodeList.find (pMacAddr);
if (pNode != _nodeList.end())
{
cout << "\nAdding link to Parent " << pMacAddr ;
childInfo c ;
c.relativeMeshId = atoi (meshId.substr(meshId.length(),1).c_str()) ;
c.iChild = iNode ;
(*pNode).second.children.push_back ((c)) ;
}
}
On Jun 19, 8:22 am, Obnoxious User <O...@127.0.0.1wrote:
>On Tue, 19 Jun 2007 12:34:31 +0000, vivekian wrote:
>>Hi,
I have this following class
[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)) ;
I'd guess you just cut this few lines out of a bigger context, since
neither 'iNode' nor 'pNode' are declared.

--
Obnoxious User

This is the code :

#include <map>
#include <string>
#include <vector>
#include <cstdlib>
#include <iostream.h>
#include <algorithm>

class nodeInfo ;

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

class nodeInfo
{
public:
std::string meshId ;
std::vector<childInfochildren;
nodeInfo () {};
nodeInfo (const nodeInfo & n ) ;
nodeInfo & operator= (const nodeInfo&) ;
};

class NodeManager
{
private:
std::map <const std::string , struct nodeInfo _nodeList;
public:
NodeManager();
virtual ~NodeManager();

void addRoute (const std::string parentMacAddr,const std::string
childMacAddr,const std::string meshId);
} ;

NodeManager::NodeManager()
{
}

NodeManager::~NodeManager()
{
}

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 ;
}

nodeInfo::nodeInfo (const nodeInfo & n)
{
meshId = n.meshId ;
copy (n.children.begin(), n.children.end(), children.begin()) ;
}

nodeInfo & nodeInfo::operator= (const nodeInfo &n)
{
if (this != &n)
{
meshId = n.meshId;
copy (n.children.begin(), n.children.end(), children.begin()) ;
}
return *this ;
}

void NodeManager::addRoute(const std::string pMacAddr,const
std::string cMacAddr,const std::string meshId)
{
std::map <const std::string , struct nodeInfo>::iterator iNode =
_nodeList.find (cMacAddr) ;
if (iNode == _nodeList.end())
{
struct nodeInfo n;
n.meshId = meshId ;
_nodeList[cMacAddr] = n;
cout << "\nAdding " << n.meshId ;
}
else
{
//delete previous lineage
//deleteRoute ( iNode -first , iNode -second.meshId ) ;
iNode -second.meshId = meshId ;
}

std::map <const std::string , struct nodeInfo>::iterator pNode =
_nodeList.find (pMacAddr);
if (pNode != _nodeList.end())
{
cout << "\nAdding link to Parent " << pMacAddr ;
childInfo c ;
c.relativeMeshId = atoi (meshId.substr(meshId.length(),1).c_str()) ;
c.iChild = iNode ;
(*pNode).second.children.push_back ((c)) ;
}
}
Your code has bugs.

First error is you write assignment operators and copy constructors when
they are not needed. This is a mistake.

More seriously this copy ctor is bugged

nodeInfo::nodeInfo (const nodeInfo & n)
{
meshId = n.meshId ;
copy (n.children.begin(), n.children.end(), children.begin()) ;
}

because you fail to allocate any space for the children vector.
Ironaically if you hadn't bothered to write this copy constructor, then
there would not have been a bug. Which is my first point, don't write
copy constructors or assignment operators when they are not needed, it's
asking for trouble. Assignment operator has simialar bug. As before, get
rid, problem goes away.

Third point, this line is bugged

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

perhaps you meant

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

john
Jun 25 '07 #7

P: n/a
On Jun 25, 12:07 pm, John Harrison <john_androni...@hotmail.com>
wrote:
Some corrections below

vivekian wrote:
vivekian wrote:
On Jun 19, 8:22 am, Obnoxious User <O...@127.0.0.1wrote:
On Tue, 19 Jun 2007 12:34:31 +0000, vivekian wrote:
Hi,
I have this following class
[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)) ;
I'd guess you just cut this few lines out of a bigger context, since
neither 'iNode' nor 'pNode' are declared.
--
Obnoxious User
This is the code :
#include <map>
#include <string>
#include <vector>
#include <cstdlib>
#include <iostream.h>
#include <algorithm>
class nodeInfo ;
class childInfo
{
public:
int relativeMeshId ;
std::map <const std::string, nodeInfo >::iterator iChild ;
childInfo () {};
childInfo ( const childInfo & c ) ;
childInfo & operator= (const childInfo&) ;
};
class nodeInfo
{
public:
std::string meshId ;
std::vector<childInfochildren;
nodeInfo () {};
nodeInfo (const nodeInfo & n ) ;
nodeInfo & operator= (const nodeInfo&) ;
};
class NodeManager
{
private:
std::map <const std::string , struct nodeInfo _nodeList;
public:
NodeManager();
virtual ~NodeManager();
void addRoute (const std::string parentMacAddr,const std::string
childMacAddr,const std::string meshId);
} ;
NodeManager::NodeManager()
{
}
NodeManager::~NodeManager()
{
}
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 ;
}
nodeInfo::nodeInfo (const nodeInfo & n)
{
meshId = n.meshId ;
copy (n.children.begin(), n.children.end(), children.begin()) ;
}
nodeInfo & nodeInfo::operator= (const nodeInfo &n)
{
if (this != &n)
{
meshId = n.meshId;
copy (n.children.begin(), n.children.end(), children.begin()) ;
}
return *this ;
}
void NodeManager::addRoute(const std::string pMacAddr,const
std::string cMacAddr,const std::string meshId)
{
std::map <const std::string , struct nodeInfo>::iterator iNode =
_nodeList.find (cMacAddr) ;
if (iNode == _nodeList.end())
{
struct nodeInfo n;
n.meshId = meshId ;
_nodeList[cMacAddr] = n;
cout << "\nAdding " << n.meshId ;
}
else
{
//delete previous lineage
//deleteRoute ( iNode -first , iNode -second.meshId ) ;
iNode -second.meshId = meshId ;
}
std::map <const std::string , struct nodeInfo>::iterator pNode =
_nodeList.find (pMacAddr);
if (pNode != _nodeList.end())
{
cout << "\nAdding link to Parent " << pMacAddr ;
childInfo c ;
c.relativeMeshId = atoi (meshId.substr(meshId.length(),1).c_str()) ;
c.iChild = iNode ;
(*pNode).second.children.push_back ((c)) ;
}
}
On Jun 19, 8:22 am, Obnoxious User <O...@127.0.0.1wrote:
On Tue, 19 Jun 2007 12:34:31 +0000, vivekian wrote:
Hi,
I have this following class
[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)) ;
I'd guess you just cut this few lines out of a bigger context, since
neither 'iNode' nor 'pNode' are declared.
--
Obnoxious User
This is the code :
#include <map>
#include <string>
#include <vector>
#include <cstdlib>
#include <iostream.h>
#include <algorithm>
class nodeInfo ;
class childInfo
{
public:
int relativeMeshId ;
std::map <const std::string, nodeInfo >::iterator iChild ;
childInfo () {};
childInfo ( const childInfo & c ) ;
childInfo & operator= (const childInfo&) ;
};
class nodeInfo
{
public:
std::string meshId ;
std::vector<childInfochildren;
nodeInfo () {};
nodeInfo (const nodeInfo & n ) ;
nodeInfo & operator= (const nodeInfo&) ;
};
class NodeManager
{
private:
std::map <const std::string , struct nodeInfo _nodeList;
public:
NodeManager();
virtual ~NodeManager();
void addRoute (const std::string parentMacAddr,const std::string
childMacAddr,const std::string meshId);
} ;
NodeManager::NodeManager()
{
}
NodeManager::~NodeManager()
{
}
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 ;
}
nodeInfo::nodeInfo (const nodeInfo & n)
{
meshId = n.meshId ;
copy (n.children.begin(), n.children.end(), children.begin()) ;
}
nodeInfo & nodeInfo::operator= (const nodeInfo &n)
{
if (this != &n)
{
meshId = n.meshId;
copy (n.children.begin(), n.children.end(), children.begin()) ;
}
return *this ;
}
void NodeManager::addRoute(const std::string pMacAddr,const
std::string cMacAddr,const std::string meshId)
{
std::map <const std::string , struct nodeInfo>::iterator iNode =
_nodeList.find (cMacAddr) ;
if (iNode == _nodeList.end())
{
struct nodeInfo n;
n.meshId = meshId ;
_nodeList[cMacAddr] = n;
cout << "\nAdding " << n.meshId ;
}
else
{
//delete previous lineage
//deleteRoute ( iNode -first , iNode -second.meshId ) ;
iNode -second.meshId = meshId ;
}
std::map <const std::string , struct nodeInfo>::iterator pNode =
_nodeList.find (pMacAddr);
if (pNode != _nodeList.end())
{
cout << "\nAdding link to Parent " << pMacAddr ;
childInfo c ;
c.relativeMeshId = atoi (meshId.substr(meshId.length(),1).c_str()) ;
c.iChild = iNode ;
(*pNode).second.children.push_back ((c)) ;
}
}

Your code has bugs.

First error is you write assignment operators and copy constructors when
they are not needed. This is a mistake.

More seriously this copy ctor is bugged

nodeInfo::nodeInfo (const nodeInfo & n)
{
meshId = n.meshId ;
copy (n.children.begin(), n.children.end(), children.begin()) ;

}

because you fail to allocate any space for the children vector.
Ironaically if you hadn't bothered to write this copy constructor, then
there would not have been a bug. Which is my first point, don't write
copy constructors or assignment operators when they are not needed, it's
asking for trouble. Assignment operator has simialar bug. As before, get
rid, problem goes away.

Third point, this line is bugged

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

perhaps you meant

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

john
Yes, thats what i thought initially, there should be no need of copy
constructor or assignment operators since there is no dynamic
allocation of memory. Below is the code which is present when the copy
constructors and assignment operators are removed. The main.cpp and
the output follow it :

// NodeManager.h
//------------------
#ifndef NODEMANAGER_H_
#define NODEMANAGER_H_

#include <map>
#include <string>
#include <vector>
#include <cstdlib>
#include <iostream.h>
#include <algorithm>

class nodeInfo ;

class childInfo
{
public:
int relativeMeshId ;
std::map <std::string, nodeInfo >::iterator iChild ;
childInfo () {} ;
~childInfo () {} ;
};

class nodeInfo
{
public:
std::string meshId ;
std::vector<childInfochildren;
nodeInfo () {};
~nodeInfo () {} ;
};

class NodeManager
{
private:
std::map <std::string , struct nodeInfo _nodeList;
public:
NodeManager();
virtual ~NodeManager();

void addRoute (const std::string parentMacAddr,const std::string
childMacAddr,const std::string meshId);
void deleteRoute (const std::string childMacAddr,const std::string
meshId);
void getRoute (const std::string macAddr,const std::string
&meshId);
};

#endif /*NODEMANAGER_H_*/

//NodeManager.cpp
//---------------

#include "NodeManager.h"

NodeManager::NodeManager()
{
}

NodeManager::~NodeManager()
{
}

void NodeManager::addRoute(const std::string pMacAddr,const
std::string cMacAddr,const std::string meshId)
{
std::vector<childInfotrial ;
std::map <std::string , struct nodeInfo>::iterator iNode =
_nodeList.find (cMacAddr) ;
if (iNode == _nodeList.end())
{
struct nodeInfo n;
n.meshId = meshId ;
_nodeList[cMacAddr] = n;
cout << "\nAdding " << n.meshId ;
}
else
{
//delete previous lineage
iNode -second.meshId = meshId ;
}

std::map <std::string , struct nodeInfo>::iterator pNode =
_nodeList.find (pMacAddr);
if (pNode != _nodeList.end())
{
childInfo c ;
c.relativeMeshId = atoi
(meshId.substr(meshId.length()-1,1).c_str()) ;
c.iChild = iNode ;
(*pNode).second.children.push_back ((c)) ;
}
}

// main.cpp

#include "LibMgr/NodeManager.h"

int main ( int argc , char *argv[] )
{
NodeManager n ;
n.addRoute("" , "a" ,"11") ;
n.addRoute("" , "b" ,"12") ;
n.addRoute("" , "c" ,"13") ;
n.addRoute("a", "d" ,"111") ;
n.addRoute("a", "e" ,"112") ;
n.addRoute("b", "f" ,"121") ;
n.addRoute("c", "g" ,"131") ;
n.addRoute("c", "h" ,"132") ;
n.addRoute("c", "i" ,"133") ;
}
// Output
// ------
Adding 11
Adding 12
Adding 13
Segmentation fault

Thanks,
Vivekian

Jun 25 '07 #8

P: n/a
>
>
// Output
// ------
Adding 11
Adding 12
Adding 13
Segmentation fault

Thanks,
Vivekian
I've tried your code with two different compilers VC++ 2005 and g++
3.4.4 (on cygwin) and your program runs without problems. I can't see
any bug either.

Strange.

john
Jun 25 '07 #9

P: n/a
On Jun 25, 2:46 pm, John Harrison <john_androni...@hotmail.comwrote:
// Output
// ------
Adding 11
Adding 12
Adding 13
Segmentation fault
Thanks,
Vivekian

I've tried your code with two different compilers VC++ 2005 and g++
3.4.4 (on cygwin) and your program runs without problems. I can't see
any bug either.

Strange.

john
Its strange indeed. I am using g++ (GCC) 4.1.2 20061115 (prerelease)
(SUSE Linux).
Using gdb it seg faults at (*pNode).second.children.push_back ((c)) ;
The error is :

Program received signal SIGSEGV, Segmentation fault.
0xb7e7e4bf in std::basic_string<char, std::char_traits<char>,
std::allocator<char::basic_string ()
from /usr/lib/libstdc++.so.6

vivekian

Jun 25 '07 #10

P: n/a
On Jun 25, 4:28 pm, vivekian <vivekli...@gmail.comwrote:
On Jun 25, 2:46 pm, John Harrison <john_androni...@hotmail.comwrote:
// Output
// ------
Adding 11
Adding 12
Adding 13
Segmentation fault
Thanks,
Vivekian
I've tried your code with two different compilers VC++ 2005 and g++
3.4.4 (on cygwin) and your program runs without problems. I can't see
any bug either.
Strange.
john

Its strange indeed. I am using g++ (GCC) 4.1.2 20061115 (prerelease)
(SUSE Linux).
Using gdb it seg faults at (*pNode).second.children.push_back ((c)) ;
The error is :

Program received signal SIGSEGV, Segmentation fault.
0xb7e7e4bf in std::basic_string<char, std::char_traits<char>,
std::allocator<char::basic_string ()
from /usr/lib/libstdc++.so.6

vivekian
Could it possibly be a bug in the STL implementation ? since it runs
fine in VC++.

Thanks,
vivekian

Jun 26 '07 #11

P: n/a
vivekian wrote:
[snip]
Below is the code which is present when the copy
constructors and assignment operators are removed. The main.cpp and
the output follow it :
I wonder, how you got any output. I cannot get your code to compile.

// NodeManager.h
//------------------
#ifndef NODEMANAGER_H_
#define NODEMANAGER_H_

#include <map>
#include <string>
#include <vector>
#include <cstdlib>
#include <iostream.h>
You mean <iostream>. There is no <iostream.hin the C++ standard.
#include <algorithm>

class nodeInfo ;

class childInfo
{
public:
int relativeMeshId ;
std::map <std::string, nodeInfo >::iterator iChild ;
This is not good. At this point, nodeInfo is an incomplete type. The
standard containers require template arguments to be complete. See
[17.4.3.6]. This is, where my compiler (g++-4.1.1) barfs.

childInfo () {} ;
~childInfo () {} ;
};

class nodeInfo
{
public:
std::string meshId ;
std::vector<childInfochildren;
nodeInfo () {};
~nodeInfo () {} ;
};

class NodeManager
{
private:
std::map <std::string , struct nodeInfo _nodeList;
public:
NodeManager();
virtual ~NodeManager();

void addRoute (const std::string parentMacAddr,const std::string
childMacAddr,const std::string meshId);
void deleteRoute (const std::string childMacAddr,const std::string
meshId);
void getRoute (const std::string macAddr,const std::string
&meshId);
};

#endif /*NODEMANAGER_H_*/

//NodeManager.cpp
//---------------

#include "NodeManager.h"

NodeManager::NodeManager()
{
}

NodeManager::~NodeManager()
{
}

void NodeManager::addRoute(const std::string pMacAddr,const
std::string cMacAddr,const std::string meshId)
{
std::vector<childInfotrial ;
std::map <std::string , struct nodeInfo>::iterator iNode =
_nodeList.find (cMacAddr) ;
if (iNode == _nodeList.end())
{
struct nodeInfo n;
n.meshId = meshId ;
_nodeList[cMacAddr] = n;
cout << "\nAdding " << n.meshId ;
}
else
{
//delete previous lineage
iNode -second.meshId = meshId ;
}

std::map <std::string , struct nodeInfo>::iterator pNode =
_nodeList.find (pMacAddr);
if (pNode != _nodeList.end())
{
childInfo c ;
c.relativeMeshId = atoi
(meshId.substr(meshId.length()-1,1).c_str()) ;
c.iChild = iNode ;
(*pNode).second.children.push_back ((c)) ;
}
}

// main.cpp

#include "LibMgr/NodeManager.h"

int main ( int argc , char *argv[] )
{
NodeManager n ;
n.addRoute("" , "a" ,"11") ;
n.addRoute("" , "b" ,"12") ;
n.addRoute("" , "c" ,"13") ;
n.addRoute("a", "d" ,"111") ;
n.addRoute("a", "e" ,"112") ;
n.addRoute("b", "f" ,"121") ;
n.addRoute("c", "g" ,"131") ;
n.addRoute("c", "h" ,"132") ;
n.addRoute("c", "i" ,"133") ;
}
// Output
// ------
Adding 11
Adding 12
Adding 13
Segmentation fault

Thanks,
Vivekian
Jun 26 '07 #12

P: n/a
On Jun 26, 8:47 am, Kai-Uwe Bux <jkherci...@gmx.netwrote:
vivekian wrote:

[snip]
Below is the code which is present when the copy
constructors and assignment operators are removed. The main.cpp and
the output follow it :

I wonder, how you got any output. I cannot get your code to compile.
// NodeManager.h
//------------------
#ifndef NODEMANAGER_H_
#define NODEMANAGER_H_
#include <map>
#include <string>
#include <vector>
#include <cstdlib>
#include <iostream.h>

You mean <iostream>. There is no <iostream.hin the C++ standard.
#include <algorithm>
class nodeInfo ;
class childInfo
{
public:
int relativeMeshId ;
std::map <std::string, nodeInfo >::iterator iChild ;

This is not good. At this point, nodeInfo is an incomplete type. The
standard containers require template arguments to be complete. See
[17.4.3.6]. This is, where my compiler (g++-4.1.1) barfs.
Even if i comment out iChild as a member , which would solves the
problem which you are facing, the program still segfaults. The code
without iChild is pasted at http://rafb.net/p/VOtPGW12.html . Line 83
seems to be the culprit. BTW , what error does the compiler throw ?

vivekian

Jun 26 '07 #13

P: n/a
vivekian wrote:
On Jun 26, 8:47 am, Kai-Uwe Bux <jkherci...@gmx.netwrote:
>vivekian wrote:

[snip]
Below is the code which is present when the copy
constructors and assignment operators are removed. The main.cpp and
the output follow it :

I wonder, how you got any output. I cannot get your code to compile.
// NodeManager.h
//------------------
#ifndef NODEMANAGER_H_
#define NODEMANAGER_H_
#include <map>
#include <string>
#include <vector>
#include <cstdlib>
#include <iostream.h>

You mean <iostream>. There is no <iostream.hin the C++ standard.
#include <algorithm>
class nodeInfo ;
class childInfo
{
public:
int relativeMeshId ;
std::map <std::string, nodeInfo >::iterator iChild ;

This is not good. At this point, nodeInfo is an incomplete type. The
standard containers require template arguments to be complete. See
[17.4.3.6]. This is, where my compiler (g++-4.1.1) barfs.

Even if i comment out iChild as a member , which would solves the
problem which you are facing, the program still segfaults. The code
without iChild is pasted at http://rafb.net/p/VOtPGW12.html. Line 83
seems to be the culprit.
Interesting. For me, it runs without problems.

What compiler are you using?

BTW , what error does the compiler throw ?
It complains about the type being incomplete:

vivekian_002.cc:15: instantiated from here
/added/pkg/gcc-4.1.1/usr/lib/gcc/i686-pc-linux-gnu/4.1.1/../../../../include/c++/4.1.1/bits/boost
_concept_check.h:216: error: '__gnu_cxx::_SGIAssignableConcept<_Tp>::__a'
has incomplete type
vivekian_002.cc:10: error: forward declaration of 'struct nodeInfo'

I should remark that I built g++ with concept checks enabled.
Best

Kai-Uwe Bux
Jun 26 '07 #14

P: n/a
On Jun 26, 10:06 am, Kai-Uwe Bux <jkherci...@gmx.netwrote:
vivekian wrote:
On Jun 26, 8:47 am, Kai-Uwe Bux <jkherci...@gmx.netwrote:
vivekian wrote:
[snip]
Below is the code which is present when the copy
constructors and assignment operators are removed. The main.cpp and
the output follow it :
I wonder, how you got any output. I cannot get your code to compile.
// NodeManager.h
//------------------
#ifndef NODEMANAGER_H_
#define NODEMANAGER_H_
#include <map>
#include <string>
#include <vector>
#include <cstdlib>
#include <iostream.h>
You mean <iostream>. There is no <iostream.hin the C++ standard.
#include <algorithm>
class nodeInfo ;
class childInfo
{
public:
int relativeMeshId ;
std::map <std::string, nodeInfo >::iterator iChild ;
This is not good. At this point, nodeInfo is an incomplete type. The
standard containers require template arguments to be complete. See
[17.4.3.6]. This is, where my compiler (g++-4.1.1) barfs.
Even if i comment out iChild as a member , which would solves the
problem which you are facing, the program still segfaults. The code
without iChild is pasted athttp://rafb.net/p/VOtPGW12.html. Line 83
seems to be the culprit.

Interesting. For me, it runs without problems.

What compiler are you using?
BTW , what error does the compiler throw ?

It complains about the type being incomplete:

vivekian_002.cc:15: instantiated from here
/added/pkg/gcc-4.1.1/usr/lib/gcc/i686-pc-linux-gnu/4.1.1/../../../../include/c++/4.1.1/bits/boost
_concept_check.h:216: error: '__gnu_cxx::_SGIAssignableConcept<_Tp>::__a'
has incomplete type
vivekian_002.cc:10: error: forward declaration of 'struct nodeInfo'

I should remark that I built g++ with concept checks enabled.

Best

Kai-Uwe Bux
My bad. The problem was a namespace conflict. I was using struct
childInfo in another file in the project which had different fields.
When the vector children had to allocate memory it would pick up that
definition, hence seg faulting. Thanks for all the help and patience.

vivekian

Jun 26 '07 #15

P: n/a
vivekian wrote:
>
Could it possibly be a bug in the STL implementation ? since it runs
fine in VC++.

Thanks,
vivekian
\
No, John is right. Your program is incorrect.

One of the insidious forms of undefined behavior is
appearing to work normally on some days and not on others.
Jun 27 '07 #16

This discussion thread is closed

Replies have been disabled for this discussion.