- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我是 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&) 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/
C语言sscanf()函数:从字符串中读取指定格式的数据 头文件: ?
最近,我有一个关于工作预评估的问题,即使查询了每个功能的工作原理,我也不知道如何解决。这是一个伪代码。 下面是一个名为foo()的函数,该函数将被传递一个值并返回一个值。如果将以下值传递给foo函数,
CStr 函数 返回表达式,该表达式已被转换为 String 子类型的 Variant。 CStr(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CSng 函数 返回表达式,该表达式已被转换为 Single 子类型的 Variant。 CSng(expression) expression 参数是任意有效的表达式。 说明 通常,可
CreateObject 函数 创建并返回对 Automation 对象的引用。 CreateObject(servername.typename [, location]) 参数 serv
Cos 函数 返回某个角的余弦值。 Cos(number) number 参数可以是任何将某个角表示为弧度的有效数值表达式。 说明 Cos 函数取某个角并返回直角三角形两边的比值。此比值是
CLng 函数 返回表达式,此表达式已被转换为 Long 子类型的 Variant。 CLng(expression) expression 参数是任意有效的表达式。 说明 通常,您可以使
CInt 函数 返回表达式,此表达式已被转换为 Integer 子类型的 Variant。 CInt(expression) expression 参数是任意有效的表达式。 说明 通常,可
Chr 函数 返回与指定的 ANSI 字符代码相对应的字符。 Chr(charcode) charcode 参数是可以标识字符的数字。 说明 从 0 到 31 的数字表示标准的不可打印的
CDbl 函数 返回表达式,此表达式已被转换为 Double 子类型的 Variant。 CDbl(expression) expression 参数是任意有效的表达式。 说明 通常,您可
CDate 函数 返回表达式,此表达式已被转换为 Date 子类型的 Variant。 CDate(date) date 参数是任意有效的日期表达式。 说明 IsDate 函数用于判断 d
CCur 函数 返回表达式,此表达式已被转换为 Currency 子类型的 Variant。 CCur(expression) expression 参数是任意有效的表达式。 说明 通常,
CByte 函数 返回表达式,此表达式已被转换为 Byte 子类型的 Variant。 CByte(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CBool 函数 返回表达式,此表达式已转换为 Boolean 子类型的 Variant。 CBool(expression) expression 是任意有效的表达式。 说明 如果 ex
Atn 函数 返回数值的反正切值。 Atn(number) number 参数可以是任意有效的数值表达式。 说明 Atn 函数计算直角三角形两个边的比值 (number) 并返回对应角的弧
Asc 函数 返回与字符串的第一个字母对应的 ANSI 字符代码。 Asc(string) string 参数是任意有效的字符串表达式。如果 string 参数未包含字符,则将发生运行时错误。
Array 函数 返回包含数组的 Variant。 Array(arglist) arglist 参数是赋给包含在 Variant 中的数组元素的值的列表(用逗号分隔)。如果没有指定此参数,则
Abs 函数 返回数字的绝对值。 Abs(number) number 参数可以是任意有效的数值表达式。如果 number 包含 Null,则返回 Null;如果是未初始化变量,则返回 0。
FormatPercent 函数 返回表达式,此表达式已被格式化为尾随有 % 符号的百分比(乘以 100 )。 FormatPercent(expression[,NumDigitsAfterD
FormatNumber 函数 返回表达式,此表达式已被格式化为数值。 FormatNumber( expression [,NumDigitsAfterDecimal [,Inc
我是一名优秀的程序员,十分优秀!