#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语言内存对齐
基础教程推荐
猜你喜欢
- character-encoding – Linux中最常见的C语言编码(和Unix?) 2023-11-21
- Qt数据库应用之实现通用数据库请求 2023-03-18
- g++: const 丢弃限定符 2022-10-07
- VisualStudio2010安装教程 2023-01-05
- 05-C语言进阶——动态内存管理 2023-11-20
- C语言的三种条件判断语句你都了解吗 2023-03-05
- 纯C++代码详解二叉树相关操作 2023-05-15
- C语言植物大战数据结构二叉树递归 2023-04-09
- 利用QT设计秒表功能 2023-05-30
- C语言数组长度的计算方法实例总结(sizeof与strlen) 2023-04-26
