Using zero-sized array inside a struct/union

Hi all!

I have some structures inside an union like this:

union MyUnion {
struct {
int var1;
int var2;
char data[];
} message1;

struct {
int array1[5];
float extra;
char data[];
} message2;

char buffer[MAX_MESSAGE_SIZE];
};

As you can see, data[] member should to have access to remaining data in buffer[MAX_MESSAGE_SIZE].
This code works fine in C but now I am trying to put it inside a new program developed in C++ but I am getting some warning messages:

warning C4200: nonstandard extension used : zero-sized array in struct/union
Cannot generate copy-ctor or copy-assignment operator when UDT contains a zero-sized array

I'm using MSVC to compile it.
It seems to have no effect and all works fine but I don't like to see those warning messages while compiling the project.
What is the best/right solution to get rid those warning messages?

I found some solutions like this one: http://stackoverflow.com/questions/3350852/how-to-correctly-fix-zero-sized-array-in-struct-union-warning-c4200-without

but there are many structures inside the real union and I don't want to polute the code like that solution.
Is There any other solution that should works too?

PS.: I'm sorry if this subject was already discussed here but I wasn't able to find any other thread regarding it.
warning C4200: nonstandard extension used : zero-sized array in struct/union
Cannot generate copy-ctor or copy-assignment operator when UDT contains a zero-sized array


I haven't tried an example of this myself (Inside the union, maybe I'll get some time later today), however that said, can you write your own copy and assignment ctor for the struct as that is what it is complaining about?
Hi clanmjc! Thanks for your reply!

Sorry, I forgot to say one thing: the code should to be compiled for both C and C++ languages.
It is a ModBUS protocol definition and I want to use the same file for both situations: firmware of my PLC (It is using an ARM Cortex-M3 processor ) and my PC.
For ARM I have only a C compiler, not C++.
For PC I am programming for windows using Visual C++.
The code is running fine for both now but I don't like to get compilation warnings. That is why I am looking for a solution. :-)

Thank you again!

Best regards,
Marcelo Utikawa da Fonseca
I don't know if this is portable but couldn't you do something like....



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
union MyUnion {
struct {
int var1;
int var2;
char data[MAX_MESSAGE_SIZE - (sizeof(int) + sizeof(int))];
} message1;

struct {
int array1[5];
float extra;
char data[MAX_MESSAGE_SIZE - (sizeof(int) * 5 + sizeof(float))];
} message2;

char buffer[MAX_MESSAGE_SIZE];
};


Or some variation of it (might need to do something for alignment).
That's a very good idea! :-)

I will create a macro to calculate remaining size. I think that it will works as expected!

I will try to implement your solution and give the results here.

thanks a lot!

Best regards,
Marcelo Utikawa da Fonseca
Yeah I'd like to see the macro, let us know how it goes.
Since you're flirting with undefined behavior anyway you could just define the arrays as
char data[1]. Nothing would stop you from going beyond the single element declared.
Last edited on
If you do that, you'll have to byte align the structure.
Topic archived. No new replies allowed.