最近我一直在尝试阅读更多开源C代码.我在业余爱好项目中采用的一种常见模式如下: -
在我的C文件中,我有静态或导出的函数.只有导出的函数才会放在头文件中.仅在对象范围内使用的全局变量也用作静态全局变量.
我的问题涉及在头文件中使用"静态内联"函数的有用性和动机.从我在线阅读的内容来看,不使用static关键字会导致多重定义错误,这就是不仅仅将函数定义为"内联"的原因.
但是,这是否意味着导出此函数以供其他对象使用?如果是,那么为什么不在C文件中定义此函数并通过头文件导出它?如果不是,为什么将它放在头文件中而不是仅仅在C文件中?
这种编码风格背后有原因吗?我错过了什么?
一个这样的例子可以在git代码库里面找到static inline:
/*
* Converts a cryptographic hash (e.g. SHA-1) into an int-sized hash code
* for use in hash tables. Cryptographic hashes are supposed to have
* uniform distribution, so in contrast to `memhash()`, this just copies
* the first `sizeof(int)` bytes without shuffling any bits. Note that
* the results will be different on big-endian and little-endian
* platforms, so they should not be stored or transferred over the net. …Run Code Online (Sandbox Code Playgroud) 我有一个相当大的软件项目的 Git 存储库。我想在每条提交消息中搜索某个子字符串。我不只是寻找当前分支上的提交,而是寻找存储库知道的每一个提交。
结果不必按任何特定顺序(尽管如果有某种顺序,那就太好了)。这可能吗?我该怎么做呢?我发现“git log -c -S”很有用,但这似乎适用于当前分支。
想象一种情况,我想调用一个执行一定量处理但有时间限制的函数。我可以在 golang 中使用context.Contextand编写一个函数select。我会想象如下:
package main
import (
"context"
"fmt"
"time"
)
func longRunning(ctx context.Context, msg string) {
stop := make(chan bool)
done := make(chan bool)
go func() {
for {
fmt.Printf("long running calculation %v...", msg)
select {
case <-stop:
fmt.Println("time to stop early!")
return
default:
}
}
done <- true
}()
select {
case <-done:
return
case <-ctx.Done():
stop <- true
return
}
}
func main() {
ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
defer …Run Code Online (Sandbox Code Playgroud) 我正在学习 Rust,目前正在练习我正在学习 Rust,目前正在这里。
到目前为止,我已经提出以下几点:
impl ParsePosNonzeroError {
...
...
fn from_parseint(err: ParseIntError) -> ParsePosNonzeroError {
ParsePosNonzeroError::ParseInt(err)
}
Run Code Online (Sandbox Code Playgroud)
fn parse_pos_nonzero(s: &str)
-> Result<PositiveNonzeroInteger, ParsePosNonzeroError>
{
let x: i64 = s.parse::<i64>().map_err(ParsePosNonzeroError::from_parseint);
PositiveNonzeroInteger::new(x).map_err(ParsePosNonzeroError::from_creation)
}
Run Code Online (Sandbox Code Playgroud)
当我这样做时,我收到错误消息:
! Compiling of exercises/error_handling/errors6.rs failed! Please try again. Here's the output:
error[E0308]: mismatched types
--> exercises/error_handling/errors6.rs:34:18
|
34 | let x: i64 = s.parse::<i64>()
| ____________---___^
| | |
| | expected due to this
35 | | .map_err(ParsePosNonzeroError::from_parseint);
| |_____________________________________________________^ expected `i64`, found enum `Result` …Run Code Online (Sandbox Code Playgroud) 我读了这个问题,描述了TLB击落的内容.我试图了解这是由内核或处理器执行的操作还是两者兼而有之?
我的问题是: -
我想按大小对字符串向量进行反向排序,约束条件是如果有2个相等长度的字符串,我希望它们保留原始顺序.例如:对以下字符串集进行排序: -
aab
aac
aacghgh
aabghgh
Run Code Online (Sandbox Code Playgroud)
应该产量: -
aacghgh
aabghgh
aab
aac
Run Code Online (Sandbox Code Playgroud)
目前我正在进行如下排序: -
struct comp_functor {
bool operator()(const string& s1, const string& s2) {
return s1.size() > s2.size();
}
};
struct comp_functor c;
vector<string> vecs;
sort(vecs.begin(), vecs.end(), c);
Run Code Online (Sandbox Code Playgroud)
有没有办法在重载方法中指定我想保留原始排序,如果它们具有相同的长度?如果没有,那么使用STL库解决这个问题的最佳方法是什么?
我有以下代码作为测试的一部分:
expected := 10
var wg sync.WaitGroup
for i := 0; i < expected; i++ {
go func(wg *sync.WaitGroup) {
wg.Add(1)
defer wg.Done()
// do something
}(&wg)
}
wg.Wait()
Run Code Online (Sandbox Code Playgroud)
令我惊讶的是,我panic: Fail in goroutine after TestReadWrite has completed在运行“go test”时得到了。当使用“go test -race”运行时,我没有感到恐慌,但测试后来失败了。在这两种情况下,尽管有 wg.Wait(),但 goroutine 并未完成执行。
我做了以下更改,现在测试按预期工作:
expected := 10
var wg sync.WaitGroup
wg.Add(expected)
for i := 0; i < expected; i++ {
go func(wg *sync.WaitGroup) {
defer wg.Done()
// do something
}(&wg)
}
wg.Wait()
Run Code Online (Sandbox Code Playgroud)
我的疑问是:
wg.Add(1)在 goroutine 内部完成的。为什么在这种特定情况下它会表现出意外?这里发生的情况似乎是,一些 …