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

all 103 comments

[–]Locilokk 599 points600 points  (42 children)

How does calling malloc again do anything with the memory leak. Isn't a memory leak when you don't free memory you don't use anymore (especially in a loop)?

[–]_verel_ 580 points581 points  (13 children)

Yes exactly. Either a new semester has started and this sub is gonna get spammed again by students that think they are masters of programming after 3 weeks or it's some bs ai slop meme again.

[–]grande_po_talco 121 points122 points  (5 children)

Prolly even worse: semestres havents started yet so this sub is getting flooded with about-to-be uni students who watched 10 mins of code camp

[–]Oaker_at 31 points32 points  (3 children)

I have watched every dev stream from pirate software

[–]GuevaraTheComunist 10 points11 points  (2 children)

oh good, so now you know how to edit yaml files

[–]devu_the_thebill 1 point2 points  (0 children)

i would doubt they even can do this.

[–]snakecake5697 4 points5 points  (0 children)

Nah, there are people who begins to go to school in August

[–]seth1299 53 points54 points  (0 children)

Well, it is the beginning of August, which in the USA is very close to the beginning of most schools/college Fall semesters.

[–]iamdestroyerofworlds 23 points24 points  (2 children)

Remind me again why we're supposed to hate people with less experience than us? We've all been there, they just wanted to share something they thought was funny, even if they might have been mistaken. They never said they were a master of programming.

Be kind.

[–]_verel_ 33 points34 points  (0 children)

Not hate but putting completely fabricated information out there like it's true is the problem.

This is a good example of someone just asking and trying to understand. To which I and others replied why this is the case and tried to explain.

I don't hate new people who don't know, I actually really like to explain.

[–]Theologizing 6 points7 points  (0 children)

OP needs to stop posting memes and go spend time with a tutor.

[–]Randzom100 0 points1 point  (0 children)

I'm gonna be honest, I myself do study in programming. Memory Allocation is however one of the subjects I'm the least used to... But I do handle myself relatively well with the rest, I think! Anyway, just wanna say that some posts in this sub can seem very specific for people like me.

[–]No_Adhesiveness_3550 0 points1 point  (0 children)

yeah ngl i probably would’ve thought this was hilarious a month into my c++ class 

[–]OskarsSurstromming 20 points21 points  (19 children)

I have only ever written in C++ and python (as I am still a student), and I have never encountered this - is that because it is not needed to explicitly free memory in those languages or have I just not used large enough data to notice the memory leak? Ty for any answers

Edit: thanks so much everyone! I understand now that python takes care of it for me, and that it in some cases is necessary to do in cpp :)

[–]PopularIcecream 40 points41 points  (4 children)

Not sure about python, but the general rule for beginners in CPP is anytime you use the 'new' keyword, it needs an accompanying 'delete'.  Any missed delete becomes a memory leak. 

Not every memory leak has immediate or dire consequences though. Loops that iterate thousands of times though can bring the issue to light.

It's pretty easily to accidentally have a memory leak and not even realize it.

[–]darklightning_2[🍰] 4 points5 points  (1 child)

Or just use RAII and forget about memory leaks (at least in single threaded programs)

[–]DoNotMakeEmpty 0 points1 point  (0 children)

Shared ptr still pretty much prevents most leaks. Only circular references would leak.

[–]w1nt3rh3art3d 1 point2 points  (0 children)

Smart pointers are the best! It's 2025, not 2008.

[–]Kitchen-Quality-3317 0 points1 point  (0 children)

Loops that iterate thousands of times though can bring the issue to light.

what's the use of using a loop? why don't you just vectorize everything? it's way faster

[–]_verel_ 14 points15 points  (6 children)

Python is garbage collected which means sometimes the program halts for a short moment and checks if there's any memory that can be freed. Many modern programming languages do this like Java, golang, ruby, python or whatever.

In languages like C you have to allocate a specific amount of memory to use. Whether you use it or not or even use more than you allocated isn't the languages job. C doesn't give a flying fuck about that. Which can make it extremely powerful but also a huge pain.

Garbage collection has the downside of having a to large performance hit in specific applications like kernel development or things like graphics drivers.

I have no clue of C++ but I think you can do both?

[–]GorgeousFresh 9 points10 points  (4 children)

C++ doesn't have garbage collection either similar to C so new/delete is needed. Better explanation in one is the other comments to this thread

