编写网络包(存储在堆上)转换程序时,在hp-ux机器上运行时会遇到 si_code: 1 - BUS_ADRALN - Invalid address alignment. Please refer to the following link that helps in handling unaligned data 错误,经排查,是由于hp-ux 的cpu基于IA-64架构, 不支持内存随机存取,需要做字节序对齐操作。

当将内存数据类型进行强制转换后,随机读取的field1 地址很有可能不在一次可读取的地址上(如,地址为0X0001-0X0005), 此时便会抛出bus_adraln错误,导致宕机。

如:

struct SHead{
uint32_t field1;
uint32_t field2;
}; SHead *head = (SHead*)buf;
printf("%d\n", head->field1);

  一种解决方案是多一次内存拷贝,如:

struct SHead{
uint32_t field1;
uint32_t field2;
}; SHead head;
memcpy(&head, buf, sizeof(SHead));
printf("%d\n", head.field1);

  

msdn上对内存字节对齐的介绍:

Many CPUs, such as those based on Alpha, IA-64, MIPS, and SuperH architectures, refuse to read misaligned data. When a program requests that one of these CPUs access data that is not aligned, the CPU enters an exception state and notifies the software that it cannot continue. On ARM, MIPS, and SH device platforms, for example, the operating system default is to give the application an exception notification when a misaligned access is requested.

Misaligned memory accesses can incur enormous performance losses on targets that do not support them in hardware.

Alignment is a property of a memory address, expressed as the numeric address modulo a power of 2. For example, the address 0x0001103F modulo 4 is 3; that address is said to be aligned to 4n+3, where 4 indicates the chosen power of 2. The alignment of an address depends on the chosen power of two. The same address modulo 8 is 7.

An address is said to be aligned to X if its alignment is Xn+0.

对内存对齐不是很熟悉的同学请参考:

http://msdn.microsoft.com/en-us/library/ms253949(VS.80).aspx

编写跨平台代码之memory alignment的更多相关文章

  1. 在 C# 中使用 Span<T> 和 Memory<T> 编写高性能代码

    目录 在 C# 中使用 Span 和 Memory 编写高性能代码 .NET 中支持的内存类型 .NET Core 2.1 中新增的类型 访问连续内存: Span 和 Memory Span 介绍 C ...

  2. 如何编写跨平台的Java代码

    欢迎和大家交流技术相关问题: 邮箱: jiangxinnju@163.com 博客园地址: http://www.cnblogs.com/jiangxinnju GitHub地址: https://g ...

  3. “前.NET Core时代”如何实现跨平台代码重用 ——程序集重用

    除了在源代码层面实现共享("前.NET Core时代"如何实现跨平台代码重用 --源文件重用)之外,我们还可以跨平台共享同一个程序集,这种独立于具体平台的"中性" ...

  4. 微软良心之作——Visual Studio Code 开源免费跨平台代码编辑器

    微软良心之作——Visual Studio Code 开源免费跨平台代码编辑器 在 Build 2015 大会上,微软除了发布了 Microsoft Edge 浏览器和新的 Windows 10 预览 ...

  5. mvn编写主代码与测试代码

    maven编写主代码与测试代码 3.2 编写主代码 项目主代码和测试代码不同,项目的主代码会被打包到最终的构件中(比如jar),而测试代码只在运行测试时用到,不会被打包.默认情况下,Maven假设项目 ...

  6. maven编写主代码与测试代码

    3.2 编写主代码 项目主代码和测试代码不同,项目的主代码会被打包到最终的构件中(比如jar),而测试代码只在运行测试时用到,不会被打包.默认情况下,Maven假设项目主代码位于src/main/ja ...

  7. 通过Intel XDK编写跨平台app(二)

    通过Intel XDK编写跨平台app(一) 通过Intel XDK编写跨平台app(二) 在这个系列的上一篇文章中,我们大致了解了Interl XDK的概况.在这一部分中,我们会详细地介绍如何通过这 ...

  8. 通过Intel XDK编写跨平台app(一)

    Intel XDK 是一个新的跨平台手机应用开发工具.它努力把整个开发流程变的简单,尽可能把所有的平台都封装到一个包中,通过收集各种开发工具来使你的开发变的简单. 在这篇文章中,我将会向你介绍什么是I ...

  9. 用React Native编写跨平台APP

    用React Native编写跨平台APP React Native 是一个编写iOS与Android平台实时.原生组件渲染的应用程序的框架.它基于React,Facebook的JavaScript的 ...

随机推荐

  1. requirejs学习博客址分享

    1. http://blog.jobbole.com/30046/ 2. http://www.requirejs.cn/ 3. http://www.ruanyifeng.com/blog/2012 ...

  2. real-time application

    http://www.hanselman.com/blog/InstallingAndRunningNodejsApplicationsWithinIISOnWindowsAreYouMad.aspx ...

  3. 教你如何监控 Apache?

    什么是 Apache? Apache 是一款 HTTP 服务器软件,现在更名为 "http",而 Apache 则成了一个(包含httpd的项目)巨大的基金组织,根据习惯后文都用 ...

  4. 基于Maven的spring_security入门

    配置文件的修改点没什么变化,可以参考:http://blog.csdn.net/ouitiken/article/details/8830505 pom.xml的依赖参考: <dependenc ...

  5. java中的单例模式与doublecheck

    转自: http://devbean.blog.51cto.com/448512/203501 在GoF的23种设计模式中,单例模式是比较简单的一种.然而,有时候越是简单的东西越容易出现问题.下面就单 ...

  6. OA学习笔记-007-Dao层设计

    一. User, UserDao save(User user), update(), delete(), find(), ...Role, RoleDao save(Role role), upda ...

  7. Knockout绑定audio的pause事件导致音频无法停止

    ...时间过得真快, 一晃4天已经过去了, 然而自己并没有动笔写什么. 自省. 看了看今天的工作, 感觉好像没什么可写的. 不禁在想是不是一天一篇有点儿难. 再一想, 这分明就是在给自己找理由. 就是 ...

  8. android 世界各国英文简写代码 资源文件

    今日又用到这段代码,忽然感觉到如果是第一次用的人肯定也会很麻烦.故在此上传一份.后人再用就不必重复做此工作.跟体育老师学过语文,见谅. 提供下载地址 http://download.csdn.net/ ...

  9. 转:十八、java中this的用法

    http://blog.csdn.net/liujun13579/article/details/7732443 我知道很多朋友都和我一样:在JAVA程序中似乎经常见到“this”,自己也偶尔用到它, ...

  10. 【Node】SuperAgent

    What is the current mechanism to construct a query string with SuperAgent?http://stackoverflow.com/q ...