gpt4 book ai didi

c++ - 从函数返回 QString - 线程安全?

转载 作者:太空狗 更新时间:2023-10-29 20:24:58 27 4
gpt4 key购买 nike

我是 Qt 的新手 - 但这可能是一个非常基本的 C++ 问题。我有一个返回 QString 的简单函数:

QString testclass::myfunc(int i)
{
QString result;
switch (i) {
case 1: result = "one"; break;
case 2: result = "two"; break;
}
return result;
}

这样安全吗? c 编译器是否确保返回值在内存中保留足够长的时间以供调用函数使用? (或者这是否有内存损坏的风险)。如果是后者,返回 QString 的正确方法是什么? (结果变量必须是静态的吗?结果必须是测试类的成员变量吗?)

QString 包含常量重要吗? (what id case 3 assigned result to a random string)

如果 myfunc 是我想从不同线程调用的静态方法怎么办?我是否必须通过引用传递一个额外的 Qstring 以确保每个调用者都有自己的变量(并返回 void)?


这是实际的函数(稍微清理了一下)- 请注意,此函数是静态的,以防有所不同:

QString L::toString(const L::editions &level)
{
QString levelStr;
switch (level) {
case L::one:
levelStr = "one";
break;
case L::two:
levelStr = "two";
break;
case L::three:
levelStr = "thre";
break;
default:
levelStr = "Internal Error";
break;
}
return levelStr;
}

然而 valgrind 提示(第 121 行是 'levelStr = "one";')

34 bytes in 1 blocks are definitely lost in loss record 197 of 716
in L::toString(L::edition const&) in /mnt/lserver2/data/development/haast/src/linfo.cpp:121
1: malloc in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so
2: QArrayData::allocate(unsigned long, unsigned long, unsigned long, QFlags<QArrayData::AllocationOption>) in /opt/Qt/5.3/gcc_64/lib/libQt5Core.so.5.3.1
3: QString::QString(int, Qt::Initialization) in /opt/Qt/5.3/gcc_64/lib/libQt5Core.so.5.3.1
4: /opt/Qt/5.3/gcc_64/lib/libQt5Core.so.5.3.1
5: QString::fromUtf8_helper(char const*, int) in /opt/Qt/5.3/gcc_64/lib/libQt5Core.so.5.3.1
6: QString::fromUtf8(char const*, int) in <a href="file:///opt/Qt/5.3/gcc_64/include/QtCore/qstring.h:492" >/opt/Qt/5.3/gcc_64/include/QtCore/qstring.h:492</a>
7: QString::operator=(char const*) in <a href="file:///opt/Qt/5.3/gcc_64/include/QtCore/qstring.h:609" >/opt/Qt/5.3/gcc_64/include/QtCore/qstring.h:609</a>
8: L::toString(L::Lconst&amp;) in <a

最佳答案

http://qt-project.org/doc/qt-5/qstring.html#details

The QString class provides a Unicode character string.

QString stores a string of 16-bit QChars, where each QChar corresponds one Unicode 4.0 character. (Unicode characters with code values above 65535 are stored using surrogate pairs, i.e., two consecutive QChars.)

Unicode is an international standard that supports most of the writing systems in use today. It is a superset of US-ASCII (ANSI X3.4-1986) and Latin-1 (ISO 8859-1), and all the US-ASCII/Latin-1 characters are available at the same code positions.

Behind the scenes, QString uses implicit sharing (copy-on-write) to reduce memory usage and to avoid the needless copying of data. This also helps reduce the inherent overhead of storing 16-bit characters instead of 8-bit characters.

In addition to QString, Qt also provides the QByteArray class to store raw bytes and traditional 8-bit '\0'-terminated strings. For most purposes, QString is the class you want to use. It is used throughout the Qt API, and the Unicode support ensures that your applications will be easy to translate if you want to expand your application's market at some point. The two main cases where QByteArray is appropriate are when you need to store raw binary data, and when memory conservation is critical (like in embedded systems).

基本上 QString 很棒,几乎不用担心。您可以随心所欲地使用它。如果您因过于频繁地附加字符串而遇到任何速度减慢的问题,可以使用一种特殊的方法来使用字符串生成器,但根据我的经验,在尝试使 QString 变得更好之前,还有许多其他地方需要改进。

并直接回答您的问题:

这样安全吗? c 编译器是否确保返回值在内存中保留足够长的时间以供调用函数使用? (或者这是否有内存损坏的风险)。如果是后者,返回 QString 的正确方法是什么? (结果变量必须是静态的吗?结果必须是测试类的成员变量吗?)

在上面提到的所有情况下,都是安全的。只要任何函数都有 QString 的句柄,共享指针等就会将其保存在内存中。一旦它完全超出范围,它就会自行清理。

QString 包含常量重要吗? (what id case 3 assigned result to a random string)

不,没关系。

如果 myfunc 是我想从不同线程调用的静态方法怎么办?我是否必须通过引用传递一个额外的 Qstring 以确保每个调用者都有自己的变量(并返回 void)?

你应该用跨线程保护来包装它,比如 QMutexLocker

更新:QMutexLocker 示例

// In your constructor

m_mutex = new QMutex();


// When accessing a shared element across threads

{
QMutexLocker locker(m_mutex);
// Accessing a variable is now threadsafe!
m_sharedDataString += "!";
}

希望对您有所帮助。

关于c++ - 从函数返回 QString - 线程安全?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25541329/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com