小编wef*_*fa3的帖子

在头文件中声明一个全局变量`extern const int`,但在源文件中只声明`int`

我正在试验GCC,发现你可以const在头文件中声明外部变量,但在实现文件中保持它们是可变的.

编辑:这实际上不起作用.我编译测试代码的唯一原因是因为我没有在"header.c"中包含"header.h".

header.h:

#ifndef HEADER_H_
#define HEADER_H_

extern const int global_variable;

#endif
Run Code Online (Sandbox Code Playgroud)

header.c:

int global_variable = 17;
Run Code Online (Sandbox Code Playgroud)

这似乎是一个非常好的功能,用于保持global_variablereadonly给用户,header.h但保持它们可以通过implementation(header.c)进行修改.

注意:以下代码只是这种声明方式如何阻止赋值的示例global_variable.

#include "header.h"

int main(void)
{
    global_variable = 34; /* This is an error as `global_variable` is declared const */
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

因为我之前从未见过技术.我开始怀疑它是否有效.

这是一个定义明确的行为还是这是GCC未能提醒我的错误?

c header constants external global-variables

7
推荐指数
1
解决办法
6906
查看次数

将使用"new"分配的内存传递给C库是否安全?

我知道那new delete是不可饶恕的malloc free.

这是否意味着我应该避免使用newC库将使用的内存?

使用时会出现什么问题new而不是malloc将内存传递给C库时会出现什么问题?

void func()
{
    int *p = new int(42);

    // Should I insist on using malloc for p if this function is a part
    // of a C library?
    lib_func(p);
}
Run Code Online (Sandbox Code Playgroud)

c c++ memory libraries

5
推荐指数
2
解决办法
406
查看次数

此映射有什么问题?Vim以-替换-模式启动

.vimrc自上次更新以来,我的代码中存在一个奇怪的错误。

每次我启动vim时,-- REPLACE --它都会以令人讨厌的方式启动。

我设法找出正是这一行.vimrc引起了问题。

" Disable search highlighting temporally
nnoremap <esc> :nohl<cr>
Run Code Online (Sandbox Code Playgroud)

当我对此行发表评论时,问题就消失了。

我真的对映射的问题感到困惑。它可以正常工作,但是会-- REPLACE --在启动时使vim进入模式。

我目前根本没有启用任何插件。

vim

5
推荐指数
2
解决办法
1409
查看次数

为什么这些 Unicode 变量名称不能与 -fextended- 标识符一起使用?“, “ 和 ?

我听说可以使用 Unicode 变量名-fextended-identifiersGCC 中的标志来使用 Unicode 变量名。所以我用C++编写了一个测试程序,但它无法编译。

\n
#include <iostream>\n#include <string>\n\n#define \xc2\xac !\n#define \xe2\x89\xa0 !=\n#define \xc2\xab <<\n#define \xc2\xbb >>\n\n/* uniq: remove duplicate lines from stdin */\nint main() {\n    std::string s;\n    std::string t = "";\n    while (cin \xc2\xbb s) {\n        if (s \xe2\x89\xa0 t)\n            cout \xc2\xab s;\n        t = s;\n    }\n    return 0;\n}\n
Run Code Online (Sandbox Code Playgroud)\n

我收到这些错误:

\n
#include <iostream>\n#include <string>\n\n#define \xc2\xac !\n#define \xe2\x89\xa0 !=\n#define \xc2\xab <<\n#define \xc2\xbb >>\n\n/* uniq: remove duplicate lines from stdin */\nint main() {\n    std::string s;\n    std::string t …
Run Code Online (Sandbox Code Playgroud)

c++ unicode gcc variable-names

5
推荐指数
2
解决办法
5581
查看次数

是否有可能使这个 YACC 语法明确?表达式:... | 表达式 表达式

我正在用yacc / bison编写一个简单的计算器。

表达式的语法看起来有点像这样:

expr
: NUM
| expr '+' expr { $$ = $1 + $3; }
| expr '-' expr { $$ = $1 - $3; }
| expr '*' expr { $$ = $1 * $3; }
| expr '/' expr { $$ = $1 / $3; }
| '+' expr %prec '*' { $$ = $1; }
| '-' expr %prec '*' { $$ = $1; }
| '(' expr ')' { $$ = …
Run Code Online (Sandbox Code Playgroud)

grammar yacc ambiguity bison ambiguous-grammar

5
推荐指数
1
解决办法
664
查看次数

作为参数来起作用

我只是注意到这个编译没有任何错误或警告使用-pedantic -Wall两者gccclang.

#include <stdio.h>

int x = 0;

void func(int f(const char *)) {
    f("func()!");
}

int main(void) {
    func(puts);
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下,似乎将参数f视为指向函数的指针int (*)(const char *).

但这种行为是我从未见过或听过的.这是合法的C代码吗?如果是这样,那么当您将函数作为函数的参数时会发生什么?

c parameters pointers function-pointers function

5
推荐指数
1
解决办法
78
查看次数

"goto"语句如何影响CPU的"分支预测"?

要了解有关CPU和代码优化的更多信息,我已经开始研究汇编编程.我还阅读了一些聪明的优化,比如"分支预测",CPU可以加速自身.

我的问题可能看起来很愚蠢,因为我还不太清楚这个问题.

我有一个非常模糊的记忆,我在某处(在互联网上)读过这些goto语句会降低程序的性能,因为它不能很好地处理CPU中的分支预测.然而,这可能只是我编造的内容并没有真正阅读过.

我认为这可能是真的.

我希望这个例子(在伪C中)将澄清为什么我认为是这样的:

int function(...) {
    VARIABLES DECLARED HERE

    if (HERE IS A TEST) {
        CODE HERE ...
    } else if (ANOTHER TEST) {
        CODE HERE ...
    } else {
        /*
        Let us assume that the CPU was smart and predicted this path.
        What about the jump to `label`?

        Is it possible for the CPU to "pre-fetch" the instructions over there?
        */
        goto label;
    }

    CODE HERE...

label:
    CODE HERE...
}
Run Code Online (Sandbox Code Playgroud)

对我来说,这似乎是一项非常复杂的任务.那是因为那时CPU需要查找goto跳转的位置,以便能够在那里预取指令.

你对此有所了解吗?

c cpu optimization goto branch-prediction

2
推荐指数
1
解决办法
1036
查看次数

GNU bison 和 yacc 有什么区别?

我正在编写一个解析器,我希望它尽可能便携。

现在我正在使用GNU bison来生成我的解析器,但我不确定我的代码是否依赖于不完全可移植的yacc扩展。

所以我想知道GNU bison具有的原始yacc缺失的功能。

我担心的原因是我注意到我的解析器无法使用bison端口在 Windows 上编译。如果它能让我的解析器更容易在不同平台之间移植,我会牺牲GNU bison功能并坚持使用原始的标准化yacc

那么GNU bison和原始标准yacc之间有什么区别呢?如果我希望我的程序尽可能便携,在使用GNU bison时应该避免哪些功能?

parsing porting yacc bison

2
推荐指数
1
解决办法
1720
查看次数

具有变量外部和静态链接的怪异行为

我发现一些与externstatic(内部)变量有关的行为,我觉得很奇怪。

这是一个例子:

/* file a.c */
#include <stdio.h>

/* variable with static linkage */
static int x = 24;

int f() {
        /* variable with extern linkage */
        extern int x; 
        return x;
}

int main() {
        printf("%d\n", f());
        return 0;
}
Run Code Online (Sandbox Code Playgroud)

--

/* file x.c */
/* define symbol `x` to be an `int` equal to 100 */
int x = 100;
Run Code Online (Sandbox Code Playgroud)

我使用以下命令编译该程序:

$ cc a.c x.c -o a
Run Code Online (Sandbox Code Playgroud)

然后,我运行程序并获得以下输出:

$ ./a
24
Run Code Online (Sandbox Code Playgroud)

为什么该程序输出24 …

c linker static symbols external

2
推荐指数
1
解决办法
202
查看次数

声明"内联"的函数是递归的是合法的吗?

我正在考虑一下C编程语言,并开始想知道如何inline与递归交互.我做了这个测试程序来找出答案.

static inline void f(void) {
    f();
}

int main(void) {
    f();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我使用编译程序gcc并且根本没有警告

$ c99 -Wall -pedantic main.c -o a
Run Code Online (Sandbox Code Playgroud)

我的问题是

关于混合inline递归的C标准有什么用?

对我来说,不应该允许它是合乎逻辑的.

c recursion inline segmentation-fault language-lawyer

-2
推荐指数
1
解决办法
77
查看次数