Is it possible to create memory leak in C++ without heap allocation, through very bad design ?
One example I had in mind, please correct me if it does not do what I think it does:
#include <iostream>
#include <string>
void WhatIsYourName()
{
std::string name;
std::cout << "What is your name? ";
getline (std::cin, name);
std::cout << "Hello, " << name << "!\n";
WhatIsYourName();
}
int main()
{
WhatIsYourName();
}
To me it looks like WhatIsYourName()
is initializing a new std::string name
on every call, but the function never really goes out of scope so the memory is never released.
Is that right ? Or is the compiler smart enough to see that the variable will not be used in the future, so it deletes it without the function going out of scope ?
What kind of other bad design would create a memory leak using only stack allocation ?