r/ProgrammerHumor 2d ago

Meme bugIntroducedDebugging

Post image
1.1k Upvotes

120 comments sorted by

View all comments

339

u/AdBrave2400 2d ago

Is the mistake that they're allocating 1 byte and storing in a pointer to an int which is 4 bytes?

2

u/bloody-albatross 2d ago edited 2d ago

You're still on 32 bit? Are you doing a lot of embedded stuff?

Edit: I misread and thought they where referring to the pointer size.

13

u/AdBrave2400 2d ago

Isn't int 32-bit on most platforms and long long int is 64-bit?

3

u/bloody-albatross 2d ago edited 2d ago

I read it as the size of the pointer being 4 bytes, but now I see you were referring to the int. AFAIK the size of int is specified as "at least" 32 bit in the C standard, and all 32 and 64 bit platforms I know indeed use 32 bit. Though the size of long (not speaking of long long) is different on different 64 bit platforms, IIRC. Windows uses 32 bit and Linux uses 64. Both use 64 for long long. I prefer to use (u)int*_t when I need to be sure about the size and I use int/size_t/time_t/... when I interface with functions that use those in their signature.

1

u/conundorum 2d ago

Assuming 8-bit bytes, sizeof(int) is guaranteed to be a minimum of two, and intended to be the system's word size (which should technically be 8 for 64-bit platforms, but we're so used to 32-bit int that most platforms intentionally stagnate int at 32 bits & most processors have two native word sizes to accomodate), but can be anything higher. ILP64 models have 64-bit int, and there was at least one platform where all bytes were 64-bit and sizeof(char) == sizeof(long long) == 1, though, so it can get weird sometimes.

(Also, as a note, long long is required to be at least 64 bits. long is required to be at least 32 bits, and is meant to just be the 32-bit data type, but ends up being the design limitation fulcrum for most platforms; Windows is locked into 32-bit long because it needs to support 32-bit executables, and Linux is locked into 64-bit long because it needs to support punning pointers to long.)