8 ms·
I do memory twiddling a lot in C. The answer is what happens in practice depends on the machine alignment. On an 8 bit machine generally alignment is 1 byte and
by Gibbon1 7y ago
I do memory twiddling a lot in C. The answer is what happens in practice depends on the machine alignment. On an 8 bit machine generally alignment is 1 byte and there is no padding. On a 32 bit machine yeah you'll have 3 bytes of padding for each element.
Me I think alignment is increasingly a bad idea. Most modern processors don't deal with memory in word sized chunks anymore. Padding just increases cache pressure.
Either way the example is trashfire grade C.
- tumult 7y agoThat is not how padding and alignment works in C. The example above has no padding between each field, because the implementation specifies how much padding there should be. For clang, gcc, and msvc, on x86 and AMD64, there is no padding between chars in a struct. if the struct were instead { char a; int b; } then there would be 3 bytes of padding between a and b. The example is not trashfire and is in fact totally valid. Common ARM processors will fault if you try to perform an unaligned access. You must align where required. Edit: correction to mistake above: I originally wrote { int a; char b; } and should have said the padding came after b. I've fixed it to match the explanation text and have the padding between. I should really be writing these posts in a separate text editor.
- Const-me 7y ago> if the struct were instead { int a; char b; } then there would be 3 bytes of padding between a and b. I doubt it. To get 3 bytes of padding between fields, you need this: { char a; int b; }
- tumult 7y agoYes! Sorry, my mistake. int then char on x86 will put the padding at the end of the struct, not in the middle. I had originally written it that other way (char and then int), then edited it to be the other way (int and then char) but didn't update the other text. I edited the post to have this correction.
- bluGill 7y agoFor x86 and XMD64. What about arm, Mips, Sparc, alpha, whatever what in the IBM mainframes... All of the above have C compilers and each does something different. I don't know the rules for each, but I know some of them have really weird rules in specific cases.
- tumult 7y agoWhat? Why don't you look it up? Why are you asking here, when you can find each of these with a few seconds of searching or downloading manuals?
- bluGill 7y agoYou misunderstand. In context of the grandparent: all the world is NOT x86/AMD64. there are many other processes with many different rules. That his code works on the above two doesn't mean it will on the others.
- tumult 7y agoI took care to mention that x86 is not the only platform, and to what extent they're relevant. I also made sure to not specifically answer as if this is only for x86. I don't know why you're willingly misinterpreting what I said. It's quite frustrating, especially after I took care to not answer as if x86 were the only platform. I asked you why you're asking about specifics here in a querying conversational, when you can just search the internet and be given the specific answers immediately. Because you're asking me to reply again, I'll go ahead and answer your question for you: all of those platforms are the same as far as padding/alignment for char is concerned. But just to reiterate, I was careful to not require this kind of knowledge for my replies to make sense.
- bluGill 7y agoI don't work with them, but their are weird systems with non 8 but bytes. I don't even know what they are, but every time this comes up in committee those compiler writers speak up
- barrkel 7y agoFields are generally aligned so that they are at an offset which is an integer multiple of the field type's alignment. Type alignment for built-in types is generally the highest power of two which is both (less than or equal to the type's size) and (less than or equal to the maximum alignment for the target ABI). Type alignment for compound types (like struct) is generally the maximum of the constituent types' alignments. But details may vary from implementation to implementation and platform to platform.
- DagAgren 7y ago> Most modern processors don't deal with memory in word sized chunks anymore ARM does. That is a very modern processor in very wide use. Linux hides this from you by trapping illegal memory accesses and handling them in software. Instead of a crash, you get abysmal performance if you do not align correctly.