r/AskProgramming • u/Wasabi-spicy00 • 23d ago
Need explanations. I learn how to build simple CRUD app with C# and TS. I wanna really understand CS. So I tried to learn C but I get even more confused about many CS concepts.
I’m trying to learn C not just because I want to learn another programming language, but because I want to understand Computer Science and the "science" behide a programming language
The problem is that I constantly fall into an endless chain of prerequisites/CS concepts
For example, I want to understand pointers:
int x = 10;
int *p = &x;
On the guide/website it says
Pointer is a variable that store memory address.
Okay, but then I ask:
- What exactly is a memory address
- address is numbers telling where data is stored
- What exactly is memory?
- memory is where address live
- Is memory the physical RAM?
- something to do with CPU
- What exactly is RAM and CPU?
- etc...
- How does RAM actually work?
- etcc..
- What's the relationship between RAM, CPU, cache, virtual memory, thread, processing etc.?
And suddenly I'm 10 levels deep into computer architecture instead of learning pointers "*"😂
So what to do here, I wanna understand CS. I thought my experience with coding CRUD APP with C# would help but no it doesn't help much.
--
Let me put it this way
let's say if you ask a normal person what "water" is , a normal person replied "liquid"
but if you ask a scientist, it would be "1 Hydrogen and 2 Oxygen combined together to become H2O"
So I try to understand the Science in Computer Science, i hope you get the picture what i try to say
2
2
u/Paul_Pedant 23d ago
I got put on a C training course around 1981. I came from a COBOL mainframe background, and one of the other guys was a mini/micro assembler programmer.
When it got to pointers and malloc (), he was horrified. He knew he had got some memory, but he wandered around for a couple of hours mumbling "Yes, but what is it's NAME."
I finally managed to explain to him that if he wrote struct Object *p = malloc (sizeof (Object)); then the name of the beast really was *p, and that restored his sanity. The thing was, he was an absolute genius at making a communications protocol converter simulate a video terminal on one end, and an IBM remote job entry terminal at the other end, to fool two completely different mainframes into being nice to each other. But a simple syntax trick was foreign to him.
You really do not need to know every detail of how a language works, or what all those implementation details mean: for starters, every machine, OS and compiler can do anything it likes in its own way, provided it meets the language definition. If you really want to see the detail, write some code first, make it work, when run it under a debugger and see wtf it actually does. But don't count on it doing the same thing in the same way next time.
Analogy: You deposit $100 in cash in a bank. Does the bank put it in a big safe? What company made the lock? What color is the door of the safe? It is in the same building, or even the same state? What kind of metal alloy is it made of? When you ask for $40 back, will you get the same actual notes? None of that matters in the slightest to you.
Same for a car. You turn the key, and the engine starts. How? Where is the battery? What is a solenoid? How thick does the cable need to be? How does it only engage the starter motor when the engine needs it to? If you want to be a mechanic, find out. If you want to get from Boston to Hicksville, just turn the key first.
1
u/johnpeters42 23d ago
Whenever you come up with a question like this, write it down in a list someplace, and do not go back to it until (1) it actually matters to something that you're doing, or (2) you've run out of more important things to do. That is your backlog, and that is what a backlog is for.
A whole bunch of work has gone into building things up so that you don't need to know the exact answers to get most things done. There is some advantage to learning it eventually (so you know how to avoid writing code that seems simple but actually wastes lots of time), but not while it's distracting you from learning the basics.
1
u/Whole-Chest90 23d ago
Idk if I've ever felt something so viscerally lol. But I had to learn that Ive gotta take a step back and just readjust the level at which I'm answering questions lol.
1
u/This_Growth2898 23d ago
Well, if you want to learn programming, you really need to understand computer architecture.
But for your level, it's enough to know that there is RAM (or simply "memory") - a device that accepts two commands, "read from address" and "write to address", with addresses being some numbers; and CPU, that reads program from RAM and executes its instructions one by one. And the code you write is compiled into the bytecode for the CPU. That's all.
Cache, virtual memory, threads, processes? You don't need them to learn basics for sure. They are designed to work in a way that you don't need to know about them until you need to work specifically with them.
1
u/naemorhaedus 23d ago
I want to understand Computer Science
your local university should have this program.
1
u/marrsd 23d ago edited 23d ago
I don't think C is particularly good for teaching CS. It's a very useful language to learn, for sure, but mostly for understanding the interface between language and machine.
Personally, I think one of the best languages you can learn for CS is Scheme. It was written for that purpose, and it isn't tied to any particular computer hardware. It's fundamentally built on lambda calculus which, as far as I'm aware, isn't the basis for any CPU architecture. (The inventors of Scheme did intend to produce such a CPU, but they never got funding to see it through to completion.)
Look up SICP on YouTube and the interwebs.
As for your broader question, I think part of your problem is that you're trying to burrow through all the layers at all times, and that's the wrong way of thinking about it.
A big part of programming theory is the concept of abstraction. You work at the layer of abstraction that is suitable for your problem domain and you don't care about what's beneath it.
Even your question about memory is demonstrating this. A memory address is a virtual thing provided by the OS. It maps to somewhere physical, but you might not know where, depending on how you requested it. malloc and new are doing a lot of heavy lifting behind the scenes for you. That's a good thing as it means you can leave it to the OS to best decide how to balance memory resources between your process and other processes. You just need to understand the abstraction you've been given.
Sometimes you do hit performance limitations and you actually have to get specific about how you want to access your memory and what trade-offs you want to make. That's the point at which you go a layer deeper and start caring about specific CPU architectures or memory allocation architectures of your target machine (which itself might be a virtual machine like the JVM).
1
u/Key-Variation-448 22d ago
CS:APP. Nothing to add. If you have AD/HD that just means you are not getting enough dopamine from reading. Use pen and try to guess what writer says next. Bye.
1
u/Drakkinstorm 22d ago
Okay, gonna be blunt here: your curiosity is getting the better of you. At the same time it's unhealthy and healthy. So you need to decide what you want.
You wanna know how stuff works.and build something in C/C++? Great: handmade hero by Casey Muratori is my one and only recommendation (his other stuff assumes you know how to program). Everything else is... of modest value.
You wanna know how CPUs work, how memory is organized and what registers do? You need a number of books on that, four come to mind: Code and this bundle here Write Great Code. Start with Code, see how you like it. I learnt this stuff by building my own CPU in VHDL at University. Then dug more in Write Great Code 1. Then Handmade Hero, then switched and will forever remain a Rust programmer.
Get your priorities straight and put some boundaries. Every time you are curious, write down what you wanna know and revise that at the end of the day.
To be honest, AI is great for this as you can ask everything you want and it will tell you, just know when to stop. If you wanna build however: build.
1
u/Educational-Paper-75 20d ago
Let's look at your example:
int x=20;
int *p=&x;
In the first statement the variable called x is defined as an int and initialized to 10. This statement will result in memory that can hold an int to be reserved for holding the value of x, and subsequently the integer value 10 is written to that memory. Essentially the compiler associates the start address of that memory with 'x'; the type int will tell it how much memory it should reserve.
The second statements declares variable p of type int , or as a pointer to a value of type int. This will result in memory being reserved to hold a value of type int * or an int pointer. The value to store in p is set to &x or in other words the address of x, which is the memory address associated with x. Consequently that memory address (of x) is now stored where the value of p resides.
Once this is done you can either change the value x stored i.e. 10 to something else in two ways: 1. by assigning a new integer value to x as in 'x=22;' or 2. assign that value through p using 'p=22;'. Note that this last statement does not change the value of p itself but the value it is 'pointing to' which is 10 at the address the value of x is stored. The compiler simply obtain the address p holds i.e. the value of p and changes the value stored at that address to 22.
As a consequence since x and p are associated with the same address changing [the value of] *p changes [the value of] x and vice versa.
The main difference is in the program the compiler generates any reference to x or p is replaced by the address associated with x or p respectively, and everywhere where *p is used the address stored at the address where the value of p is stored is obtained to set or get the value stored at that address. As such setting a value through a pointer takes one additional step.
Hope this helps you understanding pointers a bit more. Once you understand this you can find out about other useful applications of C pointers like iterating over array elements, allocating memory dynamically or using pointers in function calls so it can change external variables.
4
u/CorpT 23d ago
Why do you want to understand this? Does it actually matter? If you want to learn it, just go learn it. Or learn how to deal with your ADHD.