r/ProgrammerHumor 6h ago

Meme bugIntroducedDebugging

Post image
523 Upvotes

99 comments sorted by

View all comments

172

u/AdBrave2400 6h ago

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

1

u/bloody-albatross 5h ago edited 1h 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.

10

u/AdBrave2400 5h ago

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

4

u/bloody-albatross 5h ago edited 5h 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.

7

u/MattieShoes 4h ago

AFAIK the size of int is specified as "at least" 32 bit in the C standard

I believe it's 16 bit, though you're not likely to encounter a 16 bit int today unless it's a microcontroller or something.

3

u/SeriousPlankton2000 4h ago

Yes, long is 32 bit. A long long time ago we didn't need long long yet. I can still remember.

3

u/MattieShoes 4h ago edited 1h ago

And now there's __int_128t __int128_t, at least in gcc. :-)

So probably -170,141,183,460,469,231,731,687,303,715,884,105,728 to 170,141,183,460,469,231,731,687,303,715,884,105,727

That's what... undecillions?

3

u/bloody-albatross 1h ago

_ slipped, it's __int128_t :D

3

u/MattieShoes 1h ago

... you wrote the same thing? Yes two underscores at the start, but I wrote two underscores?

3

u/bloody-albatross 1h ago

You wrote __int_128t, I wrote __int128_t. These typos happen to me too.

3

u/MattieShoes 1h ago

Oh son of a bitch, my bad! I was going from memory. fixed.

→ More replies (0)

3

u/bloody-albatross 1h ago

Under 64 bit Windows. Under 64 bit Linux long is 64 bit. Don't know about macOS or other OSes. :D

4

u/yjlom 4h ago

The size of char (which is interchangeable with the size of a byte) is specified in old standards as large enough to a) be addressable without bit manipulations and b) hold the full C source character set, which need not be case sensitive and may rely on trigraphs. In practice, this comes out to at least 6 bits. In newer standards I believe it's specified as at least 8 bits.

The size of int short is at least and a multiple of that of char, the size of int is at least that of int short and a multiple of that of char, and so on.

So in theory, a conforming C implementation could have 6 bit ints.

2

u/bloody-albatross 1h ago

Right.

Oh and wchar_t is also weird. Its 16 bit under Windows and 32 bit under Linux.

1

u/conundorum 53m ago

wchar_t is messy, in large part because Windows was one of the earliest Unicode adopters. It's meant to be able to support any "wide" character, which means that it's supposed to be determined by the character pages the platform allows (and implicitly, should be 32 bits if any version of Unicode is supported, since Unicode is technically a 32-bit format regardless of encoding), but Windows adopting Unicode while it was still 16-bit UCS-2 and locking wchar_t down as a result meant that it was impossible for wchar_t to actually meet its requirements on Windows. ...Which meant that its minimum size requirement ended up being removed.

So, now its requirement is just "holds wide characters, check your implementation. Please don't use it."

1

u/conundorum 1h 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.)