On Thu, 1 Apr 2021 at 18:25, Kannuswamy, Nanthakumar wrote: > > Thanks > > We started working on fixing the code. > > How its behaving differently in gcc 4.2 and gcc 8.2 compiler? In GCC 4.x the std::string class uses a reference-counted implementation, so the pointer returns by getName().c_str() is the same pointer returned by name.c_str(), so it happens to still be valid after the getName1() function returns. Since GCC 5.x the default is to use a non-reference-counted std::string implementation, so the pointer returned by getName().c_str() becomes invalid when temporary returned by getName() is destroyed.