In C++, using malloc the way you are using it is undefined behavior, as it does not perform object initialization, which means that it’s not calling the vector’s constructor (but there are other things that are not done, as initializing the vtable if there was one, for example).
In your example, you should use new:
FP_TREE *tree = new FP_TREE();
TREE_NODE *test = new TREE_NODE();
tree->root.push_back(test);
//This code will leak unless you call delete tree; delete test;
But what you really should do in C++ is rely on RAII and avoid raw pointers (use smart pointers, like std::unique_ptr, if you need to work with pointers)
FP_TREE tree{};
TREE_NODE test{};
tree.root.push_back(&test);
//Be careful, you can end up with a dangling pointer stored in
//tree.root if test goes out of scope
1
solved vector in struct causing Segmentation fault [closed]