微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用直接I / O进行数据传输的内存alignment要求

我正在阅读Michael Kerrisk的“Linux编程接口”。 我正在通过memalign()用于alignment要求的示例 。

代码评论对我来说没有意义。 任何人都可以解释为什么我们需要2 *alignment?

/* memalign() allocates a block of memory aligned on an address that is a multiple of its first argument. By specifying this argument as 2 * 'alignment' and then adding 'alignment' to the returned pointer,we ensure that 'buf' is aligned on a non-power-of-two multiple of 'alignment'. We do this to ensure that if,for example,we ask for a 256-byte aligned buffer,we don't accidentally get a buffer that is also aligned on a 512-byte boundary. */ buf = memalign(alignment * 2,length + alignment); if (buf == NULL) errExit("memalign"); buf += alignment;

如果我创build了一个stream程,这是否意味着我将永远能够终止它?

C#:如何判断EXE是否有图标?

使用xbuild编译visual c ++项目时定义一个目标

fgets在Linux上发出更长的read()调用

修改此程序以使用getCommandLine()

如何产生不会与父母一起死亡的儿童stream程?

有没有办法为另一个进程设置一个令牌?

QT发送信号到另一个QT应用程序?

Linux上C语言中的POSIX线程和全局variables

multithreading应用程序崩溃,出现内存使用率高的错误R6016或0xC0000005

作者在这里想要一个有n字节对齐但不是2n字节对齐的缓冲区,可能是因为对齐不够或者类似的东西(我没有这本书)来演示失败。

他通过要求2n字节对齐的缓冲区(显然也有n字节对齐)然后添加n来实现这一点。 这打破了2n字节的对齐,但保持了n字节对齐。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