我正在试验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_variable
readonly给用户,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未能提醒我的错误?
我知道那new
delete
是不可饶恕的malloc
free
.
这是否意味着我应该避免使用new
C库将使用的内存?
使用时会出现什么问题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) .vimrc
自上次更新以来,我的代码中存在一个奇怪的错误。
每次我启动vim时,-- REPLACE --
它都会以令人讨厌的方式启动。
我设法找出正是这一行.vimrc
引起了问题。
" Disable search highlighting temporally
nnoremap <esc> :nohl<cr>
Run Code Online (Sandbox Code Playgroud)
当我对此行发表评论时,问题就消失了。
我真的对映射的问题感到困惑。它可以正常工作,但是会-- REPLACE --
在启动时使vim进入模式。
我目前根本没有启用任何插件。
我听说可以使用 Unicode 变量名-fextended-identifiers
GCC 中的标志来使用 Unicode 变量名。所以我用C++编写了一个测试程序,但它无法编译。
#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) 我正在用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) 我只是注意到这个编译没有任何错误或警告使用-pedantic -Wall
两者gcc
和clang
.
#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代码吗?如果是这样,那么当您将函数作为函数的参数时会发生什么?
要了解有关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
跳转的位置,以便能够在那里预取指令.
你对此有所了解吗?
我正在编写一个解析器,我希望它尽可能便携。
现在我正在使用GNU bison来生成我的解析器,但我不确定我的代码是否依赖于不完全可移植的yacc扩展。
所以我想知道GNU bison具有的原始yacc缺失的功能。
我担心的原因是我注意到我的解析器无法使用bison端口在 Windows 上编译。如果它能让我的解析器更容易在不同平台之间移植,我会牺牲GNU bison功能并坚持使用原始的标准化yacc。
那么GNU bison和原始标准yacc之间有什么区别呢?如果我希望我的程序尽可能便携,在使用GNU bison时应该避免哪些功能?
我发现一些与extern
和static
(内部)变量有关的行为,我觉得很奇怪。
这是一个例子:
/* 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编程语言,并开始想知道如何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标准有什么用?
对我来说,不应该允许它是合乎逻辑的.