From 6af86468d0d3213c4911a96d5c6ef85e004c0463 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Gil=20de=20G=C3=B3mez=20P=C3=A9rez?= Date: Tue, 11 Aug 2026 18:23:17 +0300 Subject: [PATCH] Solved: 02 - Hello World! --- modules/01_fundamentals/src/hello_world.cpp | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/modules/01_fundamentals/src/hello_world.cpp b/modules/01_fundamentals/src/hello_world.cpp index 2a24a75..c3bbfbf 100644 --- a/modules/01_fundamentals/src/hello_world.cpp +++ b/modules/01_fundamentals/src/hello_world.cpp @@ -1,11 +1,21 @@ #include "hello_world.hpp" +#include +#include + namespace fundamentals { auto greet(const char* name) -> const char* { - // TODO: Return a static string in the format "Hello, !" - (void)name; - return "Hello, World!"; + const auto n = std::string(name); + const auto c = "Hello, " + n + "!"; + // LESSON: Never return pointers to local objects -> dangling -> undefined behavior + // NO! --> return c.c_str(); + // The destructor will be called when c goes out of scope, destroying the object and freeing its memory + // This is shit C++ and should use std::string instead as a return value + const auto p = new char[c.length() + 1]; // Give space for the null terminated pointer + strcpy(p, c.c_str()); + return p; + // TL;DR -> Never use C-style strings if possible } } // namespace fundamentals