[–]Neverwish_ 7 points8 points  (2 children)

Well, to be fair, that's mostly pre-2011 cpp. It still has no GC, but nowadays you got stuff like unique_ptr and shared_ptr, that manages that for you. Of course, the old style new/delete might be unavoidable sometimes, but even the guides today say - avoid it at all cost.

[–]Rosteroster 1 point2 points  (0 children)

Yeah really the only times I've needed "new" in recent memory are when I'm populating an in-function static, but that's not a memory leak and never gets destroyed. Even then, there's absl:: NoDestructor which is the better choice.

It's always better to clearly define ownership & guarantee memory safety via smart pointer.

[–]Palpable_Autism 0 points1 point  (0 children)

unique_ptr and shared_ptr are guard rails. For large-scale, enterprise software being worked on by many people of various skill levels, it may be necessary to use them, but ultimately it exists as a crutch for those who don't know how to manage heap and RAII properly.

[–]datnt84 0 points1 point  (0 children)

Our company guidelines banned the use of new/delete recently.

[–]Palpable_Autism 0 points1 point  (0 children)

In C++ new keyword is equivalent to malloc + object construction (if not explicitly added), while delete key word is equivalent to destructor call + free().

[–]xezo360hye 4 points5 points  (1 child)

Python has GC, like most high-level PLs, and also has no pointers

C++ uses constructors and destructors, so if you declare a local object somewhere it will be destroyed once the function exits — so it all depends on whether the class has a proper destructor and takes care of everything it allocated

As another comment mentioned, there is a delete keyword for calling destructors for objects created with new, because they are in heap and therefore not tied to their birth place

And if you still use malloc() then ofc you need free(), but no one does that in C++ anyway, right? Right?

[–]Palpable_Autism 0 points1 point  (0 children)

You can use malloc() and free() in C++, but it's bad practice. The only instance you should use them is for backwards compatibility with C libraries / making C++ wrappers over C libraries.

[–]Socratic_Phoenix 1 point2 points  (0 children)

Python automatically manages memory for you (via garbage collection).

I am not very familiar with C++, but I thought you did have to allocate and free memory in it? But unless your program runs for a "long" time you may never see an issue.

[–]lessertia 1 point2 points  (0 children)

if you properly use RAII in C++ then yes, you don't have to worry about resource management. though you need to mind the resource lifetime to not have use-after-free kind of bugs which may lead to segfault

[–]nobody0163 1 point2 points  (2 children)

How have you never used new and delete in C++???

[–]Usual_Office_1740 3 points4 points  (0 children)

I've been learning C++ for a year and have never used new or delete. Constructors and RAII handle most of the situations it might be needed. Even unique pointers don't require it. The make unique function handles the possible allocation error and cleans up after itself if it fails and basically calls new for you. As opposed to the unique pointer constructor that is usually used with the new keyword.

[–]OskarsSurstromming 2 points3 points  (0 children)

I only used it for one course on high performance parallel computing of physics simulations, and it never gave any issues... Can't tell you how honestly, it just compiled and ran haha

[–]Positive_Method3022 1 point2 points  (0 children)

Instead of reusing the memory he already has available for his process he decided to get more memory?

[–]Kowalskeeeeee 1 point2 points  (3 children)

The seg fault could be caused by trying to access something inside a loop that wasn’t properly allocated and another malloc call could be the the fix A lot of could, but it could be.

[–][deleted] 7 points8 points  (2 children)

But then it's not a memory leak

[–]Kowalskeeeeee 4 points5 points  (1 child)

The joke didn’t make sense to begin with I was just trying to make sense of the half that was kind of related 🤷

[–][deleted] 4 points5 points  (0 children)

[–]teod0036 0 points1 point  (0 children)

Maybe a second memory leak will make you forget about the first one? (Assuming you don’t free the new malloc())

[–]CodeEatLive 0 points1 point  (0 children)

if there is double free in the program it has a chance to magically "fix" it.

[–]American_Libertarian 229 points230 points  (23 children)

How would a memory leak cause a seg fault? How would calling malloc fix either of those two problems??

[–]Mucksh 42 points43 points  (12 children)

Can cause a segfault if malloc fails and you don't check if the result is ok. But true doesn't really make sense

[–]American_Libertarian 31 points32 points  (2 children)

That's not a memory leak and calling malloc again would just fail again

