为什么将 C 代码块括在花括号中?

发布于 2024-08-09 20:14:36 字数 829 浏览 11 评论 0原文

我正在查看一些 C 代码,并注意到它充满了围绕代码块的花括号,没有任何类型的控制结构。看一下:

//do some stuff . . .
fprintf(stderr, "%.2f sec\n", (float)(clock() - t) / CLOCKS_PER_SEC);
{
    //a block! why not?
    char *tmp_argv[3];
    tmp_argv[0] = argv[0]; tmp_argv[1] = str; tmp_argv[2] = prefix;
    t = clock();
    fprintf(stderr, "[bwa_index] Convert nucleotide PAC to color PAC... ");
    bwa_pac2cspac(3, tmp_argv);
    fprintf(stderr, "%.2f sec\n", (float)(clock() - t) / CLOCKS_PER_SEC);
}

为什么要在代码中插入这样的块?里面塞满了它们。有某种性能优势吗?一些神秘的 C 东西?为什么???

编辑:此代码来自 BWA,这是一个将小序列比对到的生物信息学程序使用 Burrows-Wheeler 变换 的大型参考,以防万一你们中的任何人我们想知道。此代码示例与应用程序的功能并不是特别相关。

I am looking at some C code, and have noticed it is full of these curly braces surrounding blocks of code without any sort of control structure. Take a look-see:

//do some stuff . . .
fprintf(stderr, "%.2f sec\n", (float)(clock() - t) / CLOCKS_PER_SEC);
{
    //a block! why not?
    char *tmp_argv[3];
    tmp_argv[0] = argv[0]; tmp_argv[1] = str; tmp_argv[2] = prefix;
    t = clock();
    fprintf(stderr, "[bwa_index] Convert nucleotide PAC to color PAC... ");
    bwa_pac2cspac(3, tmp_argv);
    fprintf(stderr, "%.2f sec\n", (float)(clock() - t) / CLOCKS_PER_SEC);
}

Why would you insert blocks like this in the code? It is chock full of 'em. Is there some kind of performance benefit? Some mystical C thing? Why???

edit: This code if from BWA, a bioinformatics program that aligns small sequences to large reference ones using the Burrows-Wheeler transform, in case any of you were wondering. This code example isn't particularly relevant to the functionality of the application.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

氛圍 2024-08-16 20:14:36

旧代码需要 { } 才能进行声明。

在 C89 中,您不能在任何地方都执行 int i; ;声明仅在块的开头有效。

所以:

a = 1;
int i; /* error */
i = 2;

...无效,但是

a = 1
if (e) {
  int i;

...很好,就像一个普通的块一样。

即使在声明有效后(C99)块项,最终的样式仍然继续,部分是由于惯性,部分是为了向后可移植性,还因为为新声明建立一个范围是有意义的。

Legacy code needed { } in order to do declarations at all

In C89, you couldn't just do int i; anywhere; declarations were only valid at the beginning of blocks.

So:

a = 1;
int i; /* error */
i = 2;

...wasn't valid, but

a = 1
if (e) {
  int i;

...was fine, as was a plain block.

The resulting style continued even after declarations became valid (C99) block-item(s), partly by inertia, partly for backwards portability, and also because it makes sense to establish a scope for new declarations.

在风中等你 2024-08-16 20:14:36

范围变量。例如,变量tmp_argv仅在大括号之间有效。

To scope variables. E.g. the variable tmp_argv will only be valid between the braces.

乖乖公主 2024-08-16 20:14:36

我最近发现的另一个用例是当您具有打开/关闭语义并且想要清楚地标记“内部”代码时:

f = fopen('file');
{
    // do stuff
}
fclose(f);

这可以很好地提醒您关闭/释放对象,并使代码更加清晰。

Another use case for this I've recently discovered is when you have open/close semantics and you want to clearly mark the 'inner' code:

f = fopen('file');
{
    // do stuff
}
fclose(f);

This works well to remind you to close/free objects and a makes the code somewhat cleaner.

千秋岁 2024-08-16 20:14:36

块是一个范围,它决定变量的生命周期以及它们对编译器的可见性。因此,当控制退出块时,在块内创建的变量就会消失。

当这些变量是具有构造函数和析构函数的类的实例时,它会非常方便。

但是,在您的示例中并没有太大优势。

A block is a scope that determines the lifetime of variables, as well as their visibility to the compiler. So variables that get created within a block go away when control exits the block.

It can be very handy when those variables are instances of classes with constructors and destructors.

However, in your example there is not much advantage.

孤芳又自赏 2024-08-16 20:14:36

它正在创建一个范围。堆栈对象在超出范围时将被销毁。看起来它正在进行某种打字,这意味着每个块都是他们想要计时的东西。但是,我没有看到任何作用域计时器对象,所以,是的,没有意义。

It is creating a scope. Stack objects are destroyed when they go out of scope. It looks like it is doing some sort of typing, which would mean each block is something that they wanted to time. However, I don't see any scoped timer objects, so, yeah, makes no sense.

陌上青苔 2024-08-16 20:14:36

您在块内声明的变量是该块的本地变量。这样您就可以在代码的其他位置(如下)重新定义 tmp_argv ,而不会与这段代码发生冲突。

The variables that you declare inside the block are local to that block. This way you may be able to redefine tmp_argv in some other place of your code (below) without conflicting with this piece of code.

拥抱没勇气 2024-08-16 20:14:36

这就是全部吗?也许程序员在代码的其他地方使用了 tmp_argv 。我想不出任何其他原因,因为 {} 之间的 tmp_argv 与大括号之外的任何内容都是分开的。

Is that all of it? Maybe the programmer is using tmp_argv somewhere else in the code. I can't think of any other reason since the tmp_argv between the { and } is separate from any outside the braces.

逆光飞翔i 2024-08-16 20:14:36

我有时在这些情况下使用块:
- 本地化变量
- 或者更容易阅读
...

I sometimes use blocks in these cases:
- To localize variables
- Or to easier to read
...

兲鉂ぱ嘚淚 2024-08-16 20:14:36

嗯 - 我可能不符合这里的要求,但我认为在这样的块内定义的局部变量在块外无效

Hmm - I'm maybe off the chart here but I think local variable define inside such block will not be valid outside of the block

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文