To my understanding this code snippet has a problem, since std::string
will be removed after to_string
and port
should have an incorrect value.
#include <iostream>
#include <stdio.h>
int main() {
int a = 123;
const char* port = std::to_string(a).c_str();
printf("val = %s", port);
return 0;
}
However, program works just fine and prints "123" (with g++ compiler). Valgrind does not spot a problem as well.
Is there any way to demonstrate (maybe some tool or compiler option) that code above indeed has a problem?