如何通过查看地址来获得结构的大小 [英] how to get the size of a structure by looking at address

查看:96
本文介绍了如何通过查看地址来获得结构的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人

是否有任何方法可以通过在调试时在结构成员的地址上滑动来获取结构成员消耗了多少字节.
例如-
我有一个结构strt1,它的成员之一是a1现在strt1-> a1的地址是
0x00fe4984
我们可以计算出该成员携带的大小吗?

我很抱歉,如果这是一个愚蠢的问题,但我无法计算其大小:(

Dear All

Is there any way to get that how many bytes is being consumed by a structure member, by looikng at its address at debug time.
for example-
i have a struct strt1 its one of member is a1 now address of strt1->a1 is
0x00fe4984
can we calculate the size this member is carrying?

i am sorry if its a silly problem but i could not calculate its size :(

推荐答案

如果您有源代码,它是微不足道的.在已取消引用的结构地址上使用sizeof .但是我想你是在问什么时候没有代码.

获取结构大小的唯一方法是分析内存内容.即使在那儿,您也必须有一点点主意才能将垃圾"与数据分开.在调试模式下,某些内存地址似乎是固定的,并且东西经常被加载到那里.如果可以的话,请先进行几次虚拟调试,然后再了解地址.然后在该区域中加载适当大小的虚拟数组,将其删除,然后在该区域中加载此结构.额外的CD(或任何其他字节)字节必须能够帮助将垃圾"与结构数据分开.这并不是万无一失,因为结构的数据可能与字节一致.

然而,尽管做出了这些努力,您将只知道整个结构的大小.我的意思是,您将无法确定结构是否包含八个字符或四个短裤或两个整数或一个双精度数组.这不是不可能的,但是您在解码原始字节时必须做很多肮脏的工作.
If you have the source it is trivial. Use sizeof on the dereferenced structure address. But I suppose you are asking when you don''t have the code.

The only way to get the size of the structure is to analyze the memory content. Even there you must have a fair bit of idea to separate the "junk" from the data. In debug mode, certain memory addresses seem fixed and things get loaded there quite frequently. If you can, first get to know the address after a few dummy debugs. Then load a dummy array of a suitably large size in that area, delete it and then load this structure in that area. The extra CD (or whatever) bytes must be able to help separate "junk" from the structure''s data. This isn''t fool proof since the data of the structure might coincide with the bytes.

Yet, in spite of these efforts, all you will get to know is the size of the entire structure. I mean you won''t be able to make out if the structure has an array of eight characters or four shorts or two integers or just a double. It isn''t impossible but you have to do a lot of dirty work in decoding the raw bytes.


成员的大小?不.这就像试图通过仅查看街道编号来判断房屋的大小.无法完成.
Size of the member?? No. That''s like trying to judge the size of a house by simply looking at it''s street number. Can''t be done.


如果已对结构进行了动态分配(即通过调用mallocnew),则可以使用
If the structure has been allocated dinamically (i.e. through a call to malloc or new), you can use the _msize (CRT)[^] function to get the size of the allocated memory block; however that could not be the size of the structure (e.g. if you use it on the pointer obtained by calling new MyStruct[10] what you get is sizeof(MyStruct) * 10).

Anyway, why couldn''t you use the sizeof operator?


这篇关于如何通过查看地址来获得结构的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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