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] 的相关文章

  • ASP MVC4 - 通过视图模型传递列表以查看

    我有一个模型人物 其中包括出生日期等字段 我想将所有人的列表以及每个人的计算年龄传递给视图 因此 视图模型 public class vm PersonList public Person Person get set public int
  • Microsoft Visual C++ 2008 和 R2007b 的 Mex 类型

    我想对 vs2008 和 matlab2007b 使用 mex 类型 我尝试了下面的代码 include
  • clang-tidy - 忽略第三方标头代码

    我正在为我的项目使用 CMake 并且我想向项目引入 clang tidy 检查 我用于此目的CMAKE CXX CLANG TIDY and clang tidy用于检查设置的文件 我想在 CI 中使用警告作为错误来可靠地检查提交是否引入
  • 这个洗牌算法有什么问题吗?

    我一直在做一些休闲假期计算 我的迷你项目是模拟意大利游戏 tomboli 一个关键的组成部分是对以下过程的模拟 游戏由一名男子控制 他拿着一袋 90 个弹珠 编号为 1 到 90 他从袋中随机取出一颗弹珠 每次向玩家喊出弹珠编号 经过一番思
  • ASP.NET Core 测试 - 没有方法 'public static IHostBuilder CreateHostBuilder(string[] args)

    我正在尝试在测试中设置我的应用程序并在中使用Startup s Configure method context Database EnsureCreated 并期待着Sqlite文件出现在Test sbin文件夹 这是我的代码 using
  • 返回 ObjectResult 会导致 406 Not Acceptable

    在学习 Scott Allen 的 Pluralsight 课程 Asp net Core 1 0 基础知识 时 在 MVC 框架中的控制器 模块和 操作结果 部分中 我在 Index 操作方法上遇到了 406 Not Acceptable
  • 通知另一个线程数据可用的最快方法是什么?有什么替代旋转的方法吗?

    我的一个线程将数据写入循环缓冲区 另一个线程需要尽快处理该数据 我本来想写这么简单的spin 伪代码 while true while a i do nothing just keep checking over and over proc
  • 如何测试 PARTIAL 视图在 C# ASP .NET MVC 中呈现

    我有一个视图 它内部有部分视图渲染 div class partialViewDiv Html RenderPartial partial Model SomeModelProperty div 和一个返回此视图的控制器 public Ac
  • 策略模式的现实示例

    我一直在读关于OCP原理 http en wikipedia org wiki Open closed principle以及如何使用策略模式来实现这一目标 我打算尝试向几个人解释这一点 但我能想到的唯一例子是根据 订单 的状态使用不同的验
  • 在javascript中调用c#函数[重复]

    这个问题在这里已经有答案了 可能的重复 从 Javascript 调用 ASP NET 函数 https stackoverflow com questions 3713 call asp net function from javascr
  • 如何使用实体框架更新特定记录的一个字段?

    我想要更新一个名叫 Pejman 的人的家庭情况 这是我的对象类 public class Person public int Id get set public string FirstName get set public string
  • 串行端口轮询和数据处理

    我正在尝试通过微控制器从传感器的多个串行端口读取数据 每个串口将接收超过2000个测量值 每个测量值7个字节 全部为十六进制 而且他们同时开火 现在我正在从 4 个串行端口进行轮询 另外 我将每个测量值转换为字符串并将其附加到字符串构建器
  • 为什么这些双精度数的返回值为-1.#IND?

    I have double score cvMatchContourTrees CT1 CT2 CV CONTOUR TREES MATCH I1 0 0 cout lt
  • C/C++ 特殊 CPU 功能的使用

    我很好奇 新的编译器是否使用了新 CPU 中内置的一些额外功能 例如 MMX SSE 3DNow 所以 我的意思是 在最初的 8086 中甚至没有 FPU 所以旧的编译器甚至不能使用它 但新的编译器可以 因为 FPU 是每个新 CPU 的一
  • C++ 中的 Ofstream 数组

    我想要在我的项目中使用 41 个输出文件来在其上写入文本 首先创建一个字符串数组list为了命名这些输出文件 然后我尝试定义一个 ofstream 对象数组并使用list命名它们 但我收到此错误 outfile cannot be used
  • 如何检查多个变量是否等于同一值?

    如何比较多个项目 例如 我希望检查所有变量 A B 和 C 是否都等于字符 X 或所有三个变量都等于 O 如果其中 2 个为 X 1 个为 O 则应返回 false I tried if A B C X A B C O Do whateve
  • 通过 boost::python 将 C++ 对象传递给 python 函数

    我想在 C 应用程序中使用嵌入 python 并调用 python 脚本中定义的函数 该函数的参数是一个 C 对象 看我的代码 class Test public void f std cout lt lt sss lt
  • 如果 foreach 是一个结构数组,它会复制每个元素吗?

    我有一个结构数组 做foreach运算符在迭代数组时复制每个元素 据我所理解foreach只是底层的语法糖转换为for 所以看来答案是否定的 但我很想得到一些确认 PS 看来应该有人已经问过了 但我无法轻易找到任何东西 因此 请以提供的参考
  • 捕获 System.Exception 总是不好的做法吗?

    请考虑下面的代码 它抛出三个不同的异常 即 System Configuration ConfigurationErrorsException System FormatException and System OverflowExcept
  • 如何注销多个非当前用户的会员用户?

    我正在使用属于 MVC2 默认项目一部分的 MembershipProvider 我希望能够获取用户名列表 注销用户 并在需要时销毁他们的会话 我能想到的最接近的是 foreach string userName in UserNames

随机推荐

  • 【前端面试题】/【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