Is it legal to overrun one element of a struct to view another?

EDIT: As pointed out by others, this is not legal, as it results in undefined behaviour. I've removed this sentence from my answer.

EDIT: As pointed out by others, this is not legal, as it results in undefined behaviour. I've removed this sentence from my answer. This has the potential to result in undefined behaviour.

You've allocated a memory chunk of 10 ints long in the struct abc, so indexing into the 5th (6th) item will take you to y0 as you've noted in THIS specific case. Where you can run into problems is when the C compiler packs the structure in a way that you do not expect. This is called data packing or bit alignment.

When the computer wants to access memory from your data structure, it will attempt to do so in uniform chunks for the entire structure. Let's use an example: struct abc { int a; char b; int c; }; What do you expect the size of this struct to be? An int is 32 bits, and a char is 8 bits, so the total size should be 32 + 8 + 32 = 72 bits.

However, you will find that on many systems, this structure is actually 96 bits in size. The reason is that char be gets bit packed on the end with an additional 24 bits to maintain a standard offset between variables. This can be extremely confusing when you declare a structure in two different places, and one gets bit packed while the other does not due to compile time options or configuration.

Look up bit packing and data alignment or bit alignment for more information.

3 @DavidPfeffer: I'm not sure accepting the only answer that doesn't mention "undefined behavior" was the right thing to do. Surely, this anwser seems to comfort you in your beliefs, but really you shouldn't do it. – ereOn Dec 13 at 14:53 3 It's not legal, in the sense that it gives undefined behaviour.

– Mike Seymour Dec 13 at 14:53 To paraphrase: This is legal in the sense that it gives undefined behavior and may print everything - well in my book that's the complete opposite of legal. – Voo Dec 13 at 15:00 I've edited my answer to reflect the fact that this is indeed illegal in that it results in undefined behaviour. – Sparticus Dec 13 at 15:11 2 The C++11 standard barely uses the word "legal", and doesn't define it.In every case where it does use it (that I can find), the thing it is describing as "illegal" is rejected by the compiler, or the thing it describes as "legal" is accepted and has defined behavior.

So take your pick what you think it should mean, but for preference use standard terminology ("well-formed", "ill-formed", "UB") – Steve Jessop Dec 13 at 15:28.

No, it's not legal nor guaranteed to work. The compiler could add padding into the struct, to aid in alignment, depending on the architecture, etc. Edit: To sum up some of the stuff in these comments and clarify... I do believe you "own" the memory there, since as edA-qa mort-ora-y points out, memcpy() of a struct needs/is expected to work. Where this is specifically guaranteed though, I'm not sure.

That being said, undefined behavior is something to avoid at all costs. What a program with undefined behavior does could change between two separate runs of the same code five seconds apart.It could cause subtle memory corruption in your program, a segfault, or run just fine, but there's no reason to ever use code that relies on undefined behavior.

I.e. There's nothing illegal about writing test. X5 = 10, even if there's no guarantee that the value will appear at test.

Y0? I'm trying to distinguish between legal and a very, very bad idea. – David Pfeffer Dec 13 at 14:42 1 @DavidPfeffer: what people meant here, is that even if you own the memory block, there is no guarantee about what you will read using this trick.

– ereOn Dec 13 at 14:48 1 @DavidPfeffer: It will not necessarily cause a segfault. Anything could happen. This is what undefined behavior means and this is why you should avoid it.

– ereOn Dec 13 at 14:49 3 I no longer think in C++11 this is undefined behaviour. This is a standard layout type and that means copying via memcpy is defined, thus there must be some guarantee about the memory in the padding space. Though I completely agree that value is undefined and has no guarantee about rolling over into the next member.

But it must be safe to access the memory located there due to the standard layout requirements. – edA-qa mort-ora-y Dec 13 at 14:59.

This is undefined behaviour - you are (un)lucky in this case. Further more (aside from the mentioned padding issue), there is the maintainability issue - it's incredibly fragile - what if someone sticks something else in between. I'm sure it's a contrived example, but the recommendation is - don't do it.

Technically the behavior is undefined. While not the best programming practice, this does work. Undefined behavior means anything can happen including what you expect.It might well crash on other implementations.

2 More important (and a more convincing argument) than potential "crashing" is the fact that the compiler may make assumptions about your code when optimizing which you are violating. – Kerrek SB Dec 13 at 14:50.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions