r/ProgrammerHumor 2d ago

Meme bugIntroducedDebugging

Post image
1.1k Upvotes

120 comments sorted by

View all comments

1

u/LostgamerFJ 2d ago

I'm not good enough at programming for this. What do the "free" and "malloc" functions do?

27

u/LucyShortForLucas 2d ago

They are core C functions. malloc allocates N bytes of memory, free frees that memory. * is t he dereference operator, so *x is trying to dereference freed memory, which is undefined behavior

3

u/DiodeInc 2d ago

I thought * was a pointer. Isn't it? How is it dereferencing freed memory?

Or is it because it's returning *x that's the bug?

8

u/Vimda 2d ago

* in a variable declaration incidates a pointer type, * in a statement is the dereference operator

2

u/DiodeInc 2d ago

I see. Thanks

3

u/ThomasScotford 2d ago

* is the dereference operator, but in the context of variable declaration, it specifies the variable is a pointer to a type.

-Thomas Scotford

1

u/ArjixGamer 2d ago

Why are you leaving a name signature in your comment, when your username is the same as that name?

2

u/LostgamerFJ 2d ago

That explains a lot. I mainly code in Java and python, as I'm a beginner and have never touched c

2

u/conundorum 2d ago

That makes sense. Java in particular does a lot of this under the hood; anything that isn't a primitive is actually a reference (special C++-style pointer with non-pointer syntax), and new is a combination of malloc() & objection creation. (delete should be a combination of object destruction & free(), but they had to change how it works to accomodate the garbage collector.)

C (and C++) just makes you do the actual bookkeeping yourself.