Antlr4 allocate ParseTree on heap

  abstract-syntax-tree, antlr4, c++, memory-management

I have a function like this to get the the AST from a file.

antlr4::tree::ParseTree *get_ast(std::string &filename) {
    std::ifstream stream;;
    antlr4::ANTLRInputStream input(stream);
    Lexer lexer(&input);
    antlr4::CommonTokenStream tokens(&lexer);
    Parser parser(&tokens);
    antlr4::tree::ParseTree *tree = parser.program();
    return tree;

But when using the return value, it seems that what tree is pointing to is already cleared (on the stack), and I need to know how to allocate the tree on the heap, so I can use the return value (and manually free).


Source: Windows Questions C++