1

I cannot understand how to return a hashed string from function temporary memory (don't know how to correctly call it). Now, I have this code:

static const QString &Utils::md5(const QString &inStr)
{
    const QByteArray out = QCryptographicHash
            ::hash(inStr.toUtf8(), QCryptographicHash::Md5)
            .toHex();
    return QString(out);
}

But it gives warning during compilation and after I run my program it crashes.

LogicStuff
  • 19,397
  • 6
  • 54
  • 74
Denis Sologub
  • 7,277
  • 11
  • 56
  • 123
  • 1
    Possible duplicate of [Can a local variable's memory be accessed outside its scope?](http://stackoverflow.com/questions/6441218/can-a-local-variables-memory-be-accessed-outside-its-scope) – LogicStuff Dec 04 '15 at 20:39

1 Answers1

2

Yes, you can't return a reference to a local object, even reference-to-const. I don't see a problem with returning by value, that is:

static QString Utils::md5(const QString &inStr) { ... }
LogicStuff
  • 19,397
  • 6
  • 54
  • 74
  • Thank you very much, I understood my error! My problem is that I need to write project on unknown for me language... so I learn it as quick as can. P.S. Sorry for my English. – Denis Sologub Dec 04 '15 at 20:34
  • I have provided a duplicate you can read. Do a better research next time. You had the keywords. – LogicStuff Dec 04 '15 at 20:40
  • Maybe, I have problem with English too. I understood answer above but there are in similar topics no. – Denis Sologub Dec 04 '15 at 20:48