============================== (C) Integarting the Expr Class [TOC] ============================== ---- VIDEO ------------------------------ https://www.youtube.com/embed/OeiPykbtQTE ----------------------------------------- Required Changes ================ The parse functions call the constructors of the expression tree class _Expr_. That means they own the created expression node. When they return the pointer they transfer ownership to the caller. This means two things: - When a parse function creates a new node its not only the owner of this node but also all its child nodes. - It therefore transfers the ownership of a complete expression tree not just one node. This needs to be documented: ---- CODE (type=c) ------------------------------------------------------------- // The returned expression tree is owned by the called function: const struct Expr *parseExpr(void); const struct Expr *parseTerm(void); const struct Expr *parsePowerExpr(void); const struct Expr *parseUnaryExpr(void); const struct Expr *parseFactor(void); -------------------------------------------------------------------------------- Now it is clear that function _parseExprStatement()_ becomes the owner of the complete expression tree by calling _parseExpr()_. So here we also have to call the destructor before we return: ---- CODE (type=c) ------------------------------------------------------------- void parseExprStatement(void) { const struct Expr *expr = parseExpr(); //printf("> %lf\n", evalExpr(expr)); printExprTree(expr); expected(SEMICOLON); getToken(); deleteExpr(expr); } --------------------------------------------------------------------------------