结构与结构运算符大小 [英] Structures & sizeof operator

查看:58
本文介绍了结构与结构运算符大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在某些情况下,结构中的某个成员需要该结构的大小.在某些情况下,该成员将是其他而不是结构的大小.

如果不是,那么有人知道为什么该结构或要求该结构本身不这样做吗?

谢谢,

Barry

In some cases, a structure has a member that wants the size of the structure. Is there a case where that member will be something other than the size of the structure.

If not, then does anyone know why the structure or what requires the structure doesn''t do it itself?

Thanks,

Barry

推荐答案

是的,可以不同.尽管我认为它是一种肮脏的样式,但这种样式在Windows API中很流行.

结构的最后一个字段的声明类似于SOME_TYPE[1],但是结构的用户应该在结构的最后一个存储区中存储任何其他大小的数组. API接收指向结构的指针;因此,有关结构实际大小的唯一信息是在结构的Size字段中传递的;通常这是第一个领域.

我猜想这样做是为了避免过多的指针指向和对内存分配的依赖(例如,结构可以在堆栈上,但用于字段的一些内存是在堆中分配的,因此用户在使用后需要清理结构).

-SA
Yes, it can be different. Even though I consider it as a dirty style, this style is popular in Windows API.

Last field of structure is declared like SOME_TYPE[1], but the user of the structure is supposed to store the array of any other size in continues memory of the structure, at the very end. The API receives the pointer to the structure; so the only information about the actual size of the structure is passed in the Size field of the structure; usually this is a first field.

I guess this is done to avoid excessive pointers on pointers and dependency on memory allocation (when, say, the structure can be on stack but some memory for the fields is allocated in heap, so the user would need to clean up the structure after use).

—SA


Windows API通常需要结构大小,当然还有一个原因. 我会举一个简单的例子:
Often Windows API require struct size and of course there''s a reason why.
I''ll give a simple(r) example:
typedef struct TagMyData
{
   DWORD dwSize;
   DWORD dwVersion;
   BYTE bData[1];
} MyData;

int i;
MyData * pData = (MyData*) malloc(sizeof(MyData) + 9));
pData->dwVersion=0x00000001;
pData->dwSize = sizeof(MyData) + 9;
for (i=0;i<10;i++)
  pData->bData[i] = i;

/*
 ... 
 here you may call a function expecting such variable-size struct
 ...
*/

free(pData);




此处的sizeof(MyData)与实际分配的内存有所不同.




Here sizeof(MyData) is different with actual allocated memory.


结构的大小在编译时是固定的,因此可以确定程序是针对哪个Windows版本设计的.执行时间.

假设您使用 OPENFILENAME [
The size of the structure is fixed at compile time, this makes it possible to identify for which Windows version a program was designed, at execution time.

Suppose you use the OPENFILENAME[^] structure and you define your Windows version so that it will run on Windows NT 4.0. This means some members are not included in the structure when you compile it, and the sizeof() function will return a smaller result.
Now if you were to execute your program on Windows 2000 (NT 5.0), the lStructSize variable will make clear that the extra variables are not to be used because your program did not allocate memory for them.

So the conclusion is, this allows for expansion of the struct while maintaining backward compatibility.


这篇关于结构与结构运算符大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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