c++ - Qt - C++ 字符串连接段错误

标签 c++ qt segmentation-fault string-concatenation

我是 C++ 新手,所以我想我掉进了 C++ 新手陷阱。

我尝试执行以下操作:

QString  sdkInstallationDirectory=getenv("somEnv");

QString someSourceDir=sdkInstallationDirectory+"\\Data\\"+someReference+ "\\src";

我遇到了段错误。

我猜这是因为 const 字符的连接和分配给 someSourceDir QString 的内存不足。

我的错误到底是什么?我怎样才能做这个连接?

最佳答案

char * getenv ( const char * name );

A null-terminated string with the value of the requested environment variable, or NULL if that environment variable does not exist.

为什么不检查结果?

编辑。

因此,不需要检查指针。

For historical reasons, QString distinguishes between a null string and an empty string. A null string is a string that is initialized using QString's default constructor or by passing (const char *)0 to the constructor.

关于c++ - Qt - C++ 字符串连接段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11611438/

相关文章:

c++ - 返回 vector 的第一个元素被(重新)设置为 0

c++ - 结构化绑定(bind)违规

c++ - 如何将现有的Q_PROPERTY重新定义为REQUIRED?

c - 段错误: Reading values from a text file

当数据复制/扫描/读取到未初始化的指针时崩溃或 "segmentation fault"

字符数组和段错误

c++ - std::localtime 一秒导致 30 分钟的差距

C++ 升压 ASIO : how to read/write with a timeout?

linux - `root` 先前运行应用程序后,QSerialPort 无法打开 tty

c++ - 如何将 QNetworkAccessManager 移植到 WebEngine?