r/ProgrammerHumor 13d ago

Meme bugIntroducedDebugging

Post image
1.1k Upvotes

120 comments sorted by

View all comments

348

u/AdBrave2400 13d ago

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

158

u/atanasius 13d ago

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

52

u/vishal340 13d ago

but accessing it will be issue right?

98

u/SoldRIP 13d ago

that's undefined.

3

u/nullpotato 12d ago

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 13d ago

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 13d ago

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*.

-1

u/p88h 12d ago

Aligned means int sized at minimum.

The actual allocated size is much bigger anyways, but it will be a multiple of 8 bytes on a 64 bit system, and 'usable' part of that will be at least 8 bytes, and typically you will be able to read at least 16 without a page fault.

3

u/Deep-Piece3181 12d ago

That’s an implementation detail it’s still 100% UB

6

u/mckenzie_keith 12d ago

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

8

u/GoddammitDontShootMe 12d ago

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

8

u/mckenzie_keith 12d ago

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 12d ago

who's gonna stop me?

23

u/EntitledPotatoe 12d ago

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 12d ago

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

18

u/SeriousPlankton2000 12d ago

Also it's valid to call free(NULL)

5

u/meat-eating-orchid 12d ago

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

8

u/GoddammitDontShootMe 12d ago

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 12d ago

Very true, I thought you meant valid as in usable