warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]

2023-11-04

下面的文章详细介绍了这个warning的来源和解决方法。也可以关闭优化,当然关闭优化并不是最终解决方法。

down vote accepted

First off, let's examine why you get the aliasing violation warnings.

Aliasing rules simply say that you can only access an object through its own type, its signed / unsigned variant type, or through a character type (charsigned charunsigned char).

C says violating aliasing rules invokes undefined behavior (so don't!).

In this line of your program:

unsigned int received_size = ntohl (*((unsigned int*)dcc->incoming_buf));

although the elements of the incoming_buf array are of type char, you are accessing them as unsigned int. Indeed the result of the dereference operator in the expression *((unsigned int*)dcc->incoming_buf) is of unsigned int type.

This is a violation of the aliasing rules, because you only have the right to access elements of incoming_buf array through (see rules summary above!) charsigned char or unsigned char.

Notice you have exactly the same aliasing issue in your second culprit:

*((unsigned int*)dcc->outgoing_buf) = htonl (dcc->file_confirm_offset);

You access the char elements of outgoing_buf through unsigned int, so it's an aliasing violation.

Proposed solution

To fix your issue, you could try to have the elements of your arrays directly defined in the type you want to access:

unsigned int incoming_buf[LIBIRC_DCC_BUFFER_SIZE / sizeof (unsigned int)];
unsigned int outgoing_buf[LIBIRC_DCC_BUFFER_SIZE / sizeof (unsigned int)];

(By the way the width of unsigned int is implementation defined, so you should consider using uint32_t if your program assumes unsigned int is 32-bit).

This way you could store unsigned int objects in your array without violating the aliasing rules by accessing the element through the type char, like this:

*((char *) outgoing_buf) =  expr_of_type_char;

or

char_lvalue = *((char *) incoming_buf);
down vote accepted

First off, let's examine why you get the aliasing violation warnings.

Aliasing rules simply say that you can only access an object through its own type, its signed / unsigned variant type, or through a character type (charsigned charunsigned char).

C says violating aliasing rules invokes undefined behavior (so don't!).

In this line of your program:

unsigned int received_size = ntohl (*((unsigned int*)dcc->incoming_buf));

although the elements of the incoming_buf array are of type char, you are accessing them as unsigned int. Indeed the result of the dereference operator in the expression *((unsigned int*)dcc->incoming_buf) is of unsigned int type.

This is a violation of the aliasing rules, because you only have the right to access elements of incoming_buf array through (see rules summary above!) charsigned char or unsigned char.

Notice you have exactly the same aliasing issue in your second culprit:

*((unsigned int*)dcc->outgoing_buf) = htonl (dcc->file_confirm_offset);

You access the char elements of outgoing_buf through unsigned int, so it's an aliasing violation.

Proposed solution

To fix your issue, you could try to have the elements of your arrays directly defined in the type you want to access:

unsigned int incoming_buf[LIBIRC_DCC_BUFFER_SIZE / sizeof (unsigned int)];
unsigned int outgoing_buf[LIBIRC_DCC_BUFFER_SIZE / sizeof (unsigned int)];

(By the way the width of unsigned int is implementation defined, so you should consider using uint32_t if your program assumes unsigned int is 32-bit).

This way you could store unsigned int objects in your array without violating the aliasing rules by accessing the element through the type char, like this:

*((char *) outgoing_buf) =  expr_of_type_char;

or

char_lvalue = *((char *) incoming_buf);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing] 的相关文章

  • 使用 std::packaged_task/std::exception_ptr 时,线程清理程序报告数据争用

    我遇到了线程清理程序 TSan 的一些问题 抱怨某些生产代码中的数据争用 其中 std packaged task 通过将它们包装在 std function 中而移交给调度程序线程 对于这个问题 我简化了它在生产中的作用 同时触发 TSa
  • 如何在 .NET Framework 2.0 中模拟“Func<(Of <(TResult>)>) 委托”?

    我尝试使用这个类代码项目文章 http www codeproject com KB threads AsyncVar aspx在 VB NET 和 NET Framework 2 0 中 除了这一行之外 所有内容似乎都可以编译Privat
  • 如何让 Swagger 插件在自托管服务堆栈中工作

    我已经用 github 上提供的示例重新提出了这个问题 并为任何想要自己运行代码的人提供了一个下拉框下载链接 Swagger 无法在自托管 ServiceStack 服务上工作 https stackoverflow com questio
  • 复制 std::function 的成本有多高?

    While std function是可移动的 但在某些情况下不可能或不方便 复制它会受到重大处罚吗 它是否可能取决于捕获变量的大小 如果它是使用 lambda 表达式创建的 它依赖于实现吗 std function通常被实现为值语义 小缓
  • 在 LINQ 中按 Id 连接多表和分组

    我想按categoryId显示列表产品的名称组 这是我的代码 我想要我的视图显示结果 Desktop PC HP Red PC Dell Yellow PC Asus Red SmartPhone Lumia 720 Blue 我的组模型
  • 如何使用 LINQ2SQL 连接两个不同上下文的表?

    我的应用程序中有 2 个数据上下文 不同的数据库 并且需要能够通过上下文 B 中的表的右连接来查询上下文 A 中的表 我该如何在 LINQ2SQL 中执行此操作 Why 我们正在使用 SaaS 产品来跟踪我们的时间 项目等 并希望向该产品发
  • 从 Linux 内核模块中调用用户空间函数

    我正在编写一个简单的 Linux 字符设备驱动程序 以通过 I O 端口将数据输出到硬件 我有一个执行浮点运算的函数来计算硬件的正确输出 不幸的是 这意味着我需要将此函数保留在用户空间中 因为 Linux 内核不能很好地处理浮点运算 这是设
  • 使用自定义堆的类似 malloc 的函数

    如果我希望使用自定义预分配堆构造类似 malloc 的功能 那么 C 中最好的方法是什么 我的具体问题是 我有一个可映射 类似内存 的设备 已将其放入我的地址空间中 但我需要获得一种更灵活的方式来使用该内存来存储将随着时间的推移分配和释放的
  • C#:帮助理解 UML 类图中的 <>

    我目前正在做一个项目 我们必须从 UML 图编写代码 我了解 UML 类图的剖析 但我无法理解什么 lt
  • Azure 辅助角色“请求输入之一超出范围”的内部异常。

    我在辅助角色中调用 CloudTableClient CreateTableIfNotExist 方法 但收到一个异常 其中包含 请求输入之一超出范围 的内部异常 我做了一些研究 发现这是由于将表命名为非法表名引起的 但是 我尝试为我的表命
  • 如何禁用 fread() 中的缓冲?

    我正在使用 fread 和 fwrite 读取和写入套接字 我相信这些函数用于缓冲输入和输出 有什么方法可以在仍然使用这些功能的同时禁用缓冲吗 Edit 我正在构建一个远程桌面应用程序 远程客户端似乎 落后于服务器 我不知道可能是什么原因
  • C# 中的合并运算符?

    我想我记得看到过类似的东西 三元运算符 http msdn microsoft com en us library ty67wk28 28VS 80 29 aspx在 C 中 它只有两部分 如果变量值不为空 则返回变量值 如果为空 则返回默
  • 外键与独立关系 - Entity Framework 5 有改进吗?

    我读过了several http www ladislavmrnka com 2011 05 foreign key vs independent associations in ef 4 文章和问题 https stackoverflow
  • “接口”类似于 boost::bind 的语义

    我希望能够将 Java 的接口语义与 C 结合起来 起初 我用过boost signal为给定事件回调显式注册的成员函数 这非常有效 但后来我发现一些函数回调池是相关的 因此将它们抽象出来并立即注册所有实例的相关回调是有意义的 但我了解到的
  • 如何设置 log4net 每天将我的文件记录到不同的文件夹中?

    我想将每天的所有日志保存在名为 YYYYMMdd 的文件夹中 log4net 应该根据系统日期时间处理创建新文件夹 我如何设置它 我想将一天中的所有日志保存到 n 个 1MB 的文件中 我不想重写旧文件 但想真正拥有一天中的所有日志 我该如
  • C++ 函数重载类似转换

    我收到一个错误 指出两个重载具有相似的转换 我尝试了太多的事情 但没有任何帮助 这是那段代码 CString GetInput int numberOfInput BOOL clearBuffer FALSE UINT timeout IN
  • 不同类型指针之间的减法[重复]

    这个问题在这里已经有答案了 我试图找到两个变量之间的内存距离 具体来说 我需要找到 char 数组和 int 之间的距离 char data 5 int a 0 printf p n p n data 5 a long int distan
  • 我的班级应该订阅自己的公共活动吗?

    我正在使用 C 3 0 遵循标准事件模式我有 public event EventHandler
  • 如何从 ODBC 连接获取可用表的列表?

    在 Excel 中 我可以转到 数据 gt 导入外部数据 gt 导入数据 然后选择要使用的数据源 然后在提供登录信息后 它会给我一个表格列表 我想知道如何使用 C 以编程方式获取该列表 您正在查询什么类型的数据源 SQL 服务器 使用权 看
  • 从列表中选择项目以求和

    我有一个包含数值的项目列表 我需要使用这些项目求和 我需要你的帮助来构建这样的算法 下面是一个用 C 编写的示例 描述了我的问题 int sum 21 List

随机推荐

  • 【前端面试题】/【Vue】组件中的data为什么要定义成一个函数而不是一个对象?

    Q 组件中的data为什么要定义成一个函数而不是一个对象 A 因为当定义为一个数组 对象时候 我们改变data中其中一个数据的值的时候 会影响到其他的数据 导致数据污染 而定义为一个函数 则可以避免这个情况 参考 每个组件都是 Vue 的实
  • Cadence(OrCAD)原理图导入到PADS Layout遇到的问题和解决方法

    看到有网友留言说将Cadence画的原理图导入到PADS Layout中没有成功 先在Cadence中导出原理图的网表 当然这里的网表是PADS Layout支持的 asc格式 然后在PADS Layout导入该网表文件 最终出现提示错误的
  • ARM汇编指令

    ARM汇编指令 1 汇编语法 1 1 mov movw r0 63500 0xf80c 将63500放到r0寄存器的低八位中 movt r0 25667 0x6443f80c 将25667放到r0寄存器的高八位中 1 2 lsl 左移 st
  • (十)服务器K8S集群部署SpringBoot项目实战

    1 准备springboot项目 可以在 https start spring io 网站准备一个项目 这里作为k8s的学习所以springboot项目中准备一个简单的访问接口即可 2 服务器环境准备 安装Jdk 1 更新系统软件包 sud
  • MybatisPlus分页类型转换 不要在用循环转换了

    使用MybatisPlus查询的sql 返回的必须是一个对应表实体的泛型分页数据 我们给前端返回只需返回VO 我们可能会循环进行对象复制从新赋值 优化 MybatisPlus分页对象有直接转换的方法 优化前 最终分页对象 Page
  • openwrt中添加自定义驱动模块和APP

    驱动模块添加 1 make menuconfig中的 kernel modules 其中的各个配置选项来自于下面目录中的 mk文件 这里以other mk为对照 后续我们添加的驱动模块 添加到other分支当中 2 建立模块目录 路径是pa
  • 力扣 [104、111、222]

    文章目录 104 二叉树的最大深度 原题链接 思路 代码 111 二叉树的最小深度 原题链接 思路 代码 222 完全二叉树的节点个数 原题链接 思路 广度优先遍历 思路 深度优先遍历 代码 代码 104 二叉树的最大深度 原题链接 思路
  • 【HDLBits 刷题 4】Verilog Language(4)Procedures 和 More Verilog Features 部分

    目录 写在前面 Procedures Alwaysblock1 Alwaysblock2 Always if Always if2 Always case Always case2 Always casez Always nolatches
  • 数据挖掘算法之C4.5

    算法描述 C4 5算法是机器学习和数据挖掘领域中用于处理分类问题的算法 该算法是有监督学习类型的 即 给定一个数据集 每条记录都由一组特征来描述 每条记录仅属于一个标签 在给定的数据集上运行C4 5算法可以学习到一个从特征值到标签的映射 进
  • 力扣算法题(只用C)

    昨天写了力扣的五题算法题 虽然是五题简单题 不过对于初窥算法的我 感悟也是挺多 也小有成就感 毕竟是自己想出来 敲出来的 力扣上的算法题 和平时自己写的是不一样的 继续加油 nums是传入的给定整数数组 numsSize是数组长度 targ
  • 【整型提升】 【算术转换】【两千字详解】

    目录 1 隐式类型转换 1 1整型提升的概念 1 2整型提升的意义 1 3如何进行整型提升 负数的整形提升 正数的整形提升 无符号数的整形提升 1 4实战应用 2 算数转化 1 隐式类型转换 表达式求值的顺序一部分是由操作符的优先级和结合性
  • opencv旋转矩形定义以及求交叉面积

    目录 代码 运行结果 结果分析 用途 可以用来计算目标检测或者分割等结果IOU 代码 import cv2 旋转矩形的定义 中心点x 中心点y 宽 高 角度值 rect1 0 0 100 100 10 x y w h rect2 0 0 5
  • Ubuntu Workbench连接失败 your connection attempt failed for user ‘root‘ to the MySQL server at 127.0.0.1

    1 打开终端进入root帐号 2 进入 etc mysql debian cnf文件 查看debian sys maint帐号密码 3 运行 mysql u debian sys maint p 输入密码 4 修改root帐号的密码 ALT
  • 经纬度换算数值_经纬度换算

    1 经纬度和弧度的转换 转换方法 角度转弧度为 180 角度 弧度变角度为180 弧度 经度分东经和西经 从0 经线向东内到180 为东容经 用字母E表示 从0 经线向西到180 为西经 用字母W表示经度的变化规律是东经向东度数越来越大 西
  • VBA:按照Excel工作表中的名称列自动汇总多个工作薄中对应sheet中所需要的数据

    需求如下 B列为产品名为合并单元格 C列为供应商名 G H列为金额数据 数据源放在同一个文件夹内 B列产品名来源于工作薄名称中间的字符串 C列供应商名来源于工作薄中的sheet名 G H列金额数据来源于工作薄中sheet中固定单元格P25
  • c++函数指针

    1 声明函数指针 double cal int prototype double pf int 指针pf指向的函数 输入参数为int 返回值为double pf cal 指针赋值 2 指针作为函数的参数传递 void estimate in
  • Pytorch Tensor的索引与切片

    1 Pytorch风格的索引 根据Tensor的shape 从前往后索引 依次在每个维度上做索引 示例代码 import torch a torch rand 4 3 28 28 print a 0 shape 取到第一个维度 print
  • svg格式文件转换为png图片文件

    快要下班的时候 领导突然找我 发给我一个页面 说觉得这个页面的图标感觉不错 想把它做成图片放在项目里 我打开网页 用f12一看 用的是svg 这个我也不知道咋处理啊 但是遇到事情我们先不要慌 先在网上找找有没有解决办法 一顿搜索之下 我找到
  • 1. TensorRT量化的定义及意义

    前言 手写AI推出的全新TensorRT模型量化课程 链接 TensorRT下的模型量化 课程大纲如下 1 量化的定义及意义 1 1 什么是量化 定义 量化 Quantization 是指将高精度浮点数 如float32 表示为低精度整数
  • warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]

    下面的文章详细介绍了这个warning的来源和解决方法 也可以关闭优化 当然关闭优化并不是最终解决方法 down vote accepted First off let s examine why you get the aliasing