我无法理解linux中的内存映射页面和匿名页面.有人可以用一个例子解释一下吗?与它们相关的内核数据结构是什么?
在下面的程序中,当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) #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)
有人可以解释是什么原因以及为什么会发生这种情况?
该行来自 Rust libc crate。这里双冒号有什么用?我认为它c_uint从板条箱根引入范围,但我找不到它在板条箱根中定义的位置。
pub type speed_t = ::c_uint;
Run Code Online (Sandbox Code Playgroud) #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 ++ 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编译器编译此代码时,它工作正常.这是现在允许还是我错过了什么?
我已经使用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)
如果行 …
一个程序,每行打印一个输入的单词.
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) 此代码导致编译错误(最令人烦恼的解析)
#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) 下面的代码将包含大向量的对象传递到向量中。我希望它表现出色。我需要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)