1

I am tring to write this kind of code:

const char* what() const noexcept override
         {
           //just an example of what I have
           int n=3;
             std::string str="Mtm matrix error: Dimension mismatch: "+ n.toString()
             const char* str2 = str.c_str();
             return str2;
         }

how ever I keep getting these errors from valgrind because I used c_str

 Invalid read of size 1
==22582==    at 0x4C30A56: __GI_mempcpy (/builddir/build/BUILD/valgrind-3.13.0/memcheck/../shared/vg_replace_strmem.c:1525)
==22582==    by 0x56CF964: _IO_file_xsputn@@GLIBC_2.2.5
==22582==    by 0x56C4741: fwrite
==22582==    by 0x4ECC8F4: std::basic_ostream<char, std::char_traits<char> >& std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*, long)
==22582==    by 0x4ECCBF6: std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
==22582==    by 0x402BA9: main
==22582==  Address 0x5a2429a is 26 bytes inside a block of size 111 free'd
==22582==    at 0x4C2B16D: operator delete(void*) (/builddir/build/BUILD/valgrind-3.13.0/coregrind/m_replacemalloc/vg_replace_malloc.c:576)
==22582==    by 0x4EF3B62: std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()
==22582==    by 0x403ADE: mtm::Matrix<std::string>::DimensionMismatch::what() const
==22582==    by 0x402B9C: main
==22582==  Block was alloc'd at
==22582==    at 0x4C2A1E3: operator new(unsigned long) (/builddir/build/BUILD/valgrind-3.13.0/coregrind/m_replacemalloc/vg_replace_malloc.c:334)
==22582==    by 0x4EF3A18: std::string::_Rep::_S_create(unsigned long, unsigned long, std::allocator<char> const&)
==22582==    by 0x4EF462A: std::string::_Rep::_M_clone(std::allocator<char> const&, unsigned long)
==22582==    by 0x4EF46D3: std::string::reserve(unsigned long)
==22582==    by 0x4EF493E: std::string::append(char const*, unsigned long)
==22582==    by 0x401FF4: std::basic_string<char, std::char_traits<char>, std::allocator<char> > std::operator+<char, std::char_traits<char>, std::allocator<char> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> >&&, char const*)

can someone please help me understant how to get rid of this error and still convert std::string to const char*

Raea6789
  • 141
  • 1
  • 11
  • `str` is a local variable. Never return a reference to a local variable or any members of the local variable. They go out of scope at the end of the function and the reference becomes invalid before you can use it. – user4581301 Jul 02 '20 at 22:20
  • @user4581301 so how should I return it? – Raea6789 Jul 02 '20 at 22:21
  • you could make `str` `static` and pray you never have a problem with [re-entrance](https://en.wikipedia.org/wiki/Reentrancy_(computing)). Probably safe with an exception unless you have multiple threads. – user4581301 Jul 02 '20 at 22:24
  • 2
    Wait a sec. You don't need a `string` here at all.This I can give a good answer. – user4581301 Jul 02 '20 at 22:26
  • yes you are right , but I am adding `variable.toString()` to the str in my code – Raea6789 Jul 02 '20 at 22:28
  • @user4581301 `static` worked , thank you – Raea6789 Jul 02 '20 at 22:43
  • In the duplicate I find an over-arching bit of wisdom--make the string ahead of time--to be pretty good advice. – user4581301 Jul 02 '20 at 22:43
  • 1
    Remember that you have to be really careful with that `static`. If your code ever allows `what` to be called a second time before you've finished using `str`, it's game over, man. – user4581301 Jul 02 '20 at 22:46

0 Answers0