相关疑难解决方法(0)

gcc,严格别名和恐怖故事

gcc-strict-aliasing-and-casting-through-a-union中,我问是否有人遇到过通过指针进行联合惩罚的问题.到目前为止,答案似乎是否定的.

这个问题是广泛的:你有任何关于gcc和严格走样恐怖故事?

背景:引用AndreyT在c99-strict-aliasing-rules-in-c-gcc中的答案:

"严格的别名规则植根于自[标准化]时代开始以来C和C++中存在的标准部分.禁止通过另一种类型的左值访问一种类型的对象的条款存在于C89/90中(6.3 )以及C++ 98(3.10/15)......并非所有编译器都希望(或敢于)强制执行或依赖它.

好吧,gcc现在敢于用它的-fstrict-aliasing开关来做到这一点.这引起了一些问题.例如,请参阅有关Mysql错误的优秀文章 http://davmac.wordpress.com/2009/10/,以及http://cellperformance.beyond3d.com/articles/2006/06/understanding中同样出色的讨论.-strict-aliasing.html.

其他一些不太相关的链接:

重复一遍,你有自己的恐怖故事吗?当然,没有表示的问题-Wstrict-aliasing是优选的.其他C编译器也很受欢迎.

6月2日补充:迈克尔伯尔的答案中的第一个链接,确实有资格作为恐怖故事,可能有点过时(从2003年开始).我做了一个快速测试,但问题显然已经消失了.

资源:

#include <string.h>
struct iw_event {               /* dummy! */
    int len;
};
char *iwe_stream_add_event(
    char *stream,               /* Stream of events */
    char *ends,                 /* End of stream */
    struct iw_event *iwe,       /* Payload */
    int event_len)              /* Real size of payload …
Run Code Online (Sandbox Code Playgroud)

c gcc strict-aliasing

51
推荐指数
4
解决办法
3万
查看次数

将浮点值从big endian转换为little endian

是否有可能将floats从大端转换为小端?我有一个来自PowerPC平台的大端值,我通过TCP发送到Windows进程(小端).这个值是a float,但是当我memcpy将值转换为Win32 float类型然后调用_byteswap_ulong该值时,我总是得到0.0000?

我究竟做错了什么?

c++ endianness

17
推荐指数
4
解决办法
5万
查看次数

C++移动浮点位表示?

是否有符合C++标准的方法来确定编译时(或运行时,作为替代)的'float','double'和'long double'的结构?

如果我假设,std::numeric_limits< T >::is_iec559 == true并且std::numeric_limits< T >::radix == 2我怀疑可以通过以下规则:

  • 第一个X位是有效数.
  • 下一个Y位是指数.
  • 最后1位是符号位.

用下面的表达式模糊地说:

  • size_t num_significand_bits = std::numeric_limits< T >::digits;
  • size_t num_exponent_bits = log2( 2 * std::numeric_limits< T >::max_exponent );
  • size_t num_sign_bits = 1u;

除了我知道

  • std::numeric_limits< T >::digits 包括"整数位",无论格式是否实际明确表示它,所以我不知道如何以编程方式检测和调整.
  • 我猜std::numeric_limits< T >::max_exponent是永远的2^(num_exponent_bits)/2.

背景:我试图解决两个问题:

  • 设置/获取有效位中的哪些位.
  • 确定'long double'结尾的位置,所以我知道不要读取具有未初始化内存的隐式填充位.

c++ floating-point portability ieee-754 bit-representation

8
推荐指数
1
解决办法
2151
查看次数

将包含浮动的结构转换为更网络友好的结构

我正在尝试通过网络发送包含浮点数据的结构,该结构包含两种不同的硬件架构,用 C 编写。

客户端运行在x86_64架构上,服务器运行在PPC(32位)架构上。

看来我将数据转换为网络友好格式或从网络友好格式转换为数据的唯一选项是:

htons/ ntohs(2 字节)和htonl/ ntohl(4 字节)

似乎没有处理浮点数的版本(这是有道理的,因为不同的体系结构/字节序有不同的浮点数表示)。

因此,我尝试通过以下方式将浮点数拆分为整数和指数格式:

void FtoME(float num, int32_t* mantissa, int32_t* exponent)
{
    int32_t sig = (int32_t)num;
    int32_t exp = 0;
    double frac = num-sig;
    double temp = num;
    while (frac > 0 && exp > -20)
    {
        temp = temp * 10; //left shift the whole number
        sig = (int32_t)temp; //get the integer part
        frac = temp - sig; //get the fractional part
        exp--;
    }
    printf("Scientific note: …
Run Code Online (Sandbox Code Playgroud)

c computer-science network-programming

5
推荐指数
1
解决办法
593
查看次数