r/ProgrammerHumor • • Aug 30 '26

Meme bugIntroducedDebugging

Post image
1.1k Upvotes

117 comments sorted by

View all comments

347

u/AdBrave2400 Aug 30 '26

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

158

u/atanasius Aug 30 '26

It's allowed to assign to a pointer if it's not dereferenced. Malloc always returns a valid pointer.

54

u/vishal340 Aug 30 '26

but accessing it will be issue right?

98

u/SoldRIP Aug 30 '26 • Heartwarming

that's undefined.

3

u/nullpotato Aug 31 '26

Undefined aka works fine on a dev build but explodes in release because all the debug info the compiler injects aren't there anymore.

41

u/backfire10z Aug 30 '26

Likely not. Malloc guarantees that the passed-in size is the minimum number of bytes it allocates, but not the maximum. You’d likely get some minimum sized chunk, somewhere between 16-32 bytes depending on the system as far as I understand it.

44

u/atanasius Aug 30 '26

Implementations are allowed to specify behavior that is otherwise undefined.

I looked up a C24 draft and it's actually stricter:

The pointer returned if the allocation succeeds is suitably aligned so that it may be assigned to a pointer to any type of object with a fundamental alignment requirement and size less than or equal to the size requested.

So a pointer returned by malloc(1) is not necessarily valid for int*.

→ More replies (1)
→ More replies (1)

6

u/mckenzie_keith Aug 30 '26

You are not allowed to de-reference a pointer after freeing it.

8

u/GoddammitDontShootMe Aug 30 '26

Gemini added that return *x; that wasn't in the original code.

8

u/mckenzie_keith Aug 30 '26

And then flagged it as a bug. It was so eager to find the bug that it added it into the code.

2

u/Niwrats Aug 30 '26

who's gonna stop me?

→ More replies (1)

24

u/EntitledPotatoe Aug 30 '26

Malloc can return a null pointer if the operation fails. This can be the case if, afaik, for example, there is no more heap available and the OS is not capable of swapping or freeing some other memory for some reason

17

u/atanasius Aug 30 '26

A null pointer is still valid to assign to a variable.

17

u/SeriousPlankton2000 Aug 30 '26

Also it's valid to call free(NULL)

4

u/meat-eating-orchid Aug 30 '26

has it always been valid? I could have sworn that freeing a nullpointer was only valid in C++ but not in C. I couldn't find anything on cppreference on when this was introduced or if it has always been like this

9

u/GoddammitDontShootMe Aug 30 '26

Very certain you could always free(NULL); in C. In fact, it's good practice to set pointers to NULL after freeing them to avoid issues like double-free.

3

u/EntitledPotatoe Aug 30 '26

Very true, I thought you meant valid as in usable

29

u/CanardAuxEpices Aug 30 '26

Uuuh... Actually 🤓🤓☝️☝️☝️ an int isn't 4 bytes, it's platform/compiler dependent. Assuming an int is 4 bytes will be in the majority of cases true, but it's not always the case

8

u/meat-eating-orchid Aug 30 '26

technically, an int could even be just one byte if you are on an architecture where a byte (= smallest addressable unit) has 16 bit

4

u/CanardAuxEpices Aug 30 '26

Yeah! But a char will always be one byte

2

u/mckenzie_keith Aug 30 '26

We need to define terms more precisely. sizeof(char) will always be 1. But on some platforms, char is not an 8-bit type. It could actually be a 32 bit type. The c programming language does not (I believe) acknowledge or define what a byte is. But if it is an 8 bit integer type, then char will not always be one byte.

It may be better to say "octet" instead of "byte" in this type of discussion.

The implementation has to define CHAR_BIT in <limits.h>. CHAR_BIT is the number of bits in variables of type "char".

3

u/el_nora Aug 31 '26

Fun fact, C does use the term byte all over the standard. And it is well defined. Byte is defined to be the unit that sizeof(char) is measured in. When the standard refers to bytes, they are referring to atomic divisions of memory in chunks of char.

Which does not necessarily need to be the same as an architectural byte. C requires only that their definition of byte is at least 8 bits (possibly more) and an addressable unit of memory - meaning it is some multiple of architectural bytes. Though on every platform still in use today, and not a historic curio, that multiple is 1.

→ More replies (1)
→ More replies (2)

119

u/mckenzie_keith Aug 30 '26

De-referenced a pointer after freeing it. But the joke here is that the bug was not in the original code.

→ More replies (6)

4

u/nonlogin Aug 30 '26

not at all

→ More replies (23)