小编Wil*_*mKF的帖子

使用OpenMP时内存泄漏

下面测试用例运行的内存上在回路下列当使用OpenMP的"后MT节"消息32台机器(投掷的std :: bad_alloc的),但是,如果表示OpenMP的编译指示被注释的那样,代码运行通过完成精细,所以会出现,当内存并行线程分配,它不是免费的正确,因此,我们耗尽内存.

问题是下面的内存分配和删除代码是否有问题,或者这是gcc v4.2.2或OpenMP中的错误?我也试过gcc v4.3并且失败了.

int main(int argc, char** argv)
{
    std::cout << "start " << std::endl;

    {
            std::vector<std::vector<int*> > nts(100);
            #pragma omp parallel
            {
                    #pragma omp for
                    for(int begin = 0; begin < int(nts.size()); ++begin) {
                            for(int i = 0; i < 1000000; ++i) {
                                    nts[begin].push_back(new int(5));
                            }
                    }
            }

    std::cout << "  pre delete " << std::endl;
            for(int begin = 0; begin < int(nts.size()); ++begin) {
                    for(int j = 0; j < nts[begin].size(); ++j) {
                            delete nts[begin][j]; …
Run Code Online (Sandbox Code Playgroud)

multithreading gcc memory-leaks openmp allocator

5
推荐指数
1
解决办法
4830
查看次数

如何在Qt Creator中获取标签文本的上标?

我正在尝试创建一个2的上标,以显示在用户在对话框中提供的字段之后出现的单位标签的平方.我在Windows上使用Qt Creator v2.0.1.QLabel有一个文本字段和一个textFormat字段.我试图通过将textFormat组合框设置为RichText来解决这个问题,然后将textFormat字段设置为:"μm{\ super 2}"但是,在预览我的对话框时,文本按字面意思而不是RichText,因此没有上标2,而是删除了反斜杠的'{super 2}'.我也尝试使用'{\ super 2}'来反转反斜杠,但在预览对话框时我会使用文字'{\ super 2}'.

如何指定Qt Creator的上标,以便我的对话框预览显示上标2?

这里是我希望它看起来:微米2

text-formatting superscript qt-creator richtext

5
推荐指数
1
解决办法
2558
查看次数

如何在stdin上强制执行eof?

对于C++应用程序,如何以编程方式强制stdin上的文件结束(EOF)?

c++ stdin eof

5
推荐指数
2
解决办法
8308
查看次数

C++编译器可以假设const bool&value不会改变吗?

C++编译器可以假设'const bool&'值不会改变吗?

例如,假设我有一个类:

class test {
public:
  test(const bool &state)
    : _test(state) {
  }

  void doSomething() {
    if (_test) {
      doMore();
    }
  }
  void doMore();

private:
  const bool &_test;
};
Run Code Online (Sandbox Code Playgroud)

我用它如下:

void example() {
  bool myState = true;
  test myTest(myState);

  while (someTest()) {
    myTest.doSomething();
    myState = anotherTest();
  }
}
Run Code Online (Sandbox Code Playgroud)

是否允许编译器的标准假设_test的值不会改变.

我想不是,但只是想确定.

c++ const reference side-effects language-lawyer

5
推荐指数
2
解决办法
2180
查看次数

掌握创建者与依赖注入

GRASP Creator 是否与依赖注入完全矛盾?

如果不是,请解释原因。

design-patterns dependency-injection design-principles grasp

5
推荐指数
1
解决办法
760
查看次数

如何从基类中公开隐藏的重载?

鉴于此代码:

class base {
public:
  string foo() const; // Want this to be visible in 'derived' class.
}

class derived : public base {
public:
  virtual int foo(int) const; // Causes base class foo() to be hidden.
}
Run Code Online (Sandbox Code Playgroud)

如何在不使用调用基类的虚方法重载复制它的情况下使base :: foo()对派生可见?是否using做的伎俩,如果是的话,它在哪里去了,是不是这样?

class derived : public base {
public:
  virtual int foo(int) const;
  using base::foo;
}
Run Code Online (Sandbox Code Playgroud)

c++ inheritance hidden overloading

5
推荐指数
1
解决办法
1349
查看次数

Emacs TAGS表在lex/yacc文件中被#line指令混淆

我正在尝试使用etags为大型软件项目生成TAGS表.由于lex/yacc生成的.c文件中的#line指令引用了非限定文件名而不是#,TAGS文件最终会在顶层显示不存在的文件而不是子目录中存在的文件.包含包含该文件的子目录的行.

如何通过将这些#line指令正确解析为子目录中的现有文件而不是被解释为顶级文件来生成TAGS表?

我从顶级目录运行这样的etags:

rm -f TAGS; find . \( -not -regex '.*include/.*' \)
                -a \( -name '*.h' -o -name '*.hh' -o -name '*.y' -o -name '*.l'
                   -o -name '*.cc' -o -name '*.cpp' -o -name '*.hpp' -o -name '*.c'
                   -o -name '*.inl' \)
             | xargs etags -o TAGS --append
Run Code Online (Sandbox Code Playgroud)

但我有一个文件,act/Par.c,在生成的文件中间包含以下行:

#define T_NUM 274
#define T_STRING 275

#line 5 "Par.y"

#undef actCPMeshConfigIn_yywrap
#define actCPMeshConfigIn_YYMAXDEPTH 20000
Run Code Online (Sandbox Code Playgroud)

这导致以下TAGS表条目:

act/Par.c,1160
[...]
#define T_NUM 92,2870
#define T_STRING 93,2888

Par.y,1320
#undef actCPMeshConfigIn_yywrap20,
#define actCPMeshConfigIn_YYMAXDEPTH 22,
Run Code Online (Sandbox Code Playgroud)

但是文件Par.y实际上位于act/Par.y,但#line指令相对于当前文件,但TAGS使其相对于生成的TAGS文件.

在不改变我构建项目的方式的情况下,如何生成TAGS文件,以便将这些#line指令正确解释为相对于它们出现的文件?或者,如何轻松跳过这些麻烦的文件?

tags emacs etag yacc lex

5
推荐指数
1
解决办法
163
查看次数

如何创建模板具有特定类型的编译时断言?

我有一个模板函数,并希望在编译时确保它没有在特定类的子类型或超类型上实例化.

如果违反此规则,如何导致C++编译器错误?

class base {
};
class derived : public base {
};
class lowest : public derived {
};

template <typename T>
bool isCorrect(const T& obj) {
  typedef foo<T> D;
  foo<T> *def = foo<T>::find();
  return (def && def->getAnswer(object));
}
Run Code Online (Sandbox Code Playgroud)

我想isCorrect只上课derived,但不是baselowest.请注意,可能有许多其他最低类和要排除的基类字符串以及可接受的替代派生类.

在C++中是否有一种方法可以将模板限制为仅应用于我明确指定的派生类?

c++ templates compiler-errors

5
推荐指数
2
解决办法
1293
查看次数

为什么pthread_exit()在极少数情况下会在pthread_detach()之后调用时导致SEGV?

我在C++中得到一个SEGV,在pthread_join()我的应用程序正在关闭时,我无法轻易地重现(它出现在大约100,000次测试运行中).我检查了errno的值,它是零.这是在Centos v4上运行的.

在什么条件下会pthread_join()获得SEGV?这可能是某种竞争条件,因为它非常罕见.一个人建议我不应该调用pthread_detach()和pthread_exit(),但我不明白为什么.

我的第一个工作假设是,pthread_join()pthread_exit()仍然在另一个线程中运行时被调用,并且这在某种程度上导致了SEGV,但是许多人已经说过这不是问题.

在应用程序退出期间在主线程中获取SEGV的失败代码看起来大致如此(为简洁起见,省略了错误返回代码检查):

// During application startup, this function is called to create the child thread:

return_val = pthread_create(&_threadId, &attr,
                            (void *(*)(void *))initialize,
                            (void *)this);

// Apparently this next line is the issue:
return_val = pthread_detach(_threadId);

// Later during exit the following code is executed in the main thread:

// This main thread waits for the child thread exit request to finish:

// Release condition so child thread will exit: …
Run Code Online (Sandbox Code Playgroud)

c++ centos pthreads segmentation-fault race-condition

5
推荐指数
1
解决办法
1678
查看次数

为什么受保护的继承会导致 dynamic_cast 失败?

我将我的 C++ 基类更改为protected继承,并且我的dynamic_cast(s) 停止工作。

为什么要改变继承来protected改变行为dynamic_cast

struct Base {
  static Base *lookupDerived(); // Actually returns a Derived * object.
};

struct Derived : protected /* Switch this to public to get it working */ Base {
 static void test() {
   Base *base = lookupDerived();

   if (dynamic_cast<Derived *>(base)) {
      std::cout << "It worked (we must be using public inheritance)." << std::endl;
   } else {
      std::cout << "It failed (we must be using protected inheritance)." …
Run Code Online (Sandbox Code Playgroud)

c++ dynamic-cast protected public

5
推荐指数
1
解决办法
2532
查看次数