如何找到类型的大小? [英] How can I find the size of a type?

查看:48
本文介绍了如何找到类型的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我手里拿着一个 Type * .如何找到以位/字节为单位的大小(此类型的大小对象将在内存中占据)?我看到各种各样的方法都可以让我获得原始"或标量"大小,但这对聚合类型无济于事...

I'm holding a Type* in my hand. How do I find out its size (the size objects of this type will occupy in memory) in bits / bytes? I see all kinds of methods allowing me to get "primitive" or "scalar" size, but that won't help me with aggregate types...

推荐答案

大小取决于目标(出于多种原因,对齐是其中之一).

The size depends on the target (for several reasons, alignment being one of them).

在LLVM 3.2和更高版本中,您需要在 DataLayout 中使用特别是其 getTypeAllocSize 方法.这将返回以字节为单位的大小,还有一个名为 getTypeAllocSizeInBits 的位版本.DataLayout 实例可以通过从当前模块创建它来获得:DataLayout* TD = new DataLayout(M).

In LLVM versions 3.2 and above, you need to use DataLayout, in particular its getTypeAllocSize method. This returns the size in bytes, there's also a bit version named getTypeAllocSizeInBits. A DataLayout instance can be obtained by creating it from the current module: DataLayout* TD = new DataLayout(M).

对于LLVM最高为3.1版(包括),请使用 TargetData 而不是 DataLayout .不过,它公开了相同的 getTypeAllocSize 方法.

With LLVM up to version 3.1 (including), use TargetData instead of DataLayout. It exposes the same getTypeAllocSize methods, though.

这篇关于如何找到类型的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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