假设我有floatIEEE 754 binary32的保证.给定一个与存储的有效浮点相对应的位模式std::uint32_t,如何float以最有效的标准兼容方式将其重新解释为?
float reinterpret_as_float(std::uint32_t ui) {
return /* apply sorcery to ui */;
}
Run Code Online (Sandbox Code Playgroud)
我有几种方法,我知道/怀疑/假设有一些问题:
通过reinterpret_cast,
float reinterpret_as_float(std::uint32_t ui) {
return reinterpret_cast<float&>(ui);
}
Run Code Online (Sandbox Code Playgroud)
或者等价的
float reinterpret_as_float(std::uint32_t ui) {
return *reinterpret_cast<float*>(&ui);
}
Run Code Online (Sandbox Code Playgroud)
哪个遭受别名问题.
通过union,
float reinterpret_as_float(std::uint32_t ui) {
union {
std::uint32_t ui;
float f;
} u = {ui};
return u.f;
}
Run Code Online (Sandbox Code Playgroud)
这实际上并不合法,因为它只允许从最近写的成员读取.然而,似乎有些编译器(gcc)允许这样做.
通过std::memcpy,
float reinterpret_as_float(std::uint32_t ui) {
float f;
std::memcpy(&f, &ui, 4);
return f;
}
Run Code Online (Sandbox Code Playgroud)
哪种AFAIK是合法的,但复制单个单词的函数调用似乎很浪费,尽管它可能会被优化掉.
通过reinterpret_cast …
c++ standards-compliance type-conversion language-lawyer c++11
当我使用g ++编译这个示例代码时,我收到此警告:
警告:解除引用类型惩罚指针将破坏严格别名规则
[-Wstrict-aliasing]
代码:
#include <iostream>
int main()
{
alignas(int) char data[sizeof(int)];
int *myInt = new (data) int;
*myInt = 34;
std::cout << *reinterpret_cast<int*>(data);
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,不是data别名int,因此将其强制转换为int不会违反严格的别名规则?或者我在这里遗漏了什么?
编辑:奇怪,当我这样定义时data:
alignas(int) char* data = new char[sizeof(int)];
Run Code Online (Sandbox Code Playgroud)
编译器警告消失了.堆栈分配是否与严格别名产生差异?事实上它是一个char[]而不是一个char*意味着它实际上不能为任何类型别名吗?
昨天,有人给我看了这个代码:
#include <stdio.h>
int main(void)
{
unsigned long foo = 506097522914230528;
for (int i = 0; i < sizeof(unsigned long); ++i)
printf("%u ", *(((unsigned char *) &foo) + i));
putchar('\n');
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这导致:
0 1 2 3 4 5 6 7
Run Code Online (Sandbox Code Playgroud)
我很困惑,主要是for循环中的行。据我所知,似乎&foo是被强制转换为 anunsigned char *然后被i. 我觉得*(((unsigned char *) &foo) + i)是一个更详细的书写方式((unsigned char *) &foo)[i],但是这使得它看起来像foo,一个unsigned long被索引。如果是这样,为什么?循环的其余部分似乎是典型的打印数组的所有元素,所以一切似乎都表明这是真的。演员unsigned char *阵容让我更加困惑。我试图寻找有关转换的整数类型,以char *对谷歌而言,但我的研究得到了一些后无用的搜索结果停留约铸造int …
c pointers casting char-pointer implementation-defined-behavior
我正在阅读K&R中关于C的联合,据我所知,联合中的单个变量可以包含几种类型中的任何一种,如果某些东西存储为一种类型并且提取为另一种,则结果纯粹是实现定义的.
现在请检查以下代码段:
#include<stdio.h>
int main(void)
{
union a
{
int i;
char ch[2];
};
union a u;
u.ch[0] = 3;
u.ch[1] = 2;
printf("%d %d %d\n", u.ch[0], u.ch[1], u.i);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
3 2 515
Run Code Online (Sandbox Code Playgroud)
在这里,我在分配值u.ch,但来自检索u.ch和u.i.它是实现定义的吗?或者我做的事情真的很傻?
我知道这对其他大多数人来说似乎都是初学者,但我无法弄清楚输出背后的原因.
谢谢.
有一个类似的问题在这里,但在这一问题的用户似乎有一个更大的阵列,或载体。如果我有:
bool boolArray[4];
Run Code Online (Sandbox Code Playgroud)
我想检查所有元素是否为假,我可以分别检查[0],[1],[2]和[3],也可以循环遍历。由于(据我所知)false应该具有值0,而除0以外的其他任何东西都为true,所以我想到了简单地做:
if ( *(int*) boolArray) { }
Run Code Online (Sandbox Code Playgroud)
这行得通,但是我意识到它依赖于布尔值是一个字节,整数是四个字节。如果我强制转换为(std :: uint32_t)可以,还是一个坏主意?我刚好在一个数组中有3或4个布尔值,并且想知道这是否安全,如果不是,那么是否有更好的方法可以做到。
另外,如果我最终得到超过4个布尔值,但少于8个布尔值,那么我可以使用std :: uint64_t或unsigned long long之类的东西做同样的事情吗?
有什么unsigned int*不同的东西吗int*?我知道unsigned有更高的价值范围.还是,int*甚至不能指向任何unsigned int?
我有一个带有 C 编译代码的R 包,它在相当长一段时间内相对稳定,并且经常针对各种平台和编译器(windows/osx/debian/fedora gcc/clang)进行测试。
最近添加了一个新平台来再次测试包:
Logs from checks with gcc trunk aka 10.0.1 compiled from source
on Fedora 30. (For some archived packages, 10.0.0.)
x86_64 Fedora 30 Linux
FFLAGS="-g -O2 -mtune=native -Wall -fallow-argument-mismatch"
CFLAGS="-g -O2 -Wall -pedantic -mtune=native -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong -fstack-clash-protection -fcf-protection"
CXXFLAGS="-g -O2 -Wall -pedantic -mtune=native -Wno-ignored-attributes -Wno-deprecated-declarations -Wno-parentheses -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong -fstack-clash-protection -fcf-protection"
Run Code Online (Sandbox Code Playgroud)
此时,编译后的代码立即开始沿着这些行进行段错误:
*** caught segfault ***
address 0x1d00000001, cause 'memory not mapped'
Run Code Online (Sandbox Code Playgroud)
通过使用具有优化级别的rocker/r-basedocker 容器,我已经能够一致地重现段错误。运行较低的优化可以解决问题。运行任何其他设置,包括在 valgrind(-O0 和 -O2)、UBSAN(gcc/clang)下,都没有显示任何问题。我也有理由确定这在 下运行,但没有数据。 …
我的问题的背景是网络编程.假设我想通过网络在两个程序之间发送消息.为简单起见,假设消息看起来像这样,字节顺序不是问题.我想找到一种正确,可移植且有效的方法来将这些消息定义为C结构.我知道有四种方法:显式铸造,通过联合铸造,复制和编组.
struct message {
uint16_t logical_id;
uint16_t command;
};
Run Code Online (Sandbox Code Playgroud)
void send_message(struct message *msg) {
uint8_t *bytes = (uint8_t *) msg;
/* call to write/send/sendto here */
}
void receive_message(uint8_t *bytes, size_t len) {
assert(len >= sizeof(struct message);
struct message *msg = (struct message*) bytes;
/* And now use the message */
if (msg->command == SELF_DESTRUCT)
/* ... */
}
Run Code Online (Sandbox Code Playgroud)
我的理解是send_message不违反别名规则,因为byte/char指针可以为任何类型设置别名.但是,相反的情况并非如此,因此receive_message违反了别名规则,因此具有未定义的行为.
union message_u {
struct message m;
uint8_t bytes[sizeof(struct message)];
};
void receive_message_union(uint8_t *bytes, size_t …Run Code Online (Sandbox Code Playgroud) 我找不到一个体面的文档来解释对齐系统如何工作以及为什么某些类型比其他类型更严格一致.
我正在写一个类似于类型擦除的函数包装器std::function.(是的,我已经看过类似的实现,甚至是p0288r0提案,但我的用例非常狭窄且有些专业化.).以下大量简化的代码说明了我当前的实现:
class Func{
alignas(sizeof(void*)) char c[64]; //align to word boundary
struct base{
virtual void operator()() = 0;
virtual ~base(){}
};
template<typename T> struct derived : public base{
derived(T&& t) : callable(std::move(t)) {}
void operator()() override{ callable(); }
T callable;
};
public:
Func() = delete;
Func(const Func&) = delete;
template<typename F> //SFINAE constraints skipped for brevity
Func(F&& f){
static_assert(sizeof(derived<F>) <= sizeof(c), "");
new(c) derived<F>(std::forward<F>(f));
}
void operator () (){
return reinterpret_cast<base*>(c)->operator()(); //Warning
}
~Func(){
reinterpret_cast<base*>(c)->~base(); //Warning
} …Run Code Online (Sandbox Code Playgroud) c ×6
c++ ×5
casting ×2
gcc ×2
pointers ×2
arrays ×1
boolean ×1
c++11 ×1
c++14 ×1
char-pointer ×1
implementation-defined-behavior ×1
memory ×1
networking ×1
portability ×1
undefined ×1
unions ×1