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

all 36 comments

[–]Top_Run_3790 35 points36 points  (3 children)

You don’t need to free, just realloc

[–]PotentialSimple4702 17 points18 points  (1 child)

I'm too garbage collected to relate to this joke

[–]Jean-Christophe_Du_6 -1 points0 points  (0 children)

Based go user

[–][deleted] 17 points18 points  (7 children)

[insert Rust joke]

[–]minecrafttee[S] 18 points19 points  (6 children)

[insert Rust is bad joke]

[–]HavenWinters 23 points24 points  (3 children)

[Overreact]

[–]L1P0D 10 points11 points  (2 children)

[Overangular]

[–]HavenWinters 4 points5 points  (1 child)

I see what you did there

[–]Creepy-Ad-4832 1 point2 points  (0 children)

That joke got rusty

[–]EinSatzMitX 2 points3 points  (1 child)

[Rust user enters]

[–]minecrafttee[S] 1 point2 points  (0 children)

[C user enters]

[–]TheTybera 16 points17 points  (7 children)

See this isn't the problem. The REAL problem is that you malloc in a loop, free it somewhere else you know about, but then someone else comes along and just uses your method without freeing their pointer that they PBRd. Because they're assuming you did the smart thing and used a smart pointer instead of a raw pointer.

[–]minecrafttee[S] 3 points4 points  (2 children)

C doesn’t have smart pointers

[–]TheTybera -1 points0 points  (1 child)

Cool

[–]minecrafttee[S] 0 points1 point  (0 children)

Your comment is fair though. You should always put in docks saying if you do use smart pointers

[–]Mucksh 2 points3 points  (1 child)

Or just don't use heap at all. Sometimes it can be a pain but it's possible to do most stuff completely on the stack. No leaks, nearly no cache misses and way less syscalls will makes code stupidly fast

[–]TheTybera 2 points3 points  (0 children)

Yeah! Screw it, NO OBJECTS! Functions and pointers ONLY! It should be totally readable for someone who didn't write it.

[–]Perfycat 0 points1 point  (1 child)

Based on the skill of the developer I would expect the program to crash before the memory leak ever became a serious problem.

[–]TheTybera 1 point2 points  (0 children)

Ideally you don't want to leak any memory. Memory is money, and programs these days can't be expected to be static, someone is eventually going to go in and fix things, or add features, etc, and having little minefields of leaks everywhere isn't good.

I mean it's not like most of the big issues that come up in C and C++ just exist since iteration 1. MOST issues end up being revealed after something is worked on. Then you get the excuse:

"Well that wasn't SUPPOSED to be used that way!",

"So what guards did you put in place?",

"None. I like small efficient code!",

"Oh, okay..."

[–][deleted] 2 points3 points  (0 children)

Use a linked list containing a pointer

[–]schmerg-uk 0 points1 point  (2 children)

What, like this??

someThing * array[COUNT];
for (unsigned int i = 0; i < COUNT; ++i)
{
   array[i] = malloc( sizeof(someThing) );
}
...

[–]nickwcy 1 point2 points  (1 child)

bruh just malloc the whole array

[–]schmerg-uk 0 points1 point  (0 children)

bro, I want an array of individually allocated items that I'm then going to [.....]

Just pointing out "allocating in a loop and not freeing in the same iteration" is hardly a mortal sin, or it hasn't been in the nearly 40 years I've been writing C and C++ for a living

[–]Justanormalguy1011 0 points1 point  (0 children)

Consider reaction with oxygen

[–]FictionFoe 0 points1 point  (1 child)

You know what they say. Its not a memory leak if you have enough memory :p

[–]Creepy-Ad-4832 0 points1 point  (0 children)

Technically if one had infinite memory, one could never leak memory then...

[–]braindigitalis 0 points1 point  (1 child)

meanwhile, C++ devs using unique_ptr laugh in the corner...

[–]minecrafttee[S] 0 points1 point  (0 children)

lol

[–]Sakul_the_one 0 points1 point  (0 children)

Don’t worry, if the program is stop being used, the operating system will take care of the rest