No Memory Alignment with GCC
attributemethod:#include <stdio.h> struct packed
{
char a;
int b;
} __attribute__((packed)); struct not_packed
{
char a;
int b;
}; int main(void)
{
printf("Packed: %zu\n", sizeof(struct packed));
printf("Not Packed: %zu\n", sizeof(struct not_packed));
return 0;
}Output:
$ make example && ./example
cc example.c -o example
Packed: 5
Not Packed: 8
pragma packmethod:#include <stdio.h> #pragma pack(1)
struct packed
{
char a;
int b;
};
#pragma pack() struct not_packed
{
char a;
int b;
}; int main(void)
{
printf("Packed: %zu\n", sizeof(struct packed));
printf("Not Packed: %zu\n", sizeof(struct not_packed));
return 0;
}Output:
$ make example && ./example
cc example.c -o example
Packed: 5
Not Packed: 8Add-fpack-structto GCC- -fpack-struct[=n]
- Without a value specified, pack all structure members together without holes. When a value is specified (which must be a small power of two), pack structuremembers according to this value, representing the maximum alignment (that is, objects with default
alignment requirements larger than this will be outputpotentially unaligned at the next fitting location.Warning: the -fpack-struct switch causes
GCC to generate code that is not binary compatible with code generated without thatswitch. Additionally, it makes the code suboptimal. Use it to conform to a non-default application binary interface.
No Memory Alignment with GCC的更多相关文章
- 还是说Memory Model,gcc的__sync_synchronize真是太坑爹了
还是说Memory Model,gcc的__sync_synchronize真是太坑爹了! 时间 2012-01-29 03:18:35 IT牛人博客聚合网站 原文 http://www.udpw ...
- 从硬件到语言,详解C++的内存对齐(memory alignment)
转载请保留以下声明 作者:赵宗晟 出处:https://www.cnblogs.com/zhao-zongsheng/p/9099603.html 很多写C/C++的人都知道“内存对齐”的概念以及规则 ...
- 从硬件到语言,详解C++的内存对齐(memory alignment)(一)
作者:赵宗晟 出处:https://www.cnblogs.com/zhao-zongsheng/p/9099603.html 很多写C/C++的人都知道“内存对齐”的概念以及规则,但不一定对他有很深 ...
- 编写跨平台代码之memory alignment
编写网络包(存储在堆上)转换程序时,在hp-ux机器上运行时会遇到 si_code: 1 - BUS_ADRALN - Invalid address alignment. Please refer ...
- GNU C - 关于8086的内存访问机制以及内存对齐(memory alignment)
一.为什么需要内存对齐? 无论做什么事情,我都习惯性的问自己:为什么我要去做这件事情? 是啊,这可能也是个大家都会去想的问题, 因为我们都不能稀里糊涂的或者.那为什么需要内存对齐呢?这要从cpu的内存 ...
- #define barrier() __asm__ __volatile__("": : :"memory") 中的memory是gcc的东西
gcc内嵌汇编简介 在内嵌汇编中,可以将C语言表达式指定为汇编指令的操作数,而且不用去管如何将C语言表达式的值读入哪个寄存器,以及如何将计算结果写回C 变量,你只要告诉程序中C语言表达式与汇编指令操作 ...
- C++ Standards Support in GCC - GCC 对 C++ 标准的支持
C++ Standards Support in GCC - 2019-2-20 GCC supports different dialects of C++, corresponding to th ...
- reds Virtual Memory
Virtual Memory technical specification This document details the internals of the Redis Virtual Memo ...
- (C/C++) Interview in English. - Memory Allocation/Deallocation.
Q: What is the difference between new/delete and malloc/free? A: Malloc/free do not know about const ...
随机推荐
- pep-8要求归纳
代码布局缩进每个缩进级别使用4个空格. 连续行应使用Python的隐式行连接括号,括号和大括号,或使用悬挂缩进 来垂直对齐包装元素.当使用悬挂式缩进时,应考虑以下内容:第一行应该没有任何争论,应该使用 ...
- 使用Phaser开发你的第一个H5游戏(一)
本文来自网易云社区 作者:王鸽 不知你是否还记得当年风靡一时的2048这个游戏,一个简单而又不简单的游戏,总会让你在空闲时间玩上一会儿. 在这篇文章里,我们将使用开源的H5框架--Phaser来重现这 ...
- PHP过滤器 filter_has_var() 函数
定义和用法 filter_has_var() 函数检查是否存在指定输入类型的变量. 如果成功则返回 TRUE,如果失败则返回 FALSE. 语法 filter_has_var(type, variab ...
- 13-数组的API方法遍历
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- 构建maven的web项目时注意的问题(出现Error configuring application listener of class org.springframework.web.context.ContextLoaderListener 或者前端控制器无法加载)
构建项目后或者导入项目后,我们需要bulid path--->config build path 特别是maven的依赖一定要 发布到WEB_INF的lib下面,不然在发布项目的时候,这些依赖都 ...
- AVPlayerViewController视频播放器
前言 iOS8之后系统自带使用AVPlayerViewController播放视频 AVPlayerViewController AVPlayerViewController和导航控制器差不多,需要将 ...
- BZOJ4446 [Scoi2015]小凸玩密室 【树形Dp】
题目 小凸和小方相约玩密室逃脱,这个密室是一棵有n个节点的完全二叉树,每个节点有一个灯泡.点亮所有灯 泡即可逃出密室.每个灯泡有个权值Ai,每条边也有个权值bi.点亮第1个灯泡不需要花费,之后每点亮4 ...
- Explosion at Cafebazaar
Explosion at Cafebazaar 时间限制: 1 Sec 内存限制: 128 MB 题目描述 You are an engineer at Cafebazaar and your sp ...
- 作诗(bzoj 2821)
Description 神犇SJY虐完HEOI之后给傻×LYD出了一题:SHY是T国的公主,平时的一大爱好是作诗.由于时间紧迫,SHY作完诗 之后还要虐OI,于是SHY找来一篇长度为N的文章,阅读M次 ...
- *Codeforces989D. A Shade of Moonlight
数轴上$n \leq 100000$个不重叠的云,给坐标,长度都是$l$,有些云速度1,有些云速度-1,风速记为$w$,问在风速不大于$w_{max}$时,有几对云可能在0相遇.每一对云单独考虑. 多 ...