r/C_Programming • u/ab_do20_75 • Jun 27 '26
finally understood why C makes you manage memory manually and it changed how i think about programming
i spent a few months learning C and then i switched to python for my school the moment i actually understood what malloc and free do at the hardware level i realized every high level language was hiding this from me the whole time. felt like seeing behind the curtain. anyone else have that moment where C made other languages make more sense
323
Upvotes
144
u/Helpful-Primary2427 Jun 27 '26 edited Jun 27 '26
It’s really cool once it clicks! Small correction: malloc and free don’t actually do anything at the hardware level, they are C stdlib functions. You can even read the musl c malloc implementation to see what’s happening
88
u/mc_pm Jun 27 '26
Now learn a little assembly language, and you'll get the same insight about C :)
32
u/cards88x Jun 27 '26
Funny thing is, I came to C from a longtime ASM background and experienced C managing things that have to be manually handled in ASM: register usage/passing parameters to functions and DLLs, setting up stack frames, etc.
12
u/FrankieTheAlchemist Jun 28 '26
Garbage collectors are cool, and clever, and they make coding a lot easier but…all of that is obfuscation away from what’s actually happening. I won’t say I would always prefer to manually manage memory, but when it counts it really counts.
10
u/rb-j Jun 27 '26
For those of us using C embedded into a real-time process with a dedicated processor, it's what we do. We are told by the hardware guys how much memory we have and where it's at, and we have to deal with it.
5
u/Tall-Introduction414 Jun 27 '26
I think the reason has to do with being able to write high performance operating system kernels in the 70s. Ritchie didn't want to take flexibility away from the programmer for how memory is handled. Total freedom...
4
u/shreyabhinav16 Jun 28 '26
I think C is valuable not because everyone should write production code in it, but because it gives you a mental model of what's actually happening. You don't need manual memory management every day, but understanding why it exists makes you a better programmer in almost any language
3
u/weirdboys Jun 28 '26
Yes, I wished my university course really started with C instead of javascript. I only get the whole pointer, object, frame, etc, after I learned C and disassembled actual software. Otherwise programming will just remain as complicated calculator for me.
5
u/oceaneer63 Jun 27 '26
In a memory constrained embedded environment, I stay away from malloc() because what if in the unanticipated worst case not enough memory is available? So, I use a mix of global and local (stack based) variables.
2
u/duane11583 Jun 27 '26
Not specifically C in my case - it was the other way.
Yes, when I was digging inside of the language "tcl" - specifically the version known as: "jimtcl".
And saw how variables worked, and how objects worked and how functions/commands are implemented in a script language.
And at another level, how the language it self is implemented. I can see now how Lua, Python and other script languages are implemented. That was my own "ah-ha" moment of clarity,
2
u/SoapyWitTank Jun 27 '26
Now might be an appropriate time to have a little play with valgrind. You’ll be needing it and it may cement your new understanding.
2
u/nderflow Jun 28 '26
Just wait until you hear about condition codes and interrupt vectors.
2
u/-TRlNlTY- Jun 29 '26
Learn a bit of assembly and decompile a simple C program binary. C is a quite light abstraction of machine code, and it is pretty cool to understand.
2
u/flatfinger Jun 29 '26
On some implementations, malloc never actually creates memory allocations. Instead, a program is given a certain chunk of storage at startup and sets up a data structure listing what parts are available to satisfy malloc() requests. Calling malloc() removes a region of memory from that data structure, and calling free() adds it back so it can be used to satisfy future malloc() requests.
3
u/pjl1967 Jun 28 '26
That's precisely why my book has the subtitle it does.
Fun fact: It originally used "under the hood," but I changed it to "behind the curtain" to eliminate the difference between US and British English since it would more properly be "under the bonnet" in British English (though "under the hood" is still understood, especially in technical metaphorical uses).
1
u/A7r0z Jun 27 '26
thats why I'm getting bald lmao
1
u/eballeste Jun 29 '26
how about a brief recap of your aha moment and what you learned? I'm an idiot and still trying to figure this out.
1
u/sunmat02 Jul 01 '26
For me another language made C click: I was 14 when I learned C and I had a hard time understanding the concept of pointers. A couple of years later I learned Ruby. When you display an object in Ruby by default you get (or got, I don’t know, it’s been a while) something like <MyObject @ 0x….> (so you have an address in hexadecimal). Assign it to another variable and the address doesn’t change. When I noticed this, it clicked.
0
u/Biajid Jun 27 '26
C is just giving you a feeling that you are the boss- but in the back it’s doing nothing serious other than telling his big boss operating system to do it for you. Whether operating system would do it immediately is another story. The only assuring thing is that operating system might listen to C more than other language as it’s the first cousin. The only way to remove memory for sure is to physically flush it with voltage or current or photon in quantum era. It’s same like old era- the only way you hide some writing is to burn it. If you erase, and write something on top of it, an expert still can get it.
-1
u/Flagtailblue Jun 27 '26
Wait until you discover that C is just a portable language abstraction over assembler and a computer’s hardware architecture. 🤯
-9
u/seires-t Jun 27 '26
I mean, yeah...
Isn't that a bit obvious?
Maybe you should explore that idea a bit further.
Why does it matter to understand memory allocation?
What's the benefit, how does it change your way of coding?
-8
u/Royal-Ambassador-960 Jun 27 '26
Yes, many languages were designed to shield you from the ugliness and lack of safety of C.
0
u/[deleted] Jun 28 '26
[deleted] — view deleted comment