473,320 Members | 1,900 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Iterator destructor problem

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
15 4121
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
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
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
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


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
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
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
>
>
// 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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Rex_chaos | last post by:
Hi there, I am writing an iterator for a container. Just like a typical iterator, template <typename ADT> class MyIter { ... MyContainer<ADT>& refc; public:
9
by: Alexander Stippler | last post by:
Hi, I've got trouble with some well known issue. Iterator invalidation. My situation: for (it=v.begin(); it!=v.end(); ++it) { f(*it); } Under some circumstances, f may alter the container...
26
by: Michael Klatt | last post by:
I am trying to write an iterator for a std::set that allows the iterator target to be modified. Here is some relvant code: template <class Set> // Set is an instance of std::set<> class...
5
by: Ernst Murnleitner | last post by:
Hello, is it possible to derive from std::vector and derive also its iterator? If I do it like in the example below, I get a problem when I need the begin of the vector: begin() returns the...
13
by: jois.de.vivre | last post by:
Hi All, I'm trying to write a wrapper class for std::vector to extend some of its functionality. The problem I'm getting into is returning an iterator type from a member function. Here is the...
7
by: Thomas | last post by:
I am compiling with g++ the fol. class: template<typename E> class C_vector_ : public std::vector<E> { private:
7
by: Max Odendahl | last post by:
Hi, my own declared class has a stl::list as a member variable. I now need access to those from the outside. Is a custom iterator for my class the best solution for this and how to do this? I...
1
by: Scott Gifford | last post by:
Hello, I'm working on an providing an iterator interface to a database. The basic thing I'm trying to accomplish is to have my iterator read rows from the database and return constructed...
7
by: wangxiaohu | last post by:
I am trying to write a class myVector based on std::vector and add only a sort method. The sort method needs to access the array stored in the base class using iterator. I have following draft...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.