Noticed that several windows API's take a size parameter.
i.e. one of the members of the struct is dwSize which is supposed
to be initialized by client code, by taking the sizeof of the struct.
I assume this is to enable versioning of structures
/* first version of struct */
typedef struct { int dwSize; type1 t1; } A;
/* 2nd version of struct in new version of the library */
typedef struct { int dwSize; type1 t1; } A_v1; // Old struct renamed to
A_v1
typedef struct { int dwSize; type1 t1; type 2;} A;
So their implementation of function will look like
// Impl in v1 of Lib
void fn(A * ptr) { /* Implementation */ }
//Impl in v2 of lib
void fn_v1(A_v1 * ptr) { /* Renamed old fn to fn_v1 - Implementation */
void fn(A * ptr)
{
if (ptr->dwSize == sizeof(A_v1)
{
fn_v1(); return;
}
/* New Implementation Below */
}
Is my assumption correct - is this something like what they would
be doing?
Is this fully standard compliant or are they taking liberties with their
close knowledge of the internals of the compiler.
Assume lib_v1 is compiled with compiler cl_v1 & lib_v2 is compiled
with compiler cl_v2, then it is safe to assume that sizeof(A_v1) would
be the same when compiled with different versions of the same compiler?
i.e. can sizeof(A_v1) can when compiled with gcc v2 as compared to
gcc v1?
If it cannot, then can it change when compiled by two totally different
compilers?
Any other gotchas?
i.e. one of the members of the struct is dwSize which is supposed
to be initialized by client code, by taking the sizeof of the struct.
I assume this is to enable versioning of structures
/* first version of struct */
typedef struct { int dwSize; type1 t1; } A;
/* 2nd version of struct in new version of the library */
typedef struct { int dwSize; type1 t1; } A_v1; // Old struct renamed to
A_v1
typedef struct { int dwSize; type1 t1; type 2;} A;
So their implementation of function will look like
// Impl in v1 of Lib
void fn(A * ptr) { /* Implementation */ }
//Impl in v2 of lib
void fn_v1(A_v1 * ptr) { /* Renamed old fn to fn_v1 - Implementation */
void fn(A * ptr)
{
if (ptr->dwSize == sizeof(A_v1)
{
fn_v1(); return;
}
/* New Implementation Below */
}
Is my assumption correct - is this something like what they would
be doing?
Is this fully standard compliant or are they taking liberties with their
close knowledge of the internals of the compiler.
Assume lib_v1 is compiled with compiler cl_v1 & lib_v2 is compiled
with compiler cl_v2, then it is safe to assume that sizeof(A_v1) would
be the same when compiled with different versions of the same compiler?
i.e. can sizeof(A_v1) can when compiled with gcc v2 as compared to
gcc v1?
If it cannot, then can it change when compiled by two totally different
compilers?
Any other gotchas?
Comment