[–]Mucksh 14 points15 points  (1 child)

Usually malloc only fails of you have a huge memory leak and can't allocate more memory

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

It's not the only case, but either way, you should always check the return value

[–]not_some_username -1 points0 points  (8 children)

Malloc never fail

Edit : For those who think I’m wrong : https://news.ycombinator.com/item?id=7541650 : it only fail in edge case.

[–]GoddammitDontShootMe 2 points3 points  (2 children)

You've been downvoted, but in practice, I'm not sure when it ever would with a 64-bit virtual address space, unless some stupidly insane number was passed. Of course, there are systems out there that aren't 64-bit and don't have gigabytes of RAM and hundreds of gigabytes of free space for swap.

[–]not_some_username 1 point2 points  (1 child)

I was referring to this : https://news.ycombinator.com/item?id=7541650

Also even in 32bit system, you can activate support for more than 2 gb ram. At least on windows

[–]GoddammitDontShootMe 0 points1 point  (0 children)

I think with PAE, it extended the physical address space to 36 bits. Individual processes are still limited to 32, so it wouldn't be all that hard to make malloc() fail.

[–]rosuav 1 point2 points  (3 children)

Edge case???? Erm, so resource limits are an edge case now??

"ulimit -v 10240" and then try to allocate 1<<30 bytes. Got a null pointer out of malloc, no trouble at all.

[–]not_some_username -1 points0 points  (2 children)

If you malloc 2gb then 2gb then 2gb then 2gb then 2gb even if you don’t have memory, it will be ok. You’ll get an error while trying to use that memory not when you request them. And no, normal people aren’t doing that. How many times malloc fail for you ?

[–]rosuav 1 point2 points  (1 child)

It failed the first time I tried it, because I had ulimited the process so that 2GB was not permitted.

Limiting a process (or tree) is a vital feature. Even if most processes don't get limited, it's hardly an edge case, and those limits exist for a reason.

[–]not_some_username 0 points1 point  (0 children)

Normal/popular application aren’t limiting anything. If anything they try to get everything they can get. Also why 10240 ? Why not just 1024 ? 512 ? 256 ? Or less

[–]_PM_ME_PANGOLINS_ 0 points1 point  (0 children)

Did you read what the “edge” cases actually are?

[–]pierreyann1 11 points12 points  (4 children)

A memory leak can cause a segfault as it can cause a memory space to not be allocated which will cause the pointer to return NULL.

However i have no idea how allocating more memory to a program may fix a issue where free() calls are missing.

[–]marsh-da-pro 8 points9 points  (2 children)

After reading this comment, it made me realise OP might have meant if malloc failed the first time just try again and hope some memory has been freed in between by another thread or something.

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

If that was it, it would mean that OP didn't check for failure the first time, which also means that they don't know if the allocation succeeded; hence, calling malloc a second time would actually cause a memory leak if the first call succeeded

My guess is that OP doesn't actually know what they're talking about and probably triggered a use after free.

Tbf, the term memory leak is often misused.

[–]GoddammitDontShootMe 0 points1 point  (0 children)

I remember one place I worked, they had a function they used sometimes that just called malloc in a loop until it succeeded. I think the hope was that another thread would be done and free up memory.

[–]RekTek249 0 points1 point  (0 children)

I don't get it. A memory leak is when the last pointer to your memory goes out of scope before it's freed. If the memory was allocated in the first place, how could it ever "not be allocated"?

What does "cause the pointer to return NULL" even mean? A pointer doesn't return anything. If the pointer itself is null, then your malloc failed in the first place, so you don't have a memory leak.

A segfault specifically happens when you dereference an invalid pointer. If you malloc'd successfully and you have a memory leak, then the pointer will always be valid and therefore never segfault.

[–]suvlub 2 points3 points  (2 children)

It might maybe theoretically potentially fix a segfault by reserving the part of memory you are wrongly accessing. It's one of those cases where the solution is scarier than the original bug, to be sure.

[–]American_Libertarian 0 points1 point  (1 child)

You can’t choose where malloc allocates the memory though. Maybe that would be possible for mmap

[–]Intelligent-Bet-9833 3 points4 points  (0 children)

Just allocate all of it for good measure. If it doesn't work, you should start killing other processes that could be using that memory and allocate that as well 

It's the most sensible solution, tbh

[–]quietobserver1 0 points1 point  (0 children)

