我在 Linux 上使用 gcc 12.2。我使用时-nostdlib,编译器抱怨缺少 memcpy 和 memmove。所以我在汇编中实现了一个错误的memcpy,并且我让memmove调用中止,因为我总是想使用memcpy。
我想知道如果我用 C 实现自己的函数,是否可以避免编译器要求 memcpy (和 memmove)。优化器似乎注意到它到底是什么,并无论如何调用 C 函数。然而,自从它被实现(我使用#define memcpy mymemcpy)并且自从我运行它以来,我看到我的应用程序中止。它调用了我的 memmove 实现而不是程序集 memcpy。为什么 gcc 调用 move 而不是 copy?
clang 调用 memcpy 但 gcc 更好地优化了我的代码,因此我使用它来优化构建
__attribute__ ((access(write_only, 1))) __attribute__((nonnull(1, 2)))
inline void mymemcpy(void *__restrict__ dest, const void *__restrict__ src, int size)
{
const unsigned char *s = (const unsigned char*)src;
unsigned char *d = (unsigned char*)dest;
while(size--) *d++ = *s++;
}
Run Code Online (Sandbox Code Playgroud)
可重现
//dummy.cpp
extern "C" {
void*malloc() { return …Run Code Online (Sandbox Code Playgroud) 我有十六进制文字 400D99999999999A,它是 3.7 作为双精度的位模式
我如何在 C 中编写它?我看到了这个关于 float_literal 和 hex 的页面。也许这是显而易见的,我需要睡觉,但我不知道如何将位模式编写为浮点数。我知道它应该让一个人写出更精确的分数,但我不确定如何将位模式转换为文字
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
double d = 0x400D99999999999Ap0;
printf("%f\n", d); //incorrect
unsigned long l = 0x400D99999999999A;
memcpy(&d, &l, 8);
printf("%f\n", d); //correct, 3.7
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我想在我的代码中插入一些时间测量。在 x64 上我使用 __rdtscp。mac m1/m2 有类似的东西吗?具体来说,不是系统调用和高分辨率。
我有一个数组,MyStruct* my[100];我在循环中设置数据,如下所示:
if id > X && id < Y { my[id-X] = p;
Run Code Online (Sandbox Code Playgroud)
现在我想访问my[0]as name0、my[1]as different1、my[2]asanotherThing等。
我该怎么做呢?我尝试了union+X宏,但这在 gcc 中不起作用(它在 clang 中起作用)。
我想我必须编写一个脚本,这样我就不会手写所有的#defines,但我认为#define anotherThing my[2]这是给出数组项名称的唯一方法,对吗?我想简单地写different1->myStructMember,而不是my[123]->myStructMember每次都写,这样不可读。