On May 24, 7:55 pm, asterisc <Rares....@ni.comwrote:
Quote:
I'm using windows and there is no segmentation fault.
You can choose to reserve() the size instead of clean().. in build()
function. That way, there will be no resizes.
Whether a program segfault is tricky. I traced the execution using
Valgrind and confirmed that it writes to a freed memory address. See
trace below. reserve() doesnt serve the purpose because I dont know
how many to reserve.
==24373== Invalid write of size 4
==24373== at 0x407A41:
KDTree::build_tree_recursive(std::vector<int, std::allocator<int
const&) (kdtree.h:240)
==24373== by 0x407A39:
KDTree::build_tree_recursive(std::vector<int, std::allocator<int
const&) (kdtree.h:240)
==24373== by 0x407A39:
KDTree::build_tree_recursive(std::vector<int, std::allocator<int
const&) (kdtree.h:240)
==24373== by 0x407A08:
KDTree::build_tree_recursive(std::vector<int, std::allocator<int
const&) (kdtree.h:239)
==24373== by 0x407A08:
KDTree::build_tree_recursive(std::vector<int, std::allocator<int
const&) (kdtree.h:239)
==24373== by 0x407A08:
KDTree::build_tree_recursive(std::vector<int, std::allocator<int
const&) (kdtree.h:239)
==24373== by 0x407CA7: KDTree::build_tree() (kdtree.h:188)
==24373== by 0x401463: main (main.cpp:26)
==24373== Address 0x54565CC is 1,436 bytes inside a block of size
900,960 free'd
==24373== at 0x4A051A0: operator delete(void*) (vg_replace_malloc.c:
244)
==24373== by 0x4039B2:
__gnu_cxx::new_allocator<KDTreeNode>::deallocate(K DTreeNode*, unsigned
long) (new_allocator.h:94)
==24373== by 0x4039E4: std::_Vector_base<KDTreeNode,
std::allocator<KDTreeNode::_M_deallocate(KDTreeNod e*, unsigned
long) (stl_vector.h:133)
==24373== by 0x406039: std::vector<KDTreeNode,
std::allocator<KDTreeNode>
Quote:
>::_M_fill_insert(__gnu_cxx::__normal_iterator<KDT reeNode*,
std::vector<KDTreeNode, std::allocator<KDTreeNode >, unsigned long,
KDTreeNode const&) (vector.tcc:381)
==24373== by 0x406100: std::vector<KDTreeNode,
std::allocator<KDTreeNode>
Quote:
>::insert(__gnu_cxx::__normal_iterator<KDTreeNode* ,
std::vector<KDTreeNode, std::allocator<KDTreeNode >, unsigned long,
KDTreeNode const&) (stl_vector.h:658)
==24373== by 0x40619D: std::vector<KDTreeNode,
std::allocator<KDTreeNode::resize(unsigned long, KDTreeNode)
(stl_vector.h:426)
==24373== by 0x4061E0: KDTree::get_next_free_node_index() (kdtree.h:
309)
==24373== by 0x40777E:
KDTree::build_tree_recursive(std::vector<int, std::allocator<int
const&) (kdtree.h:202)
==24373== by 0x407A39:
KDTree::build_tree_recursive(std::vector<int, std::allocator<int
const&) (kdtree.h:240)
==24373== by 0x407A39:
KDTree::build_tree_recursive(std::vector<int, std::allocator<int
const&) (kdtree.h:240)
==24373== by 0x407A39:
KDTree::build_tree_recursive(std::vector<int, std::allocator<int
const&) (kdtree.h:240)
==24373== by 0x407A08:
KDTree::build_tree_recursive(std::vector<int, std::allocator<int
const&) (kdtree.h:239)