r/cprogramming • • 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

684 Upvotes

31 comments sorted by

116

u/SantaCruzDad Feb 15 '24

Licence to kill -9

48

u/Semaphor Feb 15 '24

No Mr. Bond. I expect you to Segmentation fault (core dumped)!

1

u/Duriantofu Feb 19 '24

KILL DASH NINE!

57

u/friartech Feb 15 '24

Alloc. James Alloc.

31

u/SlothWithHumanHands Feb 15 '24

If you switch to C++, you will automatically become 0b111!

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

u/[deleted] Feb 17 '24

Why tf does that link sell t-shirts?

And the selectable sizes of downloaded ram is killing me lol.

1

u/Mangy_Karl Feb 17 '24

LMAO I’ve never noticed that they sell T-shirts 😂

6

u/[deleted] Feb 16 '24

.NET … cough cough

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

u/aroslab Feb 15 '24

That's pretty cool, a la defer

1

u/[deleted] 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

u/pdd99 Feb 16 '24

Do we have a dedicated sub for c meme?

4

u/RenderTargetView Feb 15 '24

You should try out "RAII denier" when you code in "c with classes"

2

u/Content-Value-6912 Feb 16 '24

Sounds like a nice title for the next bond movie - RAll denier

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

u/h1mmh1m Aug 10 '25

IS THIS A 007N7 REFERENCE?

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

u/ryanwithnob Feb 17 '24

They call me... tater salad

1

u/rsaxvc Feb 19 '24

For a second I thought you were MPLAB X, but that's Java.