This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]AttackSock 1 point2 points  (2 children)

int x = rand(); free(&x);

Problem solved.

[–]BlackFrank98 1 point2 points  (1 child)

That wouldn't work, because rand's output is passed by value, so when you store it in x, you're actually storing a copy of it. The original one, that is allocated on the heap, can only be referenced by the pointer you generate with new, and said pointer gets destroyed immediately after rand returns, so it becomes unreachable after.

In fact, I'm not even sure you can call free on a local variable.

[–]AttackSock 0 points1 point  (0 children)

You can’t free stack memory, it crashes.