Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

c

standards

Given the following contrived example code:

struct abc
{
    int x[5];
    int y[5];
};

void main()
{
    struct abc test;
    test.y[0] = 10;
    printf("%n", test.x[5]);
}

The output of the program is 10.

While not the best programming practice, this does work. However, is this an artifact of the compiler and platform, or is this legal code? (i.e. defined by the C standard?)

Even if the result is not guaranteed to be 10, is there ever an instance where this would be "illegal" (i.e. writing to memory I do not "own")?

like image 267
David Pfeffer Avatar asked Dec 13 '11 14:12

David Pfeffer


People also ask

Is your project headed for an overrun?

While many of a project’s stakeholders are eager to get the project’s building started, if you have faulty schedules and budgets to begin with, your project is headed for an overrun from day one. Due to the competitive nature of the bidding process, estimates may suffer from wrongful expectations of the scope of work included in the project.

How do you address cost overruns in construction projects?

To know how to address cost overruns in construction projects, it’s important to understand the root of the problem. Oftentimes, overruns hint at deeper issues in project management and from schedules and budgets being set improperly from the very beginning of the project.

Is struct optional in C++?

Note: In C++, the struct keyword is optional before in declaration of a variable. In C, it is mandatory. How to initialize structure members? Structure members cannot be initialized with declaration. For example the following C program fails in compilation. But is considered correct in C++11 and above.

What are the main causes of construction overruns?

Outside of all of that, overruns are still likely to occur if the team executing the work is not up to a certain level of standards. Poor and less experienced subcontractors can cause costly mistakes, delays and errors, even with the most impeccable designs and plans.


2 Answers

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.

like image 60
Dan Fego Avatar answered Oct 06 '22 17:10

Dan Fego


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.

like image 27
Nim Avatar answered Oct 06 '22 17:10

Nim