From 7eeb930c727bed366d1f0913c6fe64ae80e58813 Mon Sep 17 00:00:00 2001 From: Wilson Snyder Date: Sun, 3 Jan 2021 11:54:43 -0500 Subject: [PATCH] examples: Use unique_ptr --- examples/make_tracing_c/sim_main.cpp | 10 +++++----- examples/make_tracing_sc/sc_main.cpp | 15 ++++++++------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/examples/make_tracing_c/sim_main.cpp b/examples/make_tracing_c/sim_main.cpp index 571df64a8..b6ebc6cbd 100644 --- a/examples/make_tracing_c/sim_main.cpp +++ b/examples/make_tracing_c/sim_main.cpp @@ -5,6 +5,9 @@ // SPDX-License-Identifier: CC0-1.0 //====================================================================== +// For std::unique_ptr +#include + // Include common routines #include @@ -43,7 +46,8 @@ int main(int argc, char** argv, char** env) { Verilated::mkdir("logs"); // Construct the Verilated model, from Vtop.h generated from Verilating "top.v" - Vtop* top = new Vtop; // Or use a const unique_ptr, or the VL_UNIQUE_PTR wrapper + // Using unique_ptr is similar to "Vtop* top = new Vtop" then deleting at end + const std::unique_ptr top{new Vtop}; // Set some inputs top->reset_l = !0; @@ -97,10 +101,6 @@ int main(int argc, char** argv, char** env) { VerilatedCov::write("logs/coverage.dat"); #endif - // Destroy model - delete top; - top = nullptr; - // Fin exit(0); } diff --git a/examples/make_tracing_sc/sc_main.cpp b/examples/make_tracing_sc/sc_main.cpp index 9df44e1e1..f91ad5b42 100644 --- a/examples/make_tracing_sc/sc_main.cpp +++ b/examples/make_tracing_sc/sc_main.cpp @@ -6,6 +6,9 @@ // SPDX-License-Identifier: CC0-1.0 //====================================================================== +// For std::unique_ptr +#include + // SystemC global header #include @@ -45,8 +48,8 @@ int sc_main(int argc, char* argv[]) { ios::sync_with_stdio(); // Define clocks - sc_clock clk("clk", 10, SC_NS, 0.5, 3, SC_NS, true); - sc_clock fastclk("fastclk", 2, SC_NS, 0.5, 2, SC_NS, true); + sc_clock clk{"clk", 10, SC_NS, 0.5, 3, SC_NS, true}; + sc_clock fastclk{"fastclk", 2, SC_NS, 0.5, 2, SC_NS, true}; // Define interconnect sc_signal reset_l; @@ -58,7 +61,9 @@ int sc_main(int argc, char* argv[]) { sc_signal > out_wide; // Construct the Verilated model, from inside Vtop.h - Vtop* top = new Vtop("top"); + // Using unique_ptr is similar to "Vtop* top = new Vtop" then deleting at end + const std::unique_ptr top{new Vtop{"top"}}; + // Attach signals to the model top->clk(clk); top->fastclk(fastclk); @@ -129,10 +134,6 @@ int sc_main(int argc, char* argv[]) { VerilatedCov::write("logs/coverage.dat"); #endif - // Destroy model - delete top; - top = nullptr; - // Fin return 0; }