小编Joh*_*ohn的帖子

printf("%s\n", lua_tostring(L, -1)); 遇到分段错误

为什么此代码片段会遇到分段错误?

 luaL_dostring(L, "print('this is a test')");
 printf("%s\n", lua_tostring(L, -1));
Run Code Online (Sandbox Code Playgroud)

以下是错误消息和回溯:

程序收到信号 SIGSEGV,分段错误。strlen () at ../sysdeps/x86_64/strlen.S:106 106 ../sysdeps/x86_64/strlen.S: 没有这样的文件或目录。

lua

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

如何理解`std::lower_bound`的要求?

根据有关的文件std::lower_bound,其中表示:

范围 [first, last) 必须根据表达式element < valuecomp(element, value)进行分区,即,表达式 为 的所有元素true必须位于表达式 为 的所有元素之前false。完全排序的范围满足此标准。

我有点难以完全理解它。

1.什么是element < value?上述文件中本段之前似乎从未提及element(或)。valuesaid是element指当前元素之前的元素,the saidvalue是指当前元素的值吗?

更新

2.由于特定序列是否有效(即符合要求)取决于value,因此当值不同时,不能总是保证特定序列的要求。我认为定义这样的要求是没有意义的。看来完全排序的序列更可靠、更实用

c++ c++11

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

关于自增/自减运算符

这是代码片段

#include <iostream>
#include <iterator>
#include <algorithm>
#include <functional>
#include <vector>

std::vector<int> vec(5);

int produce_seq()
{
    static int value = 0;
    return (value*value++);
}

int main()
{
    std::generate_n(vec.begin(),5, produce_seq);
    for(auto val:vec)
    {
        std::cout << val <<std::endl;
    }
}
Run Code Online (Sandbox Code Playgroud)

为什么这段代码会输出

0
2
6
12
20
Run Code Online (Sandbox Code Playgroud)

以外

0
1
4
9
16
Run Code Online (Sandbox Code Playgroud)

我认为value*value++;相当于value*value; value++;

更新

不应该value++在整个表达式已经求值之后再求值吗?例如,如果存在类似 的表达式sum=a+b++;sum=a+b;则始终在 之前求值b++

c++

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

当unique_ptr被销毁时,如何检索pclose的返回值?

正如 @user17732522 指出的那样, 的删除器std::unique_ptr应该可以用一个指针作为参数来调用。

如何检索pcloseunique_ptr 被销毁时的返回值?

代码片段无法编译,

#include<memory>
#include<iostream>
#include<string>
#include<cstdio>

int ExecCmd(const std::string& cmd, std::string& output)
{
    int ret = 0;

    {
        std::unique_ptr<FILE, void(*)(FILE*, int*)> pipe(popen(cmd.c_str(), "r"), [](FILE* file, int* ret_ptr){
                              if(NULL==file)
                              {
                                  *ret_ptr = -1;
                              } 
                              else 
                              {
                                  *ret_ptr = pclose(file);
                              }
                         });
    }

    return ret;
}

int main()
{

}
Run Code Online (Sandbox Code Playgroud)

而下面的代码片段可以编译。

#include<memory>
#include<iostream>
#include<string>
#include<cstdio>

int ExecCmd(const std::string& cmd, std::string& output)
{
    int ret = 0;

    { …
Run Code Online (Sandbox Code Playgroud)

c++ lambda smart-pointers unique-ptr c++11

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

如何在表达式中触发 std::shared_ptr 的 bool 运算符(即 `bool is_empty = shared_ptr1 &amp;&amp; shared_ptr2;` )?

鉴于cur_front_rescur_back_res都是shared_ptr, std::shared_ptr 的 bool 运算符如何在表达式(即bool is_empty = cur_front_res && cur_back_res;)中触发?

仅仅因为如果操作数(即 before和 after )不是 bool 类型,&&总是会导致内置转换?&&&&

下面的代码片段确实有效

#include <iostream>
#include <memory>

int main() {
    std::shared_ptr<int> cur_front_res; // Empty shared_ptr
    std::shared_ptr<int> cur_back_res(new int(42)); // Shared_ptr pointing to an int

    bool is_empty = cur_front_res && cur_back_res;

    if (is_empty) {
        std::cout << "Both cur_front_res and cur_back_res are not empty" << std::endl;
    } else {
        std::cout << "Either cur_front_res or cur_back_res is empty" …
Run Code Online (Sandbox Code Playgroud)

c++ smart-pointers shared-ptr c++14

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

为什么 conditional_variable::notify_all 可能不会唤醒任何线程?

我用conditional_variable::notify_all()唤醒一个等待的线程(只有一个线程在等待unique_lock 确实)。

这个代码片段在大多数情况下都运行良好,但日志文件(详细信息见下文)表明unique_lock在新创建的线程已经返回后,父线程无法获取。

我将不胜感激能在这个问题上得到一些帮助。

这是相关的代码片段:

void MainWindow::deployAction(void)
{
    std::condition_variable cvRunOver;
    std::mutex mtxRunOver;
    std::unique_lock <std::mutex> ulkRunOver(mtxRunOver);
    QString workerThreadRes;
    std::thread workThread([&]()
    {
        workThread.detach();

        do_some_process_for_seconds();
        
        cvRunOver.notify_all();
        LOG(INFO)<<"to leave the subthread";
        google::FlushLogFiles(google::GLOG_INFO);
        return;
    });

    while (cvRunOver.wait_for(ulkRunOver, std::chrono::milliseconds(100)) == std::cv_status::timeout)
    {
        qApp->processEvents();
        auto curTim = std::chrono::steady_clock::now();
        std::chrono::duration<float> escapedTim= curTim-lastTim;
        if(std::chrono::duration_cast<std::chrono::seconds>(escapedTim).count()>=5)
        {
            LOG(INFO) << "processEvents()";
            google::FlushLogFiles(google::GLOG_INFO);
            lastTim = curTim;
        }
    }
    
    LOG(INFO) << "get lock and continue to run";
    google::FlushLogFiles(google::GLOG_INFO);
}
Run Code Online (Sandbox Code Playgroud)

以下是程序无法正常工作时的相关日志:

Log line format: [IWEF]hh:mm:ss.uuuuuu threadid file:line] …
Run Code Online (Sandbox Code Playgroud)

c++ qt condition-variable c++11 unique-lock

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

动态分配的结构是否已正确初始化?

对于POD结构,能否保证动态分配的结构在C++11及之后的版本中new Demo_Class[CONST_NUMBER]()得到很好的初始化(即不是垃圾)?

如果有人阐明有关初始化 POD 结构数组的详细规则,我们将不胜感激。

有一些关于基本类型的帖子,比如说new int{}等等。但是没有关于 POD 结构的直接答案。

更重要的是,大多数帖子根本没有提到 C++11 及之后的版本。

更新:感谢 eerorika 的快速回答。根据上述答案,[强调我的]:

对 T 类型的对象进行值初始化意味着:

否则,该对象将被零初始化

怎样才能充分理解呢?struct\class 可能有许多不同类型的成员变量。这是否意味着每个成员变量都将是零初始化的对象?例如:

struct Point
{
    int x;inty;
}; 

struct Demo
{
    Point pt; 
    double* ptr; 
    std::string str;
};.
Run Code Online (Sandbox Code Playgroud)

c++ language-lawyer c++11 c++17

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

调用 std::packaged_task::get_future() 时可能出现的竞争条件

在这个文档中看到,有一个演示片段:

std::packaged_task<int()> task([]{ return 7; }); // wrap the function
std::future<int> f1 = task.get_future();  // get a future
std::thread t(std::move(task)); // launch on a thread
Run Code Online (Sandbox Code Playgroud)

我的问题是,如果我像这样重写代码片段,是否存在任何潜在的问题(竞争条件):

std::packaged_task<int()> task([]{ return 7; }); // wrap the function
std::thread t(std::move(task)); // firstly launch on a thread, and then gets a future
std::future<int> f1 = task.get_future();  // get a future
Run Code Online (Sandbox Code Playgroud)

更新1 :我理解 Nicol Bolas 的答案,如果我像这样重写代码片段,是否存在任何潜在的问题(竞争条件):

std::packaged_task<int()> task([]{ return 7;});
thread_callablefunc_queue.push_back(task);  //task may be popped and run by another thread at once, …
Run Code Online (Sandbox Code Playgroud)

c++ future race-condition c++11

-1
推荐指数
1
解决办法
85
查看次数

`std::cout` 和 `std::ostream` 之间有什么关系?

我在某处看到了下面的代码片段

#include <iostream>


int main()
{
    std::ostream& os = std::cout;

    os << "thanks a lot" << std::endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

由于上述代码片段运行良好,因此表明它std::cout源自std::ostream. 但我还找不到任何直接的参考。

根据文件,其中说[强调我的]:

全局对象 std::cout 和 std::wcout控制输出到实现定义类型(派生自 std::streambuf)的流缓冲区,与标准 C 输出流 stdout 关联。

上面的引用说控制输出到派生自其他std::cout类型而不是派生自 的类型。std::streambufstd::coutstd::streambuf

std::cout我只在名为 的文件中找到 的声明/usr/include/c++/7/iostream

  extern ostream cout;      /// Linked to standard output
Run Code Online (Sandbox Code Playgroud)

我找不到 的实现std::cout

c++ c++11

-1
推荐指数
1
解决办法
2419
查看次数