putenv.cpp
来自「QT4的utils库源码包.解压后运行./configure配置,然后make.」· C++ 代码 · 共 44 行
CPP
44 行
#include "putenv.h"#include <QTextStream>#include <QProcess>#include <QList>#include <QStringList>#include <cstring>#include <stdlib.h>class EnvStrings : public QList<char*> { public: ~EnvStrings() { /* This works: */ int l = count(); for (int i=0; i < l; ++i) { delete[] at(i); } /* The below foreach() causes a segfault. foreach (char* ptr, *this) { delete[] ptr; } */ /** In order to understand why, you need to know that a foreach always makes a copy of the collection. Since we are copying the object we are destroying, and copy needs to be destroyed too, we have infinite recursion, combined with repeated deletions the same memory. */ }};void qstd::putenv(QString key, QString value) { static EnvStrings newEnv; QString str = QString("%1=%2").arg(key).arg(value); int len = str.length() + 1; char* charStar = new char[len+1]; strncpy(charStar, str.toAscii(), len); newEnv << charStar; ::putenv(charStar); /* call Global version */}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?