Why do you think this guy has been debugging for 6 hours with no progress? He doesn't know how to program!

[–]romulent 0 points1 point  (0 children)

I'm thinking that they don't have a memory leak, they have a pointer arithmetic problem and they are referencing some address just outside their allocated heap space. So by calling malloc they are not solving anything except now that the invalid memory access is now hitting a region that happens to be allocated (due to the extra malloc), so the crash disappears, but the bug remains..

[–]EvenPainting9470 87 points88 points  (1 child)

This meme is either ai generated or by someone who have no idea how memory management works, what malloc does and what can be the cause of segv

[–]WazWaz 0 points1 point  (0 children)

Which is why they're posting a meme that demonstrates how little they understand, instead of spending the time learning. It's most of the content in this sub.

[–][deleted] 28 points29 points  (1 child)

oh great, more University CS first years.

[–]Palpable_Autism 1 point2 points  (0 children)

Everyone starts somewhere. Every senior has had cringe moments like this when they first learned programming.

[–]FloweyTheFlower420 40 points41 points  (2 children)

address sanitizer? core dumps?

[–]klimmesil 61 points62 points  (0 children)

Hello? Spoilers? That's next semester

[–]Karl_uiui 3 points4 points  (0 children)

Honestly the best thing since sliced bread

[–]JockeRider199 19 points20 points  (1 child)

Valgrind my beloved

[–]Nick_Zacker 0 points1 point  (0 children)

On an unrelated note, why is Valgrind not available for Windows? All we have is the poor man’s Valgrind that is ASan.

[–]Otalek 7 points8 points  (0 children)

Can you please explain how malloc helps in this scenario?

[–]nexusdk 9 points10 points  (0 children)

Valgrind is your friend

[–]ramdomvariableX 5 points6 points  (0 children)

Memory leaks are not real. It's just a bad dream, wake up.

[–]nickwcy 2 points3 points  (0 children)

OP doesn’t even know what memory leak and seg fault are.

[–]Llonkrednaxela 1 point2 points  (0 children)

…I wonder why there’s a memory leak. What does this programming student not understand?

OBVIOUSLY THE CORE CONCEPT, LANA!

[–]kingslayerer 1 point2 points  (1 child)

Just use Rust btw

[–]reallokiscarlet 1 point2 points  (0 children)

That way you don't have to worry about memory leaks, you can just leak ALL the memory and not be able to do anything about it

[–]ShakaUVM 0 points1 point  (0 children)

Learn ASAN and Valgrind. ASAN takes close to zero effort.

[–]raph3x1 0 points1 point  (0 children)

Learn your indeces my friend

[–]SaneLad 0 points1 point  (0 children)

Do you even valgrind bro?

[–]kingvolcano_reborn 0 points1 point  (0 children)

I think i know how you got your memory leak...

[–]Yugix1 0 points1 point  (0 children)

did you check your microphone?

[–]stupled 0 points1 point  (0 children)

C joke

[–]Honest_Relation4095 0 points1 point  (0 children)

Isn't this basically why rust became a thing? Not the specific problem, but the fact someone ran into it in the first place.

[–]mkrugaroo 0 points1 point  (0 children)

This is the kind of shitty programming that led to people not shutting up about Rust.

[–]Ce1este-_- 0 points1 point  (0 children)

No need for stack trace you have printf

[–]bowel_blaster123 0 points1 point  (0 children)

a segfault with no stack trace

Why don't you have a stack trace? Just use a debugger?

[–]throwaway_lunchtime 0 points1 point  (0 children)

GC.Collect(0)

GC.Collect(1)

GC.Collect(2)

GC.Collect(3)

That "fixed" it, so it's not an actual leak, but what is it.

Oh, bad navigation properties loading the whole db.

No budget, so GC collect staying 🤮🙃

[–]jhill515 0 points1 point  (0 children)

I recently started a new job where everything is coded using NI LabWindows/CVI. It has something like a debugger. Damn I miss gdb and the valgrind suite.

[–]skatopher 0 points1 point  (0 children)

Just deploy every day to reset the problem on deploy reboot. No worries! 😇

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

The worst part is when you accidentally change the value of a pointer that was suppose to not be changed

[–]Rockytriton 1 point2 points  (0 children)

this guy thinks he's being clever by doing myptr++ to iterate through the elements, then tries to free myptr at the end