2026-08-11 17:40:36 +03:00
|
|
|
#include "hello_world.hpp"
|
|
|
|
|
|
2026-08-11 18:23:17 +03:00
|
|
|
#include <cstring>
|
|
|
|
|
#include <string>
|
|
|
|
|
|
2026-08-11 17:40:36 +03:00
|
|
|
namespace fundamentals {
|
|
|
|
|
|
|
|
|
|
auto greet(const char* name) -> const char* {
|
2026-08-11 18:23:17 +03:00
|
|
|
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
|
2026-08-11 17:40:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace fundamentals
|