c - 打包成员地址 : Disable for specific function?

标签 c gcc gcc9

我注意到每次访问打包结构的成员时都会出现此错误

  1. 如何专门针对处理未对齐的函数禁用它? (safe_unaligned_val_16/32/64 - 通过像 memcpy 一样手动获取一个字节一个字节来构造输出)
  2. 如果我将结构指针传递给函数,GCC 是否会进行一些优化?我注意到一些优化,例如当您有一个具有 4 字节基元类型的结构时,当函数不更改基元的值时,它只是将该值传递到单个 32 位寄存器。如果您知道类似的内容,请详细说明这个^

谢谢,格雷格

最佳答案

  1. How can I disable it specifically for functions that handle the misalignment? (safe_unaligned_val_16/32/64 - constructs the output by manually taking byte after byte like memcpy)

C 语言既没有指定诊断,也没有指定任何解决方法。它甚至没有结构打包的感觉,将结构布局的大部分细节留给实现自行决定。

由于您似乎对 GCC 特别感兴趣,因此您可能会发现该实现的 pragmas for influencing diagnostics可以用于该目的。特别是,您可能会发现这样做很方便

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wwhatever-the-relevant-option-is"

// function or functions ...

#pragma GCC diagnostic pop
  1. Does GCC make some optimization if I pass struct pointers to a function? I've noticed some optimizations like when you have a struct with 4 bytes of primitive types, it just passes that value to a single 32 bit register when the function doesn't change the value of the primitives in. Elaborate if you know anything like this ^

我不知 Prop 体的优化,但实现有很大的优化自由。编译器负责确保生成的程序的语义与提供给它的源代码的 C 描述等效。对于满足一组相当严格的要求的函数来说,这样的优化确实可能是有效和有用的,但不修改指向的值不足以保证这样的优化是安全的。

关于c - 打包成员地址 : Disable for specific function?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58818589/

相关文章:

c - 获取结构中数组元素的地址

c++ - 这段代码做了什么,为什么编译?

C/OSX/clang 困惑 : "symbol(s) not found" happening at link time instead of compile time

c++ - 如何使用 GCC 9 将 `std::filesystem::file_time_type` 转换为字符串

c++ - 编译器错误 : is private within this context only on gcc9 with c++17

c - C 中的类型双关和 union

c - 在 Linux 登录时显示消息

c++ - 使用#ifdefs 指定平台

c++ - 为什么 __builtin_prefetch 在这里没有任何作用?

c++ - '<一些系统头> : error: ‘<insert function here>’ is not a member of ‘std’ ' after moving source file