我正在研究Linux内核,目前我尝试实现自己的系统调用.
在内核代码中,它看起来如下:
asmlinkage long sys_my_syscall()
{
printk("My system call\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果我用systemcall()函数调用它可以正常工作,但我找到了另一种方法:
int my_syscall(void)
{
long __res;
__asm__ volatile (
"movl $312, %%eax;"
"int $0x80;"
"movl %%eax, %0;"
: "=m" (__res)
:
: "%eax"
);
if ((unsigned long) (__res) >= (unsigned long) (-125)) {
errno = -(__res);
__res = -1;
}
return (int)(__res);
}
Run Code Online (Sandbox Code Playgroud)
但它返回了价值-14 EFAULT.
我究竟做错了什么?
设置: Linux内核3.4,ARCH x86_64
可能重复:
三个规则是什么?
我有一个问题,在以下程序中双重释放内存.
调试器显示问题出在push_back()函数中.
A类:
class A {
public:
A(int x);
int x;
};
A::A(int x) {
this->x = x;
}
Run Code Online (Sandbox Code Playgroud)
B级:
class B {
public:
B(int x);
~B();
A* a;
};
B::B(int x) {
this->a = new A(x);
}
B::~B() {
delete a;
}
Run Code Online (Sandbox Code Playgroud)
主功能:
int main() {
vector<B> vec;
for(int i = 0; i < 10; i++) {
vec.push_back(B(i)); <------------ Issue is here
}
cout << "adding complete" << endl;
for(int i = 0; i < …Run Code Online (Sandbox Code Playgroud) 我找到了以下代码:
switch(val){
case 0:
// some actions
break;
case 1:
// some actions
break;
case 2:
// some actions
break;
}
Run Code Online (Sandbox Code Playgroud)
但目前还不清楚在例如情况下会发生什么val = 10?
我试图在一个程序中用不正确的值测试这段代码,没有任何事情发生 - 程序正常退出.
此代码可能导致任何潜在错误吗?有没有保证什么都不会发生?
我想知道是否有一些pro和contra直接在include文件中包含include语句,而不是在源文件中包含它们.
我个人喜欢让我的包含"干净"所以,当我将它们包含在一些c/cpp文件中时,我不必追捕所有可能的标题,因为包含文件本身不会处理它.另一方面,如果我在include文件中包含include,则编译时间可能会变大,因为即使使用include guard,也必须首先解析文件.这只是一个品味问题,还是有任何优点/缺点?
我的意思是:
sample.h
#ifdef ...
#include "my_needed_file.h"
#include ...
class myclass
{
}
#endif
Run Code Online (Sandbox Code Playgroud)
sample.c文件
#include "sample.h"
my code goes here
Run Code Online (Sandbox Code Playgroud)
与:
sample.h
#ifdef ...
class myclass
{
}
#endif
Run Code Online (Sandbox Code Playgroud)
sample.c文件
#include "my_needed_file.h"
#include ...
#include "sample.h"
my code goes here
Run Code Online (Sandbox Code Playgroud) 我有以下脚本:
use strict;
use warnings;
my @test = ("a", "b", "c", "a", "ca");
my @res = grep(m#a#, @test);
print (join(", ", @res)."\n");
Run Code Online (Sandbox Code Playgroud)
它应该只返回包含的字符串a.它完美地运作.
问题是我需要能够动态获取这些字符串.我尝试了以下方法:
use strict;
use warnings;
my $match = "a";
my @test = ("a", "b", "c", "a", "ca");
my @res = grep($match, @test);
print (join(", ", @res)."\n");
Run Code Online (Sandbox Code Playgroud)
结果是:
a,b,c,a,ca
我应该怎么做才能grep使用动态变量的数组?
我有以下代码编译得很好:
void foo::bar(const vector<int> arg) {
int* ptr = arg.data();
// do something with ptr
}
Run Code Online (Sandbox Code Playgroud)
我需要重载此函数 vector<bool>
void foo::bar(const vector<bool> arg) {
int* ptr = arg.data();
// error C2039: 'data': is not a member ofstd::vector<bool,std::allocator<_Ty>>'
// do something with ptr
}
Run Code Online (Sandbox Code Playgroud)
vector<bool>没有data()会员的原因是什么?
在这里(en.cppreference.com)我没有找到一些特定的bool案例std::vector.
该代码使用MSVS 2015进行编译.
假设我们有2个文件
1)file1.c
int Appples[10];
Run Code Online (Sandbox Code Playgroud)
2)file2.c
extern int *Appples;
Run Code Online (Sandbox Code Playgroud)
除了我必须独立处理尺寸之外,这种声明是否有任何问题?
无意中发现了以下代码:
if (bool result = f()) {
// Do the stuff
}
Run Code Online (Sandbox Code Playgroud)
它是用gcc 4.9.2和编译的MSVS 2013.
以下代码编译并打印False!:
#include <iostream>
bool foo() {
return false;
}
void bar() {
if (bool result = foo()) {
std::cout << "True!\n";
} else {
std::cout << "False!\n";
}
}
int main()
{
bar();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我认为这个(语法除外)功能只有C++17.
我明白错了吗?
我有一个文件,在行的末尾包含空格,应删除它们.
当我使用以下命令时:
%s/\s+$//
Run Code Online (Sandbox Code Playgroud)
Vim向我显示错误,找不到模式.这有什么不对?
注意:实际上,我可以使用该%s/\s*$//命令,但我想了解问题的根本原因.
假设我有以下简单程序(http://cpp.sh/5sygh):
#include <map>
#include <iostream>
using Key = std::pair<unsigned long, unsigned long long>;
struct KeyLess {
bool operator()(const Key& lhs, const Key& rhs) {
if (lhs.first < rhs.first) {
return true;
}
if (lhs.second < rhs.second) {
return true;
}
return false;
}
};
int main() {
std::map< Key , int, KeyLess> m;
m[Key{2, 169}] = 1;
m[Key{1, 255}] = 2;
m[Key{1, 391}] = 3;
m[Key{1, 475}] = 4;
std::cout << "Elements in map: " << m.size() << …Run Code Online (Sandbox Code Playgroud)