The absolute state of C++

#include

struct Foo {
char a;
float b;
char c;
};

struct Bar {
float b;
char a;
char c;
};

int main()
{
std::cout

Attached: 1300044776986.jpg (600x600, 34K)

Other urls found in this thread:

catb.org/esr/structure-packing/
youtube.com/watch?v=AKtHxKJRwp4
twitter.com/SFWRedditVideos

>what is struct padding

Is this not common knowledge? Anyways GCC has an attribute to reorder elements for the smallest size: __attribute__((packed)) afaik

This is to show what exactly? Nothing.

Watch OP being the little skanky bitch that he is runs away and closes his browser window so he doesn't have to get the ass fucking he's about to receive from the people about to respond to this thread.

>best solution is to use a compiler-specific extension

>This is to show what exactly?
It's to show that the C++ standard can't into compile time optimizations.

You shouldn't be packing structures unless you have a good reason to do so.

catb.org/esr/structure-packing/

>best solution
I didn't imply that. The solution is to rearrange them to have biggest-->lowest

>you shouldn't be using less memory with zero downsides unless you have a good reason to
u wot m8?

>not using [repr(packed)]

>The solution is to rearrange them to have biggest-->lowest
Which is absolutely fucking retarded. The C++ standard should specify that the compiler needs to do that. There is zero reason for this not to be done automatically. Stop trying to justify the faults of a shitty programming language.

Sometimes you really want a different ordering.

C/C++ assumes the programmer is smarter than the language and not the other way around, now go back to webdev or python faggot

On most computer architectures it's slower to access memory if it's not bounded to a word. On some computer architectures it's mandatory

If your program relies on the order of the member variables in memory, you are doing something horribly wrong and should not ever code anything.

>this guy has no idea what padding is and why it was invented

>moving goalposts
False, which is why one of the greatest C++ lectures ever is about how programmers often shoot themselves in the foot by trying to help the compiler: youtube.com/watch?v=AKtHxKJRwp4

No, spouting absolute utter bullcrap like that and pretending you know better means you are a dangerous idiot. You should fuck off and never ever code anything

>webdev baby

Attached: e10623c0c699037bcf73425efd779a77.jpg (450x450, 17K)

I know exactly what it is. However, there is literally zero justification for the compiler not optimizing the order of the member variables. Are you really this stupid that you think it's a good thing that the programmer has to do this work manually even though the compiler can easily figure out the most memory efficient order deterministicly?

Based

I notice you haven't given any examples of code which relies on the order of member variables and isn't total garbage. The reason why is because such a thing does not exist. If you think it does, you really have no business on this board.

>sometimes you want your program to consume more memory without any practical advantage
my sides are in orbit

If you really need the order to be something specific, you should be using tuples. Even then, you're using them wrong.

>literally makes ad hominem attack due to an inability to properly defend his position.
Cool story.

there are common cases where you want to have explicit order, wonder if it plays some role in (de)serialization implementations repying on the same layout across machines and how (if it exists) rust deals with it
any paper exploring a different alignment effects on performance?

That only relies on the order being consistent, but not on them being in any specific order. All that has to happen is the spec needs to specify what order the compiler needs to put them in, in a way which is not open to interpretation.

Still more efficient than you shitlang pajeet

it's crucial for cache optimization as well, both automatic and manual.

>Is this not common knowledge?
To anyone who studied electronics or compiler design, it is. To stupid Java "developers," it isn't.

the world would probably be a better place if the compiler automatically optimized the order, yes. however, now that we have lots of C-like APIs where you pass around void pointers and where the API relies on the order of the struct data, we can't have that optimization or it will break legacy code.

C and C++ compilers are not able to do this by standard, which mandates that all struct fields be in increasing address order. If you think this is retarded, then you have never parsed a binary file or network packet in your life.

Rather than bitching that the compiler doesn't perform optimizations that can result in incorrect behavior, you should write your structs the right way the first time around so that they are laid out in memory as you desire it. This is a systems language. You are expected to think about how data fits in memory when you write software.

This happens because struct alignment. To avoid this, use pragma pack(push, 1)