function returns QString, usage generates segfault
I'm creating a function of the form:
Code:
{
qDebug() << "DNumber as string";
char * buffer = new char[length()];
mReal->toFixPtString(buffer, mDecimalPlaces);
delete buffer;
return result;
}
The function toFixPtString(...) is from a library and it has to use char *, and it has to be dynamically allocated so that the length can be different at runtime. I want to return a QString which is not dynamically allocated, so I don't have to find a way to make sure it gets deleted. Is there a particular method for this? If I try to do anything with the value it returns, the program generates a segfault. Additionally, the compiler issues a warning about returning a local variable.
Re: function returns QString, usage generates segfault
You are returning a variable which is deleted/freed when the function returns, and thus is not valid by the time the function returns, hence the seg fault.
You'll need to dynamically allocate the qstring, or return a class variable or local static allocation of it, or pass the QString to be assigned into the function.
Re: function returns QString, usage generates segfault
There was conveniently a QString class member for me to access, and using that fixed the error. Thanks. When you said "returning a variable which is deleted/freed" I thought you meant the char *, but if Qstring was copying the string like it was supposed and then getting deleted, that wouldn't help. It makes me wonder why compiler issued a warning and not an error.
Code:
{
qDebug() << "DNumber as string";
char * buffer = new char[length()];
mReal->toFixPtString(buffer, mDecimalPlaces);
mValue = buffer;
delete buffer;
return mValue;
// the code runs and the warning dissapears, mValue persists
}
Re: function returns QString, usage generates segfault
I would change the definition from
to
Re: function returns QString, usage generates segfault
I was wondering about that. Some of the API function use const QString& for input and QString& output so I copied that without really understanding the reason. I'll change it.
Re: function returns QString, usage generates segfault
Perhaps it is time to understand the difference... :)
(QString &), is a reference to a string. Think of a reference as a pointer that has automatic de-referencing ability. In other words, using (QString&) is very similar to using (QString*).
With a reference, you can change the value of the item. This makes the reference a good choice for an argument (if you want to change the original copy), or if you want to change the value of a class member variable. The problem that you have, however, is that you returned a reference to a temporary variable that went out of scope when the function returned the value. This means that the reference is to a string that no longer exists.
If you simply return QString, then a temporary variable is created and used as the return value. The disadvantage is that a temporary QString is created to be used as the return value.
Define a function as follows:
Code:
void whatever(const QString& x)
Now, as a test, ponder what happens with this call
http://developer.kde.org/~wheeler/cpp-pitfalls.html
http://www.learncpp.com/cpp-tutorial...-by-reference/