r/cprogramming • u/LuckOfTheEyerish • Feb 15 '24
They call me the 007 of C programming
0 best practices employed 0 free memory functions used 7 gigabytes of leaked memory
57
31
16
u/hansenabram Feb 15 '24
Maybe your users just don't have enough RAM
11
u/Mangy_Karl Feb 16 '24
5
u/hansenabram Feb 16 '24
Now that's a scary link if I've ever seen one
4
u/Mangy_Karl Feb 16 '24
I’ve often wondered how many people have tried this thinking it would work lol
3
Feb 17 '24
Why tf does that link sell t-shirts?
And the selectable sizes of downloaded ram is killing me lol.
1
6
7
u/71d1 Feb 15 '24
If you're using GCC, it supports the cleanup variable attribute.
Basically when you declare your pointer you specify the function used to free it, once your pointer goes out of scope, your cleanup function shall be automatically invoked.
https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html
That is, if you're allowed to use it. Maybe you're taking a class or something. I dunno.
5
u/og_math_memes Feb 16 '24
Seems like every systems programming language these days is getting support for something like this (Rust, Nim, C++, apparently C) and I'm here for it. Manual frees should be a thing of the past in most cases imo
1
u/71d1 Feb 16 '24
That's true if you own the hardware and don't mind spending money to upgrade the CPU/memory.
Take Linux for instance, the OS runs on very old hardware. Thanks to Linux I'm able to run a 12 year old computer, like brand new. A lot of people use Linux to extend the life of their computers.
Also if you work in embedded systems producing hundreds of thousands of circuit boards upgrading a chip, despite being a $0.10-$1 cost increase, could scale up quickly in price.
Also: C doesn't support that, it's the GCC compiler that extends support for that cleanup.
1
u/og_math_memes Feb 16 '24
I mean with Rust the memory only lives as long as you need it, and that is determined at compile time. In most cases this will cause no runtime overhead whatsoever, although I'm not sure about GCC's implementation. I run Linux on a 10ish year old computer as well. Rust support for kernel modules was added somewhat recently, although my version is behind.
Good point about it being GCC specific, I'm sure there are plenty of embedded systems that use their own compilers.
2
1
Feb 16 '24
[removed] — view removed comment
1
u/71d1 Feb 19 '24
Do you know if you declare a cleanup and then return that pointer (like an idiot), will the caller be subject to a use after free or is the compiler somehow smart enough to know it will still be used?
GCC will automatically reclaim that memory if you try to return the pointer.
4
4
2
u/kbder Feb 16 '24
I wrote a basic scheme interpreter in C but didn’t implement garbage collection. I think I ran (fib 10) and it was already up to 600MB ram. Sheesh!
1
1
u/finleybakley Feb 16 '24
Still less leaked memory than python xD
2
u/gotolabel Feb 25 '24
python:
an array of a single int: 64 bytes
the garbage collector: has holes in the container
the array:
1
1
116
u/SantaCruzDad Feb 15 '24
Licence to
kill -9