Using a class instead of array

There are a million of lines of code out there that say “sizeof(array) = sizeof(elem) * length”
Yeah, but that’s for arrays and we are interested in structures.

Also, structures are guaranteed to have idenical allocation on first compatible elements when you put them in union, but I haven’t found anything in the C/C++ specs that would say that struct elements will overlap array elements.

Yeah, sure it will work but not because C/C++ specs say it should but because compilers happen to overlap structures and arrays this way.

And that’s the whole point of this discussion (besides initial question which has been answered).

So, if you found anything in the specs that guarantees expected overlapping of array and structure in the union then let us know. This would deal with our doubts. :slight_smile:

“Yeah, but that’s for arrays and we are interested in structures.”
I think I understand the point of the discussion. And the C++ spec is frankly irrelevant, since you don’t use a spec to compile your program…

The question is whether there are actual compilers out there that don’t do the right/expected thing in this case. And I submit there aren’t, at least not by their default behavior for the structures that were asked about. As long as we’re talking about 4 floats defined as a simple struct or array, the layout will be tightly packed and 4-byte aligned by default.

It only gets funky when you start mixing elements that are smaller than 4 bytes with elements that need to be 4 or 8-byte aligned for ALUs or similar. That’s when you might want the packing directives, which, btw, can really cause problems. You don’t want to wind up with floats spanning those 4-byte boundaries, even if you’re trying to tightly pack. You’re much better off manually laying out your class to ensure floats and doubles are properly aligned in that case.

In general, it’s best to stick to the default packing, which, as I said, works for unions of structs and arrays of float in every compiler I’ve ever tried.

And the C++ spec is frankly irrelevant, since you don’t use a spec to compile your program
Thats exactly whad I had in mind when I wrote:

Another thing is that people rely on structure member order pretty much and I guess no one wants to make a compiler that will not be used by anyone. So this relation between member order in structure and real order in memory will be somewhat enforced by programmers upon compilers.
And later:

Ah, to hell with this. I’m gonna use structures and unions and don’t care. It’s gonna work anyway
So the question if it’s gonna work or not has been more less answered. The only question remaining is if there is a guarantee this should work. Note that I used word “should” - compilers can sometimes break the specs.

I think my final conclusoin (for now) is that it will work but it’s not enforced by specs, so we just have no guarantee.

So forget the specs and welcome to the real world (hehe, real = float). Cheers and thanks to everyone who contributed to this discussion.