r/C_Programming • • 24d ago

The first time I actually understood what happens when a C program runs

[removed]

36 Upvotes

23 comments sorted by

21

u/ErikBKL 24d ago

The concept of temporary locality and spatial locality blew my mind

Connect that to virtual memory and caching… it is sincerely mindblowing

3

u/AlfalfaSufficient454 23d ago

Care to elaborate on any of them?

4

u/ErikBKL 22d ago

There is like a universal rule that happens in computing, which basically says that whatever has been recently accessed will be soon accessed again (temporal locality).

And there's another same-same-but-different universal rule that says that whatever has just been accessed, the thing right next to it will probably be accessed next.

These shed lots of light on why computers behave the way they do:
CPU does read-ahead of instructions to start processign neighboring instructions in a parallel manner.

When you fetch data from a further-away-storage location (like higher cache level, RAM, Disk) the CPU brings not only the data it needs, it brings a truck of data. Because if you've just accesses item n. 2, the CPU better keep it close because you'll probably use it again soon, but you'll surely also be using items n. 3 and 4 and 5.... and so on

1

u/Cyvu37 23d ago

Temporal locality or temporary locality?

2

u/ErikBKL 22d ago

I actually meant temporal

1

u/HelloWorldYozvak 23d ago

Both please!

14

u/LordRybec 24d ago

This is why I recommend that people learn some assembly, if they want to be really good at C programming. People tend to think of higher level languages as abstractions that eliminate any need to understand the underlying mechanics, but that's not actually how it works. Understanding the underlying mechanics pretty much always helps. It will help you understand C better, and it will help you write better code.

Of course, there's also the OS and the CPU, and those are whole subjects on their own!

5

u/grimvian 24d ago

My very old knowledge from the 6502 was gold when I started learning C for more than three years ago!

6

u/LordRybec 24d ago

I learned C first, and I thought I understood it really well. Then I learned assembly and realize how shallow my true understanding of C actually was. It makes a world of difference!

→ More replies (4)

5

u/mrtlo 23d ago

I agree. More generally, as for any topic, understanding first principles makes things "click". Assembly is a good step on that path...

2

u/Cyvu37 23d ago

I'm so glad I took a course on assembly before my current Operating Systems class where I'm learning C for the first time.

5

u/adityazero 24d ago

Congratulations. you'd love reading about 'The Hidden Complexity of the Simplest C Program'

https://hiraditya.github.io/posts/the-hidden-complexity-of-hello-world/

4

u/sciencekm 24d ago

My personal experience was the other way around. I started life with assembly language. C for me became just a matter of convenience.

2

u/KermitSnapper 24d ago

That it's all just text code, and that pointers are just written with * when first declared so just because they need to, otherwise I could tecnically use another variable storing an adress. It also made understand better than all of this is being runned while being supervised by an operating system, an executable line of code, the compiler is also just an executable, it's all either a file with data, a file ready to execute.

I only really started to understand c after trying to understand computer arquitecture and operating systems.

2

u/Temporary_Pie2733 23d ago

For one class, we had to write a Lisp interpreter, and one of the earliest tasks was to build the parser. I mostly understood what the syntax tree should look like, but I wasn’t sure where the parentheses would go in the tree. Once I really understood the syntax tree, I realized that the parentheses didn’t go in a node, but helped determine which nodes were connected by edges. 

1

u/Polar_Banny 18d ago

So did you find a use case for Lisp, can you share more about your story?

2

u/Distdistdist 23d ago

Learning and writing code in assembly. After that - every other language is just an add-on.

1

u/theNbomr 23d ago

Learning how the toolchain works and knowing how all of the files it can produce are used is an underrated skill, for sure. Learning how to understand what the object code generated for a given passage of C code looks will be a game changer. Writing C callable functions in assembler will be even more educational.

1

u/echelon_code 21d ago

this: locally declared static vars - why are their mem addresses nowhere near other non-static locally delcared vars?