我试图使用 a 的第三个参数SA_SIGINFO sigaction直接跳转到中断的上下文。
想到了这个:
void action(int Sig, siginfo_t *Info, void *Uctx) {
ucontext_t *uc = Uctx; setcontext(uc);
}
Run Code Online (Sandbox Code Playgroud)
将具有与以下相同的效果:
void action(int Sig, siginfo_t *Info, void *Uctx) {
return;
}
Run Code Online (Sandbox Code Playgroud)
但奇怪的是它接受三个信号(调用 setcontext 调用处理程序),然后出现段错误setcontext:
Dump of assembler code for function setcontext:
0x00007ffff7a34180 <+0>: push %rdi
0x00007ffff7a34181 <+1>: lea 0x128(%rdi),%rsi
0x00007ffff7a34188 <+8>: xor %edx,%edx
0x00007ffff7a3418a <+10>: mov $0x2,%edi
0x00007ffff7a3418f <+15>: mov $0x8,%r10d
0x00007ffff7a34195 <+21>: mov $0xe,%eax
0x00007ffff7a3419a <+26>: syscall
0x00007ffff7a3419c <+28>: pop %rdi
0x00007ffff7a3419d <+29>: cmp $0xfffffffffffff001,%rax
0x00007ffff7a341a3 …Run Code Online (Sandbox Code Playgroud) 如果您提供(整数)幂作为输入,那么创建相应的 10 次幂的最快方法是什么?我可以提出以下四种替代方案,最快的方法似乎是使用 f 字符串:
from functools import partial
from time import time
import numpy as np
def fstring(power):
return float(f'1e{power}')
def asterisk(power):
return 10**power
methods = {
'fstring': fstring,
'asterisk': asterisk,
'pow': partial(pow, 10),
'np.pow': partial(np.power, 10, dtype=float)
}
# "dtype=float" is necessary because otherwise it will raise:
# ValueError: Integers to negative integer powers are not allowed.
# see https://stackoverflow.com/a/43287598/5472354
powers = [int(i) for i in np.arange(-10000, 10000)]
for name, method in methods.items():
start = time()
for i …Run Code Online (Sandbox Code Playgroud) 另一个问题讨论优化器删除对以下调用的合法性new:是否允许编译器优化堆内存分配?。我已阅读问题、答案和N3664。
根据我的理解,编译器可以在“as-if”规则下删除或合并动态分配,即,相对于标准中定义的抽象机,结果程序的行为就像没有进行任何更改一样。
我测试了使用 clang++ 和 g++ 编译以下两个文件程序以及-O1优化,但我不明白如何允许删除分配。
// main.cpp
#include <cstdio>
extern int g_alloc;
static int* foo(int n)
{
// operator new is globally overridden in the other file.
return new int(n);
}
int main(int argc, char** argv)
{
foo(argc);
foo(argc*2);
printf("allocated: %d\n", g_alloc);
return g_alloc;
}
Run Code Online (Sandbox Code Playgroud)
// new.cpp
#include <cstdio>
#include <cstdlib>
#include <new>
int g_alloc = 0;
void* operator new(size_t n)
{
g_alloc += n;
printf("new %lu\n", n);
return malloc(n); …Run Code Online (Sandbox Code Playgroud) 有没有办法使用匹配大小写来选择字符串结尾/开头,如下所示?
match text_string:
case 'bla-bla':
return 'bla'
case .endswith('endofstring'):
return 'ends'
case .startswith('somestart'):
return 'start'
Run Code Online (Sandbox Code Playgroud) OCaml 中是否有相当于 Haskell$运算符的运算符,或者我是否必须依赖括号?例如,参见
multiplyByFive 5 + 1 = 26
Run Code Online (Sandbox Code Playgroud)
但
multiplyByFive $ 5 + 1 = 30
Run Code Online (Sandbox Code Playgroud) 以下 SML 代码取自华盛顿大学课程的家庭作业。(具体来说,它是所提供代码的一部分,以便学生可以使用它来完成课程网页上列出的作业 3上列出的作业 3。 )我不是在这里寻求作业帮助\xe2\x80\x93我想我理解代码的含义。我不太明白的是如何允许柯里化函数根据其自己的部分应用程序来定义。
\n datatype pattern = \n WildcardP\n | VariableP of string\n | UnitP\n | ConstantP of int\n | ConstructorP of string * pattern\n | TupleP of pattern list\n\nfun g f1 f2 p =\n let \n val r = g f1 f2 (* Why does this not cause an infinite loop? *)\n in\n case p of\n WildcardP => f1 ()\n | VariableP x => f2 x\n | ConstructorP(_,p) => r p\n | …Run Code Online (Sandbox Code Playgroud) 我还没有为 OCaml 设计决策找到合适的答案,希望这里对 OCaml 实现有深入了解的人之一可以提供一些启发。
在 SML 中,如果我们求值,Int.compare(6, 9)我们会得到LESSwhich 是该类型的构造函数order(其他的是GREATER和EQUAL)。在 OCaml 中,如果我们评估compare 6 9我们会得到-1。
是否有特定的理由支持 OCaml 标准库中的比较函数返回零、负数或正数,而不是 SML 中的代数数据类型?
下面的代码会产生内存泄漏吗?
#include <utility>
#include <stdexcept>
struct A {};
struct B {
B() {
throw std::runtime_error("");
}
};
template <class T>
class MyPtr {
public:
MyPtr(T* p) : m_p(p) {}
MyPtr(MyPtr&& other) : m_p(other.m_p) {
other.m_p = nullptr;
}
~MyPtr() {
delete m_p;
}
private:
T* m_p;
};
class Foo {
public:
Foo(MyPtr<A> a, MyPtr<B> b) : m_a(std::move(a)), m_b(std::move(b)) {}
private:
MyPtr<A> m_a;
MyPtr<B> m_b;
};
int main() {
try {
Foo foo(new A(), new B());
}
catch (const std::exception&) { …Run Code Online (Sandbox Code Playgroud) 当您定义一个运算符时,例如
let (++) a b = a :: b
Run Code Online (Sandbox Code Playgroud)
当你这样做时
let v = foo a ++ bar b
Run Code Online (Sandbox Code Playgroud)
bar 在 foo 之前评估。解决方法是使用 let 表达式,即
let e1 = foo a in let e2 = bar b in e1 ++ e2
Run Code Online (Sandbox Code Playgroud)
然而,有时定义一个运算符会很方便,例如它总是从左到右进行计算。有没有办法在 OCaml 中或使用 ppx 或 with 来做到这一点lazy?
考虑标准机器学习中的这个函数。
fun times_until_zero(f, x) =
if x = 0 then 0
else 1 + times_until_zero(f, f x)
Run Code Online (Sandbox Code Playgroud)
REPL 显示times_until_zero具有类型(int -> int) * int -> int。但为什么不是类型呢('a -> int) * int -> int?我从函数定义中可以看到的是,x必须是int并且f必须采用一个参数,该参数至少与int并且f必须返回一样通用int。我将它与函数绑定fun g x = 0(所以g有 type 'a -> int)一起测试并times_until_zero(g, 10)返回 1 就好了。