小编Áko*_*ács的帖子

为什么GCC保持空功能?

在大多数情况下,如果我想在C中创建一个可选功能,我只需创建两个这样的函数:

#ifdef OPTIONAL_SOMETHING
void do_something(int n, const char *s)
{
    while (n--) {
        printf("%s", s);
    }

    /* ...You might get the point, really do something... */
}
#else
void do_something(int n, const char *s)
{
    /* Empty body */
}
#endif
Run Code Online (Sandbox Code Playgroud)

因此,如果符号未定义 - 禁用该功能时 - 将空函数编译到可执行文件中.

深入到汇编列表中,似乎GCC 在禁用优化编译并调用空函数.如果启用优化,也使用和,它只编译必要的堆栈处理代码,但它会优化调用指令.总而言之,它保留了功能.-O2-O3

大约相同的情况适用于非空但未使用的方法.

它应该简单地扔掉整个东西,但事实并非如此.为什么它是默认行为?只是为了好奇:我怎么能消除这个?

c macros optimization gcc function

14
推荐指数
1
解决办法
3154
查看次数

我如何检索创建的流程输出及其返回值?

我创建了一个脚本,它启动一个程序,例如ls.我想得到新程序的输出及其返回状态,而不是再次启动它.我可以抓住输出,像这样:

my $out = `ls`
Run Code Online (Sandbox Code Playgroud)

那么,我怎么能得到这个状态呢?

perl

4
推荐指数
1
解决办法
62
查看次数

无法在Python中将字符串转换为int

这个python片段有什么问题:

for zhszam in pontok.keys():
    s = 0
    for p in pontok[zhszam]:
        if p.isdigit():
            s += int(p)
            print s
    pontok[zhszam] = s
return pontok
Run Code Online (Sandbox Code Playgroud)

其中pontok{1: ['10', ' 5', ' 3', ' 10', ' 7'], 2: ['10', ' 5', ' 3', ' 10']}.它以某种方式给出以下错误输出:

10
10
{1: 10, 2: 10}
Run Code Online (Sandbox Code Playgroud)

虽然值应该是数字的总和.

提前致谢!

python arrays hash sum type-conversion

0
推荐指数
1
解决办法
455
查看次数

标签 统计

arrays ×1

c ×1

function ×1

gcc ×1

hash ×1

macros ×1

optimization ×1

perl ×1

python ×1

sum ×1

type-conversion ×1