Zero Initialized Array

Hi Experts,

I am developing a windows driver,in which I have the following structure. In the structure am supposed to use multiple zero initialized arrays to get the exact offset value. But when i tried to use like that I got C2229 error which says that zero initialized array should be the last member of the structure. What should I need to do so that I can retain my structure.

struct My _Struct{
__be64 i_01[2633];

__be64 base_addr_atsp[3]; /* Offset 0x5248 */

__be64 i_03[41181];

__be64 base_addr_ah[3]; /* Offset 0x55948 */

__be64 i_04[0];*/

__be64 base_dr_qp[3]; /* Offset 0x55960 */

__be64 i_05[2174];

__be64 base_addr_sq_cmpl[3]; /* Offset 0x59d68 */

__be64 i_06[0];

__be64 base_key[3]; /* Offset 0x59d80 */

__be64 i_07[14];

__be64 base_rqsp[3]; /* Offset 0x59e08 */

__be64 i_08[0];

__be64 base_addr_rq_sw[3]; /* Offset 0x59e20 */

__be64 i_09[0];

__be64 base_rq_hw[3]; /* Offset 0x59e38 */

__be64 i_10[52175];

__be64 ibpr_p1_mac1; /* Offset 0xbfcb8 */

__be64 i_11[0];

__be64 ibpr_p1_mac2; /* Offset 0xbfcc0 */

__be64 i_12[0];

__be64 ibpr_p2_mac1; /* Offset 0xbfcc8 */

__be64 i_13[0];

__be64 ibpr_p_mac2; /* Offset 0xbfcd0 */

__be64 i_14[2316];

__be64 base_sq_sw[3]; /* Offset 0xc4548 */

__be64 i_15[0];

__be64 base_sq_hw[3]; /* Offset 0xc4560 */

__be64 i_16[244];

__be64 base_sq_ring[3]; /* Offset 0xc4d18 */

__be64 i_17[71];

__be64 base_sq_rspq[3]; /* Offset 0xc4f68 */

__be64 i_18[129];

__be64 base_sq_tvl[3]; /* Offset 0xc5388 */

__be64 i_19[4244];

__be64 base_cq_sw[3]; /* Offset 0xcd7a0 */

__be64 i_20[0];

__be64 base_cq_hw[3]; /* Offset 0xcd7b8 */

__be64 i_21[580];

}; /* struct My_Struct [6770560 bits] */

Thanks in Advance

Ummmm… the error is self-explanatory. It’s not a zero INITIALIZED array, it’s a zero SIZED array that’s the problem.

You can’t have an array with zero elements in a structure except as the last item in the structure.

What do you want to use these zero-size array elements for? If you tell us, we’ll tell you the “right” way to do this in the Windows world.

Peter
OSR
@OSRDrivers

Yes Peter. Its an issue related with zero-sized array. Actually the above structure is used to fetch the exact offset value. The above structure was supported in GCC compiler and it gives the struct size exactly. But in Windows, when i used the above structure it threws C2999 errors.

So ,what I tried is manually calculate the value and it is possible to fetch the exact offset value for a particular member. But my wish is to know is there any possibility to use such zero-sized array in-between the structure.

I have gone through so many links. What i could get is C can have 0-sized array as last member.

My question is “Is that possible to use multiple 0-sized array in a structure?”.

That’s all Peter.

Yes Peter. Its an issue related with zero-sized array.

Actually the above structure is used to fetch the exact offset value. The above structure was supported in GCC compiler and it gives the struct size exactly.

But in Windows, when i used the above structure it threws C2229 errors.

So ,what I tried is manually calculate the value and it is possible to fetch the exact offset value for a particular member EVEN WITHOUT USING THAT ZERO SIZED ARRAYS.

But my wish is to know is there any possibility to use such zero-sized array in-between the structure. I have gone through so many links. What i could get is C can have 0-sized array as last member.

My question is “Is that possible to use multiple 0-sized array in a structure?”.

That’s all Peter.

You understand that your question becomes, effectively, “I don’t understand C, what do I do now”??

Peter
OSR
@OSRDrivers

There is a macro called FIELD_OFFSET that will do exactly what you want in
the WDK, and it is portable so you could back port it to GCC.

Don Burn
Windows Driver Consulting
Website: http://www.windrvr.com

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of xxxxx@gmail.com
Sent: Wednesday, May 11, 2016 9:49 AM
To: Windows System Software Devs Interest List
Subject: RE:[ntdev] Zero Initialized Array

Yes Peter. Its an issue related with zero-sized array.

Actually the above structure is used to fetch the exact offset value. The
above structure was supported in GCC compiler and it gives the struct size
exactly.

But in Windows, when i used the above structure it threws C2229 errors.

So ,what I tried is manually calculate the value and it is possible to
fetch the exact offset value for a particular member EVEN WITHOUT USING THAT
ZERO SIZED ARRAYS.

But my wish is to know is there any possibility to use such zero-sized
array in-between the structure. I have gone through so many links. What i
could get is C can have 0-sized array as last member.

My question is “Is that possible to use multiple 0-sized array in a
structure?”.

That’s all Peter.


NTDEV is sponsored by OSR

Visit the list online at:
http:

MONTHLY seminars on crash dump analysis, WDF, Windows internals and software
drivers!
Details at http:

To unsubscribe, visit the List Server section of OSR Online at
http:</http:></http:></http:>

This structure is obviously auto-generated by some script. You need to fix the script to make it NOT issue the zero sized fillers when they’re not needed. Or you can write another script to prune the zero-sized fillers.

xxxxx@gmail.com wrote:

Yes Peter. Its an issue related with zero-sized array. Actually the above structure is used to fetch the exact offset value. The above structure was supported in GCC compiler and it gives the struct size exactly. But in Windows, when i used the above structure it threws C2999 errors.

What you are doing violates the C standard. A zero-length array is only
allowed at the end of a structure. GCC allows it in the middle as an
extension, but they caution that it can cause unexpected results.

You can achieve your goal in a standard-compliant way by using unions,
but I wonder if you could achieve the same effect using #defines.

My question is “Is that possible to use multiple 0-sized array in a structure?”.

No. That’s not allowed in standard C.


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.

Think about your question for a moment. A structure is, by definition, a way of grouping arbitrary memory into some kind of structured sub-allocations ? the members. This allows the functions that used the blob of memory to access specific parts of it by name instead of using pointer math and greatly reduces the chances of screwing stuff up from otherwise innocuous looking code. A zero sized array is a noop in terms of structure layout as it will not point to any memory location . An unsized array in a structure is a member that has no definite size and therefore cannot occupy any location in the structure other than the last member or else the remaining fields will be impossible to locate at compile time ? rendering the structure useless.

Sent from Mailhttps: for Windows 10

From: xxxxx@gmail.commailto:xxxxx
Sent: May 11, 2016 9:48 AM
To: Windows System Software Devs Interest Listmailto:xxxxx
Subject: RE:[ntdev] Zero Initialized Array

Yes Peter. Its an issue related with zero-sized array.

Actually the above structure is used to fetch the exact offset value. The above structure was supported in GCC compiler and it gives the struct size exactly.

But in Windows, when i used the above structure it threws C2229 errors.

So ,what I tried is manually calculate the value and it is possible to fetch the exact offset value for a particular member EVEN WITHOUT USING THAT ZERO SIZED ARRAYS.

But my wish is to know is there any possibility to use such zero-sized array in-between the structure. I have gone through so many links. What i could get is C can have 0-sized array as last member.

My question is “Is that possible to use multiple 0-sized array in a structure?”.

That’s all Peter.


NTDEV is sponsored by OSR

Visit the list online at: http:

MONTHLY seminars on crash dump analysis, WDF, Windows internals and software drivers!
Details at http:

To unsubscribe, visit the List Server section of OSR Online at http:</http:></http:></http:></mailto:xxxxx></mailto:xxxxx></https:>

Marion Bond wrote:

Think about your question for a moment. A structure is, by
definition, a way of grouping arbitrary memory into some kind of
structured sub-allocations – the members. This allows the functions
that used the blob of memory to access specific parts of it by name
instead of using pointer math and greatly reduces the chances of
screwing stuff up from otherwise innocuous looking code. A zero sized
array is a noop in terms of structure layout as it will not point to
any memory location . An unsized array in a structure is a member
that has no definite size and therefore cannot occupy any location in
the structure other than the last member or else the remaining fields
will be impossible to locate at compile time – rendering the structure
useless.

Well, I understand what he was trying to do there. This structure is
being automatically generated, and there are pad elements being inserted
at various locations to ensure correct alignment and to allow aliased
references to different sections. In those particular cases, there is
no padding required.

As I said in my earlier response, what he’s doing here is simulating a
union, and it would be better implemented as such.


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.

Zero-sized struct fields are non-standard extension, IMHO “go learn C” attitude towards the OP was uncalled.

The Microsoft compiler does not allow zero-sized arrays in the middle of a struct -

https://msdn.microsoft.com/en-us/library/0scy7z2d.aspx

This struct likely was generated by a tool, as Mr. Roberts pointed out.

So the OP can edit the struct and remove zero-sized fields, or fix the tool to generate code compatible with MS compiler.

Zero-sized array at end is treated by the MS compiler as “open array”. The following two structs are same:

struct s1 { int f1; int a; }
struct s2 { int f1; int a[0]; }

Regards,
– pa