0

I have this function that gets 3 input(mostly LPCSTR) and combines them together and finally return the value:

template<typename T>
inline T  const& LPCombine(T const& lpStr1, T const& lpStr2, T const& lpStr3)
{
    std::string str1, str2, str3, Combined;
    LPCSTR lpCombiend = "";

    str1 = lpStr1;
    str2 = lpStr2;
    str3 = lpStr3;

    Combined = str1 + str2 + str3;

    lpCombiend = Combined.c_str();

    return lpCombiend;
}

If I print the value of lpCombined in the same function it's correct and strings or numbers are concatenated so well but If I print the returned value from the LPCombine function from another function like the main function the printed value is something weird and unreadable:

╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠

What is the problem?

2
  • @Chiel That isn’t good enough; often T would itself be a pointer type, which would have the same problem. Commented Sep 11, 2017 at 16:39
  • @Chiel The result of c_str is only guaranteed to last as long as Combined, so it gets deleted after the end of the function as Combined itself is being destroyed. Commented Sep 11, 2017 at 18:09

2 Answers 2

2

Combined is destroyed on exit from LPCombine function. As result, LPCombine returns pointer to freed memory and you have unexpected behavior. Maybe you need to return std::string from the function (use std::string as input parameters).

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, using a template for this is not a good idea in the first place, since you cannot implicitly cast just anything to a std::string or back.
1

When T == LPCSTR, i.e. const char*, your function returns const reference to a raw char pointer.

The main problem is that this pointer points to the memory of a std::string object (Combined) defined inside the function body:

std::string ..., Combined;
...

lpCombiend = Combined.c_str();

return lpCombiend;

(Note that you seem to have a typo, lpCombiend should be lpCombined.)

When the function terminates, the Combined std::string object is destroyed, so the lpCombiend pointer points to garbage.

In particular, using Visual C++ in debug builds, uninitialized memory is marked with 0xCC byte sequences. Note that 0xCC is the ASCII code 204 = (Box drawing character double line vertical and right), which is exactly what you have in your output.

So, you should consider returning a std::string object instead.

I'd also question the declaration and design of your template function. Does it really make sense to have const T& input string parameters? If T is e.g. PCWSTR (const wchar_t*), the str1 = lpStr1; assignment inside your function body won't work, as str1 is a std::string, and you can't create a std::string from a const wchar_t* raw pointer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.