#include stdio.h#include stdlib.htypedef unsigned char u_char;#define NGX_ALIGNMENT sizeof(unsigned long) /* platform word */#define ngx_align(d, a) (((d) + (a - 1)) ~(a - 1)) /* In...
#include <stdio.h>
#include <stdlib.h>
typedef unsigned char u_char;
#define NGX_ALIGNMENT sizeof(unsigned long) /* platform word */
#define ngx_align(d, a) (((d) + (a - 1)) & ~(a - 1)) /* Integer multiple alignment */
#define ngx_align_ptr(p, a) (u_char *) (((uintptr_t) (p) + ((uintptr_t) a - 1)) & ~((uintptr_t) a - 1)) /* Pointers are aligned by integer multiples */
int main(int argc, char *argv[])
{
#ifndef __cplusplus
u_char* ch = (u_char *)0x00000065;
#else
u_char* ch = reinterpret_cast<u_char*>(0x00000065);
#endif
u_char* c = reinterpret_cast<u_char*>(0x00000068 & 0xFFFFFFFC);
//printf("((uintptr_t)(ch)+((uintptr_t)NGX_ALIGNMENT - 1)): 0x%p\n", ((uintptr_t)(ch)+((uintptr_t)NGX_ALIGNMENT - 1)));
//printf("~((uintptr_t) a - 1): 0x%p\n", ~((uintptr_t)NGX_ALIGNMENT - 1));
//printf("(0x00000068 & 0xFFFFFFFC) c: 0x%p\n", c);
printf("The alignment multiple is %u.\n", NGX_ALIGNMENT);
printf("Before memory alignment ch: 0x%p.\n", ch);
ch = ngx_align_ptr(ch, NGX_ALIGNMENT);
printf("After memory alignment ch: 0x%p.\n", ch);
return 0;
}
更新中。。。
沃梦达教程
本文标题为:C语言内存对齐
基础教程推荐
猜你喜欢
- C语言实现简易停车场管理系统 2023-03-13
- C语言预编译#define(预处理) 2023-04-03
- 如何告诉 MinGW 链接器不要导出所有符号? 2022-10-07
- C++高级数据结构之并查集 2023-04-20
- C/C++ Qt StatusBar底部状态栏应用教程 2023-01-10
- 漫画讲解C语言中最近公共祖先的三种类型 2023-01-01
- 使用VS2022开发在线远程编译部署的C++程序(图文详解) 2023-01-15
- C语言文件操作与相关函数介绍 2023-06-13
- C++类和对象到底是什么 2022-11-12
- 使用C/C++读写.mat文件的方法详解 2023-03-05
