我正在使用C ++来实现FP-growth算法,但是在使用vector时我遇到了一些错误。这是我的代码:
typedef struct Node
{
FPData *fpData;
int childCount;
struct Node *parent;
vector<Node*> childs;
}FPNode;
FPNode* FPTree::NewNode(string data, int support)
{
FPNode *node = (FPNode*) malloc(sizeof(FPNode));
FPData *fpData = (FPData*) malloc(sizeof(FPData));
fpData -> data = data;
fpData -> support = support;
node -> fpData = fpData;
node -> childCount = 0;
node -> parent = NULL;
return node;
}
void FPTree::InsertFPPath(FPNode* root, list<FPData*> fpDatas)
{
if(fpDatas.size() <= 0)
{
return;
}
bool flag = true;
for(int i = 0; i < root -> childCount; i++)
{
FPNode* child = root -> childs[i];
if(child -> fpData -> data == fpDatas.front() -> data)
{
flag = false;
child -> fpData -> support ++;
fpDatas.pop_front();
InsertFPPath(child, fpDatas);
break;
}
}
if(flag)
{
FPData* firstData = fpDatas.front();
FPNode* newChild = NewNode(firstData -> data, firstData -> support);
root -> childs.push_back(newChild); // error at here
root -> childCount ++;
fpDatas.pop_front();
for(int i = 0; i < fpDatas.size(); i++)
{
InsertFPPath(newChild, fpDatas);
}
}
}
InsertFPPath()
是递归方法,第二次迭代时出错:
root -> childs.push_back(newChild)
错误发生在:
construct(_Up* __p, _Args&&... __args) // here the __p is null, and __args is right
{
::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
}
线程队列停在:
#0 0x00000001000050cf in void std::__1::allocator::construct(Node**, Node* const&&&) [inlined] at /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:1673
回溯是:
#0 0x00000001000050cf in void std::__1::allocator::construct(Node**, Node* const&&&) [inlined] at /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:1673 #1 0x00000001000050bc in void std::__1::allocator_traits >::__construct(std::__1::integral_constant, std::__1::allocator&, Node**, Node* const&&&) [inlined] at /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:1600 #2 0x000000010000509c in void std::__1::allocator_traits >::construct(std::__1::allocator&, Node**, Node* const&&&) [inlined] at /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:1453 #3 0x0000000100005079 in std::__1::vector >::push_back(Node* const&) [inlined] at /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:1590 #4 0x0000000100004fa9 in FPTree::InsertFPPath(Node*, std::__1::list >) at /Users/dorothy/ME/USTC/作业/数据挖掘/experiment/FPTree/FPTree/FPTree.cpp:129 #5 0x00000001000051b4 in FPTree::InsertFPPath(Node*, std::__1::list >) at /Users/dorothy/ME/USTC/作业/数据挖掘/experiment/FPTree/FPTree/FPTree.cpp:135**
答案 0 :(得分:0)
为包含构造函数的结构(struct
或class
)或包含具有构造函数的对象的结构分配内存时,则无法使用malloc
分配内存。 malloc
函数只分配内存,但它不会调用构造函数,在你的情况下,这意味着不会构造childs
成员对象,并且使用向量将导致未定义的行为。
在C ++中动态分配内存时,需要使用new
运算符:
FPNode *node = new FPNode;
FPData *fpData = new FPData;