Notes
- Variable: a small space in computer's RAM that is assigned to a single piece of data. But a variable can only hold a single type of data. It's like a packet that can contain one snicker or one mars chocolate or one Reese's peanut butter cup.
- When we create a variable we take a specific chunk of RAM and name that chunk on the name of the variable. For example if there is a large container of chocolate, then we take one section, name it "Snickers" and now it can only contain snickers
- The size of the chunk depends on what type of data is stored inside the variable. Therefore that memory space holds that data / that type of data unless we delete the variable / close our after which computer assigns the same space to a different variable since computer RAM is limited.
- Data type / type is what type of data is stored inside a variable (is it a number, a character, a string aka words / sentences; string is called such because a word or a sentence is basically a string of characters)
- Inside our RAM there are these black chips that contain the transistors (tiny switches that turn on or off by the computer itself and each switch stands for a single bit aka binary digit aka 0 which means off and 1 which means on). Computer groups these transistors in a group of 8 therefore 8 bits = 1 byte.
- When we write a variable then it takes up the specific space for the data type we set it on like an int takes up 4 bytes aka 32 transistors.
- Usually, even though the transistors might be neatly stacked in a grid inside the RAM, so to human eyes the computer memory might be shaped like a grid, for a computer it is a continuous horizontal line. Not a grid
- When we run a program or give computer any command computer takes the binary of our source code (done in the assembling and linking process of compilation) or any command we give (like open a new tab in browser) and turns of the switches / transistors according to our code and executes it thereof THEN turns that into human language and shows us the result.
- Floating point imprecision is when there is a imperfection in any floating point number represented by the computer. It happens due to the rounding rule (if we cut off after certain number of digits after a floating point and the next number is 5 or more we MUST add 1 to the last number). This happens becuase a float variable only can have a finite amount of space and if we do a math like 10 / 3 then the result is forever 3.33333…… so just like when we are writing in paper we need to cut it off at some point cause we have finite amount of pages, the computer has to cut off the digits after it takes up more space than 4 bytes of transistors because that is the space allocated to a float (decided many years ago by some humans)
Insight
- Fun fact I found during my questions with AI session for today (15th June, 2026): Variables only ever exist in RAM. NEVER in Harddisk. When we write a program and write stuff like int a = 0; the computer does not allocate ANY space to it unless we actually run the program. A good analogy Gemini gave me was our source code is like the blueprint of a 10x10 house, it sits on our shelves but DOES not occupy any space in our land unless we build the house itself. And when the program running is over, the variable get rewritten aka the house is demolished and another house gets rebuild in the memory space (so THAT is why we get garbage values from!!! And also why we always keep saying that the memory is extremely finite even when we have 512 GB of SSD on laptops alone these days, the space doesn’t get allocated in the hard disk, ONLY in RAM!!!)
Questions
- floating point imprecision
- a string size depends on how my chars are inside it. But isn't that a memory issue? Plus if computer can't compare a char to another char and we can't perform conditionals on it then how come it knows how many characters are in a string aka how can it count that?
- if a program has thousands of bools as a data type, isn't that a huge wastage of memory? Cause at the end of the day, 7 bits of it are wasted.
Even if there are lots of transistors in a RAM the amount of instructions we give to a computer per second is a lot, then does that mean the transistors are changing their on and off position every second?
Round 2
Okay floating point imprecision also happens in calculators no? Give me an example of a floating point imprecision in programming. And does my program automatically follow arithmetic rules for cutting out a number for example if the next number in the floating point is 5 or more then we have to add 1 to the last number we're cutting out from (is THAT why these imprecisions happen? Like if someone asks to cut the number after 2 digits after the floating point and someone asks for say 5 digits... then it will be different no?)
Okay let me rephrase my second question. A compiler at least a C compiler really can't compare a string then how does it actually count how many charachters are inside the string and what do you mean it crashes into other data? Shouldn't it crash beforehand? For example, if I write a program that takes name of a user and prints it... Then how does computer allocate any space for printf if it doesn't know how long my string is going to be? It is bound to crash in the first place no? Computers can't guess
You said variable lives in RAM, but our program itself lives in the hard disk if we don't run it, then at some point, doesn't variable sit in the hard disk too?
Okay what do we mean by variable "expiring" after C executes programs? They are not parmanent labels for a chunk of memory?
Round 3
Okay so we will get floating point imprecision everytime we or the computer has to cut of a floating point number and it happens because of rounding number rule. In that case, does it vary from editor to editor or machine to machine because every machine can be slightly different? Like does the same code produce slightly different result in a local VS Code setup and say... VS Code for CS50?
Okay will the string thing be cleared up in the later weeks of cs50 like memory? Cause right now I feel i am biting more than I can chew..
Fun fact I found during my questions with AI session for today: Variables only ever exist in RAM. NEVER in Harddisk. When we write a program and write stuff like int a = 0; the computer does not allocate ANY space to it unless we actually run the program. A good analogy Gemini gave me was our source code is like the blueprint of a 10x10 house, it sits on our shelves but DOES not occupy any space in our land unless we build the house itself. And when the program running is over, the variable get rewritten aka the house is demolished and another house gets rebuild in the memory space (so THAT is why we get garbage values from!!!)
P.S. Honest confession - I don't know if I have mentioned this in my notes but the blueprint analogy is actually given to my by the AI. It was NOT made up by me. There is a small chance that I came up with this but I can't remember so thought it should be mentioned.
Top comments (0)