以这个有争议的代码为例。
struct X {
int arr[1];
float something_else;
};
struct X get_x(int first)
{
struct X ret = { .arr = { first } };
return ret;
}
int main(int argc, char **argv) {
int *p = get_x(argc+50).arr;
return *p;
}
Run Code Online (Sandbox Code Playgroud)
get_x返回一个struct X. 我只对它的成员感兴趣arr。如果我只想要arr……为什么要为整个结构创建一个局部变量?
但是..那个代码正确吗?
在显示的示例中,C 标准是否知道将 的返回值保留在get_x堆栈上,直到调用堆栈帧结束,因为我正在用指针窥视其中?
我使用 linux 内核 .clang-format 作为参考,但这部分让我很困扰。
我怎样才能得到 clang-format 来格式化这个代码
const struct my_struct hello = {.id = 0,
.modsmin = 10,
.modsmax = 20,
.strengthmin = 0,
.strengthmax = 100,
.color = COLOR_FOREGROUND };
Run Code Online (Sandbox Code Playgroud)
对此
const struct my_struct hello = {
.id = 0,
.modsmin = 10,
.modsmax = 20,
.strengthmin = 0,
.strengthmax = 100,
.color = COLOR_FOREGROUND
};
Run Code Online (Sandbox Code Playgroud) Clang 允许使用精简 lto 来加快编译时间,同时仍然保留使用 lto 和选项的大部分优点-flto=thin。gcc 是否有相当于 clang 的瘦 lto 的功能?
我一直想知道..它被称为SIMD,因为单指令多数据。那么为什么它只有单一的数据指令呢?
例如,vaddss单个数据等于多个数据vaddps。几乎每条 SIMD 指令都有一个数据版本。
为什么?
SIMD为什么叫SIMD却只有单数据指令?
如果将指针传递给只读函数,则该指针是 IN 参数。
如果一个指针被传递给一个只读函数,但是这个函数创建了一个指针的副本,以便在只读操作的模块相关函数中访问它,这个指针仍然是 IN。
如果函数仍然使用指针作为只读,但其他模块相关函数使用指针进行写操作,那么指针是什么?一个 IN 参数,但没有 const?输入/输出参数?
我的意思的例子:
class SteeringWheel {
public: float rotation;
public: SteeringWheel(void) {
this->rotation = 0.f;
}
};
class Car {
private: SteeringWheel *steeringWheel;
public:
/**
* @param[?] steeringWheel Is the steering wheel in or in/out?
*/
Car (SteeringWheel *steeringWheel) {
this->steeringWheel = steeringWheel;
}
/**
* @param[in] degree Steering amount in degrees.
*/
void steer(float degree)
{
this->steeringWheel->rotation += degree;
}
};
int main(int argc, char **argv)
{
SteeringWheel steeringWheel();
/* car() uses …Run Code Online (Sandbox Code Playgroud) 如何在 C 中调用采用匿名结构的函数?
比如这个函数
void func(struct { int x; } p)
{
printf("%i\n", p.x);
}
Run Code Online (Sandbox Code Playgroud) 拿这个代码
int main() {
int *p = &(int){2};
}
Run Code Online (Sandbox Code Playgroud)
这是什么?&(int){2}一个没有名字的变量?C 中的无名变量有什么意义?如果没有为它分配名称,编译器如何知道将它保留在堆栈中?
此外,如果我声明一个无名的值,例如
int main() {
(int){3};
}
Run Code Online (Sandbox Code Playgroud)
有没有办法以完全可移植的方式获取这个无名变量的地址?(完全可移植,无论堆栈如何排列,它都可以在每个平台上运行)
经过多年的 CI 编码,不知道您可以像这样声明无名变量。您是否有理由希望在常规命名变量上使用它?
as/400 ILE允许将来自不同语言的过程编译成模块,然后绑定在一起以制作单个程序.我正在尝试使用包含从我的CL模块调用的C函数的模块来完成此操作,该模块是入口模块.
C模块源:mylib/myfile/csource
int getValue(void){
return 20;
}
Run Code Online (Sandbox Code Playgroud)
CL模块源:mylib/myfile/clsource
pgm
dcl var(&NUM) type(*INT)
callprc prc(getValue) rtnval(&NUM) /* <== Calling C function. */
endpgm
Run Code Online (Sandbox Code Playgroud)
然后我将每个文件编译成自己的模块.
crtcmod module(cmodule) srcfile(myfile) srcmbr(csource)
crtclmod module(clmodule) srcfile(myfile) srcmbr(clsource)
Run Code Online (Sandbox Code Playgroud)
这两个编译,没问题.只是,当我尝试从这两个模块创建程序时,ILE绑定器抱怨CL源中的函数getValue未定义,程序创建失败.
crtpgm pgm(mypgm) module(clmodule cmodule) entmod(clmodule) detail(*basic)
Run Code Online (Sandbox Code Playgroud)
crtpgm给我的错误:
Unresolved references........................: 1
Symbol Type Library Object Linked Name
*MODULE mylib clmodule *YES getValue
Run Code Online (Sandbox Code Playgroud)
我错过了什么?
拿这个代码。
#include <stdlib.h>
int main(int argc , char **argv) {
int *x = malloc(argc*sizeof(int));
for (int i = 0; i < argc; ++i) {
x[i] = argc;
}
int t = 0;
for (int i = 0; i < argc; ++i) {
t += x[i];
}
free(x);
return t;
Run Code Online (Sandbox Code Playgroud)
这个循环
for (int i = 0; i < argc; ++i) {
x[i] = argc;
}
Run Code Online (Sandbox Code Playgroud)
向量化为
movdqu xmmword ptr [rax + 4*rdx], xmm0
movdqu xmmword ptr [rax + 4*rdx + 16], …Run Code Online (Sandbox Code Playgroud)