小编bor*_*ree的帖子

什么是内存映射页面和匿名页面?

我无法理解linux中的内存映射页面和匿名页面.有人可以用一个例子解释一下吗?与它们相关的内核数据结构是什么?

linux memory-management linux-kernel

25
推荐指数
2
解决办法
3万
查看次数

为什么不能在不可变的情况下在lambda中转发参数?

在下面的程序中,当mutable不使用时,程序将无法编译。

#include <iostream>
#include <queue>
#include <functional>

std::queue<std::function<void()>> q;

template<typename T, typename... Args>
void enqueue(T&& func, Args&&... args)
{
    //q.emplace([=]() {                  // this fails
    q.emplace([=]() mutable {             //this works
        func(std::forward<Args>(args)...);
    });
}

int main()
{
    auto f1 = [](int a, int b) { std::cout << a << b << "\n"; };
    auto f2 = [](double a, double b) { std::cout << a << b << "\n";};
    enqueue(f1, 10, 20);
    enqueue(f2, 3.14, 2.14);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这是编译器错误

lmbfwd.cpp: In …
Run Code Online (Sandbox Code Playgroud)

c++ lambda language-lawyer c++11

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

为什么我没有在以下c程序中获得预期的输出?

可能重复:
"#define STR(a)#a"有什么作用?
c编程语言中的宏评估

#include <stdio.h>
#define f(a,b) a##b
#define g(a)   #a
#define h(a) g(a)

int main()
{
      printf("%s\n",h(f(1,2)));
      printf("%s\n",g(f(1,2)));
      return 0;
 }
Run Code Online (Sandbox Code Playgroud)

我期待printf的输出相同.但我得到的是不同的(如下所示)

12
f(1,2)
Run Code Online (Sandbox Code Playgroud)

有人可以解释是什么原因以及为什么会发生这种情况?

c macros

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

标识符前面的::(双冒号)是什么意思?

该行来自 Rust libc crate。这里双冒号有什么用?我认为它c_uint从板条箱根引入范围,但我找不到它在板条箱根中定义的位置。

pub type speed_t = ::c_uint;
Run Code Online (Sandbox Code Playgroud)

syntax rust

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

为什么这个c程序不打印第一个printf语句?

#include<stdio.h>
#include <unistd.h>
int main(){
      while(1)
      {

              fprintf(stdout,"hello-out");
              fprintf(stderr,"hello-err");
              sleep(1);
      }
      return 0;
}
Run Code Online (Sandbox Code Playgroud)

在gcc中编译这个程序并执行它只打印hello-err而不是hello-out.为什么会这样?有人可以解释它背后的原因吗?

c printf

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

是否在c ++中允许从支持的值列表中分配std :: array?

在c ++ primer(第5版)中,提到不允许从支持的值列表中分配std :: array.

由于右侧操作数的大小可能与左侧操作数的大小不同,因此数组类型不支持assign,并且不允许从支持的值列表中进行赋值.

下面的代码作为示例给出.

  std::array<int, 10> a1 = {0,1,2,3,4,5,6,7,8,9}; 
  std::array<int, 10> a2 = {0}; // elements all have value 0
  a1 = a2; // replaces elements in a1
  a2 = {0}; // error: cannot assign to an array from a braced list
Run Code Online (Sandbox Code Playgroud)

但是,当我使用c ++ 11编译器编译此代码时,它工作正常.这是现在允许还是我错过了什么?

c++ arrays language-lawyer c++11

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

装配中的奇怪分段故障

我已经使用nasm编译器和ld链接器用汇编语言编写了一个hexdump实用程序.程序应该转储任何输入文件的十六进制值.但是它在一个特定的过程"LoadBuff"中进行了段错误.loadbuff的功能是读取输入到一个16字节的缓冲区.代码是

LoadBuff:
    push ebx                 
    push edx
    push eax

    mov eax,3       ;sys_read call      
    mov ebx,0               ;read from standard input
    mov ecx,Buff            ;pass the buffer adress
    mov edx,BuffLen         ;pass the number of bytes to be read at a time
    int 80h                 ;call the linux kernel
    mov ebp,eax
    ;cmp eax,0              ;number of characters read is returned in eax
    ;jz exit                ;if zero character is returned i.e end of iinput file      
                            ;jump to exit

    xor ecx,ecx 
    pop eax
    pop edx
    pop ebx
    ret
Run Code Online (Sandbox Code Playgroud)

如果行 …

x86 segmentation-fault fault

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

无法弄清楚C程序中的逻辑错误

一个程序,每行打印一个输入的单词.

int main() {

    int c;

    while ((c=getchar()) != EOF) {

        if (c== ' ' || c== '\n' ||c == '\t')
                putchar('\n');
        else {
            putchar(c);
        }
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

上面的程序正确打印结果,每行一个字.在相应地改变条件之后,我期望下面的程序也每行打印一个单词.但是我没有得到正确的结果.我犯了一些愚蠢的错误还是错了?

int main() {

    int c;

    while ((c=getchar()) != EOF) {

        if (c != ' ' || c != '\n' || c != '\t')
            putchar(c);
        else {
            putchar('\n');
        }
    }

    return 0;

}
Run Code Online (Sandbox Code Playgroud)

c logic boolean-logic boolean-expression

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

这两个版本的代码有什么区别?

此代码导致编译错误(最令人烦恼的解析)

#include <iostream>

class A {
        int a;
public:
        A(int x) :a(x) {}
};

class B {
public:
        B(const A& obj) { std::cout << "B\n";}
        void foo() {std::cout << "foo\n";}
};

int main()
{
        int test = 20;
        B var(A(test));      //most vexing parse
        var.foo();
        return 0;
}
Run Code Online (Sandbox Code Playgroud)

但是如果我传递20而不是test(A(20)而不是A(test)),则没有编译错误.

#include <iostream>

class A {
        int a;
public:
        A(int x) :a(x) {}
};

class B {
public:
        B(const A& obj) { std::cout << "B\n";} …
Run Code Online (Sandbox Code Playgroud)

c++ most-vexing-parse c++11

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

在向矢量添加自定义对象时如何启用移动语义?

下面的代码将包含大向量的对象传递到向量中。我希望它表现出色。我需要test在对的调用中强制转换为rvalue push_back吗?我需要告诉编译器如何移动struct实例Test吗?还是全部自动进行?

int main()
{
    struct Test
    {
        std::vector<size_t> vals;
        double sum;
    };
    std::vector<Test> vecOfTest;
    vecOfTest.reserve(100000);

    for (size_t i = 0; i < 100000; i++)
    {
        Test test{};
        test.vals.reserve(i);
        for (size_t j = 0; j < i; j++)
        {
            test.vals.push_back(j);
            test.sum += j;
        }
        vecOfTest.push_back(test);
    }


    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ move-semantics c++11

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