0

I believe that the following code is safe to return the string, even though inelegant, and works as expected, but I'd like confirmation of that, and I'd also like a test case to wrap around it that proves it is safe; ie that the contents of the string are available to the caller via .c_str() method, after the stack is re-used: ie, how can I force the stack on which the local variable resides to be freed/re-used (I'm using Visual Studio 2019 and PVS-Studio static code analysis, neither of which generates a warning). I don't like replacing the local szLocalTime with a global, nor do I like the idea of a malloc in the function as that places the onus on the caller to free. I've added some examples of cases that fill in the char[], eg C lib or Win32 functions that don't know about strings.

string FormatTimestamp(SYSTEMTIME st)
{
    char szLocalTime[1000];

    szLocalTime[0] = 0;

    // *** Some code that depends on a char buffer fills szLocalTime...
    // eg. strftime(szLocalTime, sizeof(szLocalTime)...);
    // eg. GetDlgItemText(hDlg, ID, szLocalTime, sizeof(szLocalTime));



    string s = szLocalTime; // <-- the question relates to this
    return s;
}
  • 4
    std::strings don't "point" to anything –  Sep 03 '19 at 15:36
  • 1
    Anything named an iterator, view, range or ptr will need to keep the original source around. However, most standard containers make copies and don't need the original. – Zan Lynx Sep 03 '19 at 15:37
  • 1
    Unrelated: Possibly `*** Some code that fills szLocalTime in Here ***` could take advantage of a `std::stringstream` or [Howard Hinnant's Date library](https://github.com/HowardHinnant/date) to simplify things. – user4581301 Sep 03 '19 at 15:41
  • Are you sure you meant your code like that? Maybe you were actually wondering about [returning a reference to a local var](https://stackoverflow.com/questions/4643713/c-returning-reference-to-local-variable)? Which in your case would mean changing the function signature to returning a string&. – nada Sep 03 '19 at 15:42

2 Answers2

4

std::string always copies its initializer. So it is safe.

Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271
-1

As stated by Maxim, std::string always makes a copy of whatever it's given, so this is safe to do. If you want to avoid copying, you could also just do this:

string FormatTimestamp(SYSTEMTIME st)
{
    std::string szLocalTime(1000, '\0'); 

    // String automatically initialized to 0
    // *** Some code that fills szLocalTime in Here ***

    szLocalTime.resize(strlen(szLocalTime.data())); 
    return szLocalTime; // <-- the question relates to this 
}
Alecto Irene Perez
  • 10,321
  • 23
  • 46