AcString?

AcString?

Anonymous
Not applicable
1,467 Views
5 Replies
Message 1 of 6

AcString?

Anonymous
Not applicable
const ACHAR* WhatIsReturned()
{
      AcString a = L"Test";
      AcString b = L" this";
      AcString c = a + b;
return c; }

 What happens to c when this method returns. Does the underlying string encapsulated by c get lost when c goes out of scope or is a copy of the const ACHAR* returned?

 

 

Mike

0 Likes
Accepted solutions (2)
1,468 Views
5 Replies
Replies (5)
Message 2 of 6

Alexander.Rivilis
Mentor
Mentor
Accepted solution

After return from this function destructor of "c" do returned pointer invalid. You do not use such code. IMHO.

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

0 Likes
Message 3 of 6

owenwengerd
Advisor
Advisor

Memory for all three local strings is freed before the function returns. The code is not correct.

--
Owen Wengerd
ManuSoft
Message 4 of 6

Anonymous
Not applicable

Yeah, I figured as much, but I was thinking that one of the overloads would return a new ACHAR*. I realized after I posted that this was a "cloudy mind moment" realizing that even if a new ACHAR* was returned, there was no way to free it. Will a AcString be returned by value? Or would that be freed as well.

 

AcString TestThis()
{
    AcString a = L"Test";
    AcString b = L" this!";
    AcString c = a + b;

    return c;
}

In this code, would 'c' be returned by value or would it still go out of scope?

 

 

Mike

0 Likes
Message 5 of 6

Alexander.Rivilis
Mentor
Mentor

@Anonymous wrote:

Yeah, I figured as much, but I was thinking that one of the overloads would return a new ACHAR*. I realized after I posted that this was a "cloudy mind moment" realizing that even if a new ACHAR* was returned, there was no way to free it. Will a AcString be returned by value? Or would that be freed as well.

 

AcString TestThis()
{
    AcString a = L"Test";
    AcString b = L" this!";
    AcString c = a + b;

    return c;
}

In this code, would 'c' be returned by value or would it still go out of scope?

 

 

Mike


In this code compile create copy of 'c' and return it. After that destructor of 'c' will be called. So you have a valid value of 'c' after calling TestThis():

 

AcString d = TestThis();

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

0 Likes
Message 6 of 6

owenwengerd
Advisor
Advisor
Accepted solution

It's common to return AcString by value, but functions should almost never accept string arguments as AcString by value. Almost always, string arguments should be typed as const ACHAR* or at least const AcString&. Example (untested):

 

AcString myConcatStr1(const ACHAR* in1, const ACHAR* in2)
{
  AcString in1Str(in1);
  return in1Str + in2;
}

AcString myConcatStr2(const AcString& in1Str, const AcString& in2Str)
{
  return in1Str + in2Str;
}

void test()
{
  AcString s1 = _ACRX_T("Test");
  AcString s2 = _ACRX_T(" String");
  AcString s3 = myConcatStr1(s1, s2);
  AcString s4 = myConcatStr2(s1, s2);
}
--
Owen Wengerd
ManuSoft