什么是意义“_attribute _((排列(4)));”在第一线? [英] What is meaning of the '_attribute_((aligned(4)));' in the first line?

查看:139
本文介绍了什么是意义“_attribute _((排列(4)));”在第一线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 字符BUF [BUF_LEN] _attribute _((排列(4)));
ssiz​​e_t供LEN,I = 0;
/ *读取BUF_LEN字节的价值的事件* /
LEN =读(FD,BUF,BUF_LEN);
/ *遍历每个读取事件,直到没有剩余* /
而(I< LEN){
结构inotify_event *事件=
(结构inotify_event *)及的buf [I];
监控文件活动| 239
的printf(WD =%d个面具=%d个饼干=%d个LEN =%d个DIR =%s的\\ n
事件 - > WD,事件 - >面膜,
事件 - >饼干,事件 - > LEN,
(事件 - >&面膜放大器; IN_ISDIR)? 是:无);
/ *如果有一个名字,打印* /
如果(事件 - > LEN)
的printf(NAME =%S \\ n,事件 - >名);
/ *更新索引到下一个事件的开始* /
I + = sizeof的(结构inotify_event)+事件 - > LEN;
}


解决方案

 字符BUF [BUF_LEN] _attribute _((排列(4)));

它指定一个最小对齐以字节为单位测量的变量 BUF 。结果
它使编译器分配一个4字节边界上的变量 BUF

应该是一个很好看的。

char buf[BUF_LEN]_attribute_((aligned(4)));
ssize_t len, i = 0;
/* read BUF_LEN bytes' worth of events */
len = read (fd, buf, BUF_LEN);
/* loop over every read event until none remain */
while (i < len) {
struct inotify_event *event =
(struct inotify_event *) &buf[i];
Monitoring File Events | 239
printf ("wd=%d mask=%d cookie=%d len=%d dir=%s\n",
event->wd, event->mask,
event->cookie, event->len,
(event->mask & IN_ISDIR) ? "yes" : "no");
/* if there is a name, print it */
if (event->len)
printf ("name=%s\n", event->name);
/* update the index to the start of the next event */
i += sizeof (struct inotify_event) + event->len;
}

char buf[BUF_LEN]_attribute_((aligned(4)));

It specifies a minimum alignment for the variable buf, measured in bytes.
It causes the compiler to allocate the variable buf on a 4-byte boundary.

This should be a good read.

这篇关于什么是意义“_attribute _((排列(4)));”在第一线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