Click here to Skip to main content
15,893,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted

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
 
Share this answer
 
v2
Comments
Espen Harlinn 24-Mar-11 17:30pm    
like data serialization, my 5
Sergey Alexandrovich Kryukov 24-Mar-11 21:33pm    
Thank you.
--SA
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);



Here sizeof(MyData) is different with actual allocated memory.
 
Share this answer
 
v3
Comments
Espen Harlinn 24-Mar-11 17:30pm    
my 5
Sergey Alexandrovich Kryukov 24-Mar-11 21:34pm    
Agree, a 5. Thank your for building of the whole example.
I was trying to find example from Windows API but could not remember quick enough.
--SA
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.
 
Share this answer
 
v2
Comments
CPallini 24-Mar-11 16:24pm    
Good. That was missing in other answers.
Espen Harlinn 24-Mar-11 17:29pm    
Agree, my 5
Sergey Alexandrovich Kryukov 24-Mar-11 21:44pm    
Extension is another aspect (data model evolution, kind of). Good point, a 5.
Variable run-time size of array is still most usual technique.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900