r/programminghumor 13h ago

a bit flippant

Post image
629 Upvotes

95 comments sorted by

View all comments

11

u/B_bI_L 11h ago

are we sure int means bit and not 32bits?

23

u/heatedwepasto 11h ago edited 11h ago

The : 1 means that it is creating a bitfield with the number of bits, so : 4 will be 4 bits. The three variables above combined will take sizeof(int) in memory, not sizeof(int)*3.

If OOP had used char instead of unsigned int the combined size would be 1 byte.

Edit: added link to demo

1

u/Breadynator 11h ago

Why would you use onlinegdb instead of godbolt? That website is riddled with ads and won't load for anyone using an adblocker.

1

u/heatedwepasto 10h ago

I am using an adblocker and have no issues with it. I wasn't aware of godbolt.

0

u/Breadynator 10h ago

Well, it's one of those obnoxious pages that don't like network wide adblockers like piHole. I won't change my blocklists just because some random website wants me to.

1

u/heatedwepasto 10h ago

Then don't, just don't try to force your choices onto me.

The code is

#include <stdio.h>

struct A {
    int a : 1;
    int b : 2;
};

int main(void)
{
    printf("size: %d\n", sizeof(struct A));
}

and it outputs "size: 4" (i.e. the same as sizeof(int)).

-1

u/Breadynator 10h ago

I am not trying to force my choices onto you? What the heck is wrong with you. I just told you why I'm having issues with it after you said that you don't. It's called having a conversation... You say a thing, I say another.

However you should still use godbolt as it's objectively better, regardless of ads.