Our website uses cookies to enhance your browsing experience.
Accept
to the top
>
>
>
V723. Function returns a pointer to...
menu mobile close menu
Additional information
toggle menu Contents

V723. Function returns a pointer to the internal string buffer of a local object, which will be destroyed.

Jun 02 2015

The analyzer has detected an issue when a function returns a pointer to the internal string buffer of a local object. This object will be automatically destroyed together with its buffer after leaving the function, so you won't be able to use the pointer to it.

The most common and simple code example triggering this message looks like this:

const char* Foo()
{
  std::string str = "local";
  return str.c_str();
}

In this code, the Foo() function returns a C-string stored in the internal buffer of the str object which will be automatically destroyed. As a result, we'll get an incorrect pointer that will cause undefined behavior when we try to use it. The fixed code should look as follows:

const char* Foo()
{
  static std::string str = "static";
  return str.c_str();
}

This diagnostic is classified as: