小编Bad*_*ild的帖子

12
推荐指数
1
解决办法
8184
查看次数

在OS X上,简单的C++程序给出了不正确的结果(这是命令行选项'c ++ 03'vs'c ++ 11'的结果)

这个简单的程序(在Linux上编译时)将根据是否编译而正确地给出两个不同的答案-std=c++0x.

问题:我无法在OS X(Mountain Lion,10.8 SDK)上重现同样的事情.我错过了什么?

#include <iostream>
#include <sstream>

class Thing : public std::ostringstream
{
public:
    Thing() : std::ostringstream() {}
    virtual ~Thing() { std::cerr << str(); }
};

int main(int argc, const char * argv[]) {
    Thing() << "Hello" << std::endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

要了解我的意思,请执行以下操作(首先在Linux上,看看它应该如何工作):

> g++ main.cpp
> ./a.out
0x401471
Run Code Online (Sandbox Code Playgroud)
> g++ -std=c++0x main.cpp
> ./a.out
Hello
Run Code Online (Sandbox Code Playgroud)

第一个将打印十六进制地址,第二个将打印"Hello".这是正确的行为,因为操作符<<解析为两个不同的东西(C++ 03中没有右值引用,所以你去了).

现在,在OS X上尝试相同的事情:


> xcrun c++ main.cpp
> ./a.out
0x10840dd88
Run Code Online (Sandbox Code Playgroud)

(这正确地产生十六进制输出.)


> xcrun c++ -std=c++0x main.cpp
> …
Run Code Online (Sandbox Code Playgroud)

c++ linux macos rvalue operator-keyword

8
推荐指数
2
解决办法
2716
查看次数

madvise(___,___,MADV_DONTNEED)是否指示操作系统懒惰地写入磁盘?

假设我想对可能非常大的文件执行顺序写入.

如果我在整个区域上mmap()一个巨大的区域和madvise(MAD​​V_SEQUENTIAL),那么我可以以相对有效的方式写入内存.我已经开始工作了.

现在,为了在我编写时释放各种OS资源,我偶尔会在已经写入的小块内存上执行munmap().我担心的是munmap()和msync()会阻塞我的线程,等待数据物理地提交到磁盘.我根本无法放慢作家的速度,所以我需要找到另一种方式.

在已经写好的小块内存中使用madvise(MAD​​V_DONTNEED)会更好吗?我想告诉操作系统懒惰地将内存写入磁盘,而不是阻止我的调用线程.

madvise()的联机帮助页有这样说,这是相当模糊的:

MADV_DONTNEED
Do  not expect access in the near future.  (For the time being, the 
application is finished with the given range, so the kernel can free
resources associated with it.)  Subsequent accesses of pages in this
range will succeed, but will result either in re-loading  of the memory
contents from the underlying mapped file (see mmap(2)) or
zero-fill-on-demand pages for mappings without an underlying file.
Run Code Online (Sandbox Code Playgroud)

linux kernel mmap shared-memory linux-kernel

7
推荐指数
2
解决办法
4348
查看次数

在Julia中,如何显示宏的内容?

例如,我想看看@time宏的"内部"是什么.我该怎么做?

macros julia

7
推荐指数
2
解决办法
277
查看次数

使用Qt,其中工作线程创建新的GUI元素

我将保持代码简单,以便你们可以看到我正在尝试做什么;)我知道所有的锁定问题,等等.我正在试图弄清楚信号和插槽如何与线程一起玩.

在main.cpp中:

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);
    MyConsole c;       // Subclasses QThread and implements run()
    MyReceiver r(app); // We pass app to MyReceiver for later (see below)
    QObject::connect(&c, SIGNAL(sendit()),
                     &r, SLOT(gotit()));
    c.start();         // Start the worker thread
    app.exec();
}
Run Code Online (Sandbox Code Playgroud)

假设信号和插槽已在头文件中正确设置(我已经测试过它们).现在,问题在于:

在MyReceiver.cpp中:

void MyReceiver::gotit()
{
    QLabel *label = new QLabel(0, "Hello");  // Some GUI element, any will do
    app.setMainWidget(*label);               // Some GUI action, any will do
}
Run Code Online (Sandbox Code Playgroud)

问题是:因为MyReceiver对象是在main()中创建的,它位于主线程上,这是否意味着插槽(例如,gotit())将在主线程上运行,因此可以安全地执行GUI操作?即使在信号是从不同的QThread(如本例中的MyConsole)中引发的情况下?

是否有更好的方法允许工作线程与GUI交互(例如,Obj-C/Cocoa有一个"主线程上的发送消息"类型的方法).这样做的"Qt方式"是什么?

提前致谢!

c++ user-interface qt multithreading

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

使用popen()读取时为什么会出现内置延迟?

我正在通过popen()执行长时间运行(通常是阻塞的)命令:"ls -R /"

问题:popen()读入你提供的缓冲区,它似乎试图在返回之前填充ENTIRE缓冲区.这导致它经常阻塞(如果你的缓冲区很大).

解决方案似乎是使基础fd无阻塞.当我这样做时,popen()仍会阻塞,通常每次约1秒.为什么会这样?

这是我的代码.确保使用-std = c ++ 11进行编译:

#include <cstdio>
#include <iostream>
#include <sys/time.h>
#include <unistd.h>
#include <fcntl.h>

static constexpr size_t SIZE = 65536;

struct Time
{
   friend std::ostream &operator<<(std::ostream &os, Time const &t)
   {
      (void)t;

      timeval tv;
      gettimeofday(&tv, nullptr);

      os << tv.tv_sec << "." << std::fixed << tv.tv_usec << " ";
      return os;
   }
};

int main()
{
   FILE *file;
   file = popen("ls -R /", "r");
   if(!file)
   {
      std::cerr << "Could not open app: " << errno; …
Run Code Online (Sandbox Code Playgroud)

c++ file popen nonblocking c++11

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

在Clojure中,我可以优化扫描吗?

;; Suppose we want to compute the min and max of a collection.
;; Ideally there would be a way to tell Clojure that we want to perform
;; only one scan, which will theoretically save a little time  

;; First we define some data to test with
;; 10MM element lazy-seq
(def data (for [x (range 10000000)] (rand-int 100)))

;; Realize the lazy-seq 
(dorun data)

;; Here is the amount of time it takes to go through the data …
Run Code Online (Sandbox Code Playgroud)

optimization traversal clojure sequence

4
推荐指数
1
解决办法
306
查看次数

clang 是否支持“-fprofile-generate”和“-fprofile-use”?

我正在使用 3.5,但它似乎不在那里......我错过了什么吗?

例如,使用 GCC 我可以编译这样的程序:

gcc -fprofile-generate main.cpp
Run Code Online (Sandbox Code Playgroud)

...然后我运行该程序,在它退出(优雅地)后,它将生成一组文件(我认为扩展名是 .gcda)。这些文件包含可用于执行基于配置文件的优化的分支结果,如下所示:

gcc -fprofile-use main.cpp
Run Code Online (Sandbox Code Playgroud)

Clang 中是否有我忽略的类似功能?

谢谢!

c++ profile clang

4
推荐指数
1
解决办法
2956
查看次数

如何用功能性的,基于数组的语言(如K(或Q))表达这种命令性功能?

如何用功能性的,基于数组的语言(如K(或Q))表达这种命令性功能?

在草率的C ++中:

vector<int> x(10), y(10); // Assume these are initialized with some values.

// BTW, 4 is just a const -- it's part of the algorithm and is arbitrarily chosen.

vector<int> result1(x.size() - 4 + 1); // A place to hold a resulting array.
vector<int> result2(x.size() - 4 + 1); // A place to hold another resulting array.

// Here's the code I want to express functionally.
for (int i = 0; i <= x.size() …
Run Code Online (Sandbox Code Playgroud)

functional-programming k kdb

3
推荐指数
1
解决办法
348
查看次数

匿名功能与需要的时间不起作用?

一个简单的例子应该证明我的问题:

First I define a simple variable:
(def a '(["one" 1] ["two" 2] ["nine" 9]))

;; CASE 1: (This works correctly)
(take-while #(< (second %) 5) a)
Returns: (["one" 1] ["two" 2])

;; CASE 2: (This does not seem to work correctly)
;; The only difference is the '>' instead of '<'
(take-while #(> (second %) 5) a)
Returns: ()
Run Code Online (Sandbox Code Playgroud)

在我看来CASE 2应该返回(["9"9])?

有没有办法调试这个,看看我错过了什么?

谢谢!

clojure anonymous-function

0
推荐指数
1
解决办法
176
查看次数