一种在结构中查找填充大小和位置的方法? [英] A way to find the size and location of padding in a struct?

查看:19
本文介绍了一种在结构中查找填充大小和位置的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个工具,它将一些包含结构的 C 代码作为输入.它将编译代码,然后查找并输出编译器决定添加到其中的结构的任何填充的大小和偏移量.对于使用 offsetof、sizeof 和一些添加的已知结构,这非常简单,但我无法找到一种简单的方法来为任何输入结构自动执行此操作.

I'm trying to write a tool that will take as input some C code containing structs. It will compile the code, then find and output the size and offset of any padding the compiler decides to add to structs within it. This is pretty straightforward to do by hand for a known struct using offsetof, sizeof, and some addition, but I can't figure out an easy way to do it automatically for any input struct.

如果我知道如何遍历结构中的所有元素,我想我可以毫无问题地编写该工具,但据我所知,没有办法做到这一点.我希望一些 StackOverflow 人会知道一种方法.但是,我并没有坚持我的方法,而且我当然愿意接受任何在结构中查找填充的替代方法.

If I knew how to iterate through all elements in a struct, I think I could get the tool written with no problems, but as far as I know there's no way to do that. I'm hoping some StackOverflow people will know a way. However, I'm not stuck in my approach, and I'm certainly open to any alternate approaches to finding padding in a struct.

推荐答案

这不就是什么pahole 是吗?

这篇关于一种在结构中查找填充大小和位置的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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