2

I am trying to assign a value to a wstring pointer and the value does not get assigned; however, when I break the creation and assignment of the pointer into two lines, it works. Why is this?

For example:

std::wstring* myString = &(L"my basic sentence" + some_wstring_var + L"\r\n");

The above does not work, but the below does work:

std::wstring temp = (L"my basic sentence" + some_wstring_var + L"\r\n");
std::wstring* myString = &temp;
crashmstr
  • 28,043
  • 9
  • 61
  • 79
user3038090
  • 55
  • 1
  • 4

3 Answers3

9

In the first example you are getting the address of a temporary. After that line has executed that wstring object you assigned myString to isn't available anymore (these are so called rvalues by the way). I think it should be obvious that in the second example you have a real object (a lvalue which is valid as long as it doesnt run ot of scope.

To overcome this limitation with the scope you can directly create a wstring on the heap, this might better suite your situation but without further information this is hard to tell:

std::wstring* myString = new std::wstring(L"my basic sentence" + some_wstring_var + L"\r\n");

The newly created wstring will me initialized with the contents of the temporary rvalue. Just do not forget to destroy the pointer after you are done with it.

With C++11 things have complicated so temporaries can be reused more often for performance reasons. But this topic is very though and will exceed this question. I just wanted to mention it because it might interesst you aswell. For a really great explanation take a look at this SO question: What are move semantics?

Community
  • 1
  • 1
Sebastian Hoffmann
  • 11,127
  • 7
  • 49
  • 77
1
std::wstring* myString = &(L"my basic sentence" + some_wstring_var + L"\r\n");

It points to a temporary object that its life time ends at the semicolon, so dereferencing the pointer and using it is undefined behavior.

std::wstring temp = (L"my basic sentence" + some_wstring_var + L"\r\n");
std::wstring* myString = &temp;

It points to a temporary object, but the life time is longer and dereferecing it is valid while that temporary object exists.

masoud
  • 55,379
  • 16
  • 141
  • 208
  • Your answer is great, but I accepted the above answer because Paranaix gave a similar answer and also pointed me towards more research. Thanks! – user3038090 Nov 26 '13 at 20:29
0

Of course you cannot assign the adress (that's what the & is) of a string constant to a variable. Have you thought about it? Where will the pointer point to?

John
  • 2,015
  • 5
  • 23
  • 37
  • Have you thought about your answer? What's wrong with assigning the address of a string literal to a pointer variable? The pointer will point to whatever memory location the string literal resides at. – Praetorian Nov 26 '13 at 20:21
  • @Praetorian: and where will that be after the temporary thing disapears? – John Nov 26 '13 at 20:21
  • A probably (but not neccessary!) invalid memory location, but still a memory location. – Sebastian Hoffmann Nov 26 '13 at 20:23
  • You haven't said anything about any temporary thing. I quote "*you cannot assign the adress ... of a string constant to a variable*" – Praetorian Nov 26 '13 at 20:23