获取 c++ 的字节大小以字符串形式给出的结构

发布于 2025-01-12 13:23:45 字数 370 浏览 3 评论 0原文

我有以下问题。我得到了一根绳子。该字符串代表一个有效的 C++ 结构。

我需要找出获取此结构的字节大小的最快方法。

对我来说最明显的方法是:

将结构写入文件并将该文件转换为将大小写入文件的脚本,从系统调用编译器,编译文件,运行可执行文件,解析文件。

这是荒谬的,必须有更好的方法。

我听说过 GCC 的 JITC 库,这可能是更好的方法。但由于我不需要完整的编译,可能有更简单的方法吗?

该结构的表示方式与本机 C++ 代码中的表示方式相同,即我有这个字符串

“结构MySstruct { int 字段1; 无效*字段2; 短场3; };"

将是字符串的内容。

I have the following problem. I am given a string. This string represents a valid c++ struct.

I need to figure out the fastest way to get the size in bytes of this struct.

The most obvious approach to me is:

Write struct to a file and turn that file into a script that writes the size to a file, call a compiler from the system, compile the file, run the executable, parse the file.

This is absurd, there has to be a better way.

I have heard about GCC's JITC library, that might be a better approach. But since I don;t need full blown compilation there might be a simpler way?

The struct is represented as it would in native c++ code, i.e. I have this string

"struct MySstruct {
int field1;
void* field2;
short field3;
};"

Would be the content of the string.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文