Is there a difference in how g++ handles this situation because of the differnece in code? To a beginner it seems like exactly the same code tbh.
I should mention, that both trees are massive in size, each containing around ten or so tensors with each an estimated element count of 10^5 or so integers.
EDIT: All numbers are allocated to the heap, with only one pointer from the tree-root actually laying on the stack.
{
std::cout << "\nTrial #" << i << std::endl;
v = createV(10, 5, 10);
ExTree<int> treeOpt = build_opt(v);
{
//...
treeOpt.evaluate();
}
ExTree<int> treeNai = build_naive(v);
{
//...
treeNai.evaluate();
}
}
and
{
std::cout << "\nTrial #" << i << std::endl;
v = createV(10, 5, 10);
ExTree<int> treeNai = build_naive(v);
ExTree<int> treeOpt = build_opt(v);
{
//...
treeOpt.evaluate();
}
{
//...
treeNai.evaluate();
}
}
I am asking this, because it seems to actually make a difference and i would like to know, why? Or, to ask more precisely, does the compiler realize, that treeOpt wont be used again after evaluate and frees to memory? The second code piece acutually causes std::bad_alloc
to happen more often.