0

I am trying to assign a const std::string variable to std::string variable.But getting some memory related error. code is like below:

#include <iostream>
#include <string>

using namespace std;
std::string sender1;

std::string fun()
{
      const std::string sender = "hi";
       sender1.assign(sender);
      
}

int main()
{
    fun();
    cout<<sender1<<endl;
    return 0;   
}
  • Turn on your compiler warnings. – chris Jul 15 '20 at 09:46
  • Does this answer your question? [Why does flowing off the end of a non-void function without returning a value not produce a compiler error?](https://stackoverflow.com/questions/1610030/why-does-flowing-off-the-end-of-a-non-void-function-without-returning-a-value-no) – phuclv Aug 26 '20 at 04:21

2 Answers2

3

You've forgotten a return in fun. If you change that function like this:

std::string fun()
{
    const std::string sender = "hi";
    sender1.assign(sender);
    return sender;
}

then the code will compile and run fine.

Enlico
  • 23,259
  • 6
  • 48
  • 102
-1

I got the answer finally.

We need to declare a char * globally. Then using const_cast <char *> we can convert the constant string to char and assign it.

Example: in .h file:

char * abc;

in .cc file:

func()
{
 const std::string cde = "Hello";
 //now to use this constant string in another function,we use const cast and 
 //assign it to abc like below
 abc = const_cast <char *>(cde.c_str());
}
  • 1
    `abc` will point to data that is deallocated at the end of `func()` so it would be invalid to make use of it afterwards, see https://stackoverflow.com/questions/22330250/how-to-return-a-stdstring-c-str – kmdreko Aug 26 '20 at 04:00
  • 1
    This doesn't do what you think it does. When the function returns the string variable `cde` will be destroyed. Then the pointer `abc` is left danging. This code is utterly broken. – Blastfurnace Aug 26 '20 at 04:00
  • but it worked for me. It doesn't create any dangling pointers. I haven't put the solution simply without working. – Raghavendra m a Aug 26 '20 at 13:51
  • 1
    I'm sorry but the variable `cde` is constructed in the function and is destroyed when the function returns. The pointer is referring to the contents of a destroyed object. It might have "worked" in your simple test but the code definitely invokes undefined behavior and has a terrible bug. – Blastfurnace Aug 26 '20 at 15:15
  • yes,the variable 'cde' construct within that function, and wont work outside that function.That is why I assigned it to a global char variable 'abc'. So the value of 'cde' will be in 'abc' and can be used by another function. – Raghavendra m a Aug 26 '20 at 16:57
  • 1
    You don't understand what you're talking about. The `c_str()` member function returns a pointer to the contents of the string (memory the string manages). After the string object is destroyed the pointer is no longer valid. It points inside a destroyed object or to memory that has been released back to the memory manager. The pointer assignment doesn't keep the data alive. It's a dangling pointer and the language standard says using that pointer is undefined behavior and your program is malformed. I'm not making this up, your code is a bug. – Blastfurnace Aug 26 '20 at 20:37