Your Computer is Lying To You (Virtual Memory)
Вставка
- Опубліковано 30 лип 2024
- Patreon ➤ / jacobsorber
Courses ➤ jacobsorber.thinkific.com
Website ➤ www.jacobsorber.com
---
Your Computer is Lying To You (Virtual Memory) // New programmers usually don't understand how memory works. Addresses are addresses, right? This video gives a short introduction into what virtual memory is and-at a high level-how it works.
***
Welcome! I post videos that help you learn to program and become a more confident software developer. I cover beginner-to-advanced systems topics ranging from network programming, threads, processes, operating systems, embedded systems and others. My goal is to help you get under-the-hood and better understand how computers work and how you can use them to become stronger students and more capable professional developers.
About me: I'm a computer scientist, electrical engineer, researcher, and teacher. I specialize in embedded systems, mobile computing, sensor networks, and the Internet of Things. I teach systems and networking courses at Clemson University, where I also lead the PERSIST research lab.
More about me and what I do:
www.jacobsorber.com
people.cs.clemson.edu/~jsorber/
persist.cs.clemson.edu/
To Support the Channel:
+ like, subscribe, spread the word
+ contribute via Patreon --- [ / jacobsorber ]
Source code is also available to Patreon supporters. --- [jsorber-youtube-source.heroku...]
I am learning a lot with your low level code explanation videos. It is very useful to me. Thank you very much
You are welcome! Glad I could help.
you are hands down the best youtuber when it comes to explaining C coding , memory and computer science overall for beginners like me! the clearest explanation possible! huge thanks from Russia
Yeah, OSes provide a lot of abstractions *eg. device drivers* from the core functions like inb(),outb() or memset()
Best Comp Sci professor I have never paid for, thanks for sharing your knowledge sir.
You paid him with ads, by the way
4:22 mostly, the system tries to do that, the hardware MMU helps a bit, but it eventually fail, and the machine can crash, although rarely
Iirc, the operating system dispatches page tables unique to a process and when ever you access a memory, the mmu walks the page table to give you the data
I didn't notice that compiler always puts variables on the same addresses so I always wondered how buffer overflow attacks are done. I have watched some video about it and I never understood how you know with what number to overwrite the place on the stack where the previous instruction pointer is.
Hi Jacob, Big fan of you! I have question if address are virtual then why we bothering about memmove of address overlapping.. it will be translated by MMU right?
do you got Twitter? I tried searching for you with no avail. just wanted to let you know that you are awesome man. I love your C videos. please keep up the good work. 🖖
Top! Thanks!
Awesome video !
Awesome video
How are you running macOS and Linux together, are you using a remote machine? Also why the compiler chooses address for x so big, it can even choose something like 0x1 or 0x2 or whatever (I know choosing such big addresses doesn’t make the os allocate tonnes of memory [learned from one of your video]). Thanks in advance, amazing video.
A program’s memory is divided into segments which have pretty standard placememts in virtual memory. To make sure null pointers cause segfaults the area around zero (small addresses) is left unallocated. And you lose nothing by using big addresses rather than small addresses since virtual memory mapping (stored in tables in memory managed by the OS) also divides the address space (both virtual and physical) into pages that make small and high addresses the same. So, using bigger numbers is really just a convention that we stick to.
What LFalch said. And, I'm running macOS natively, and Linux on a virtual machine, using vagrant/virtualbox.
Thank you @LFalch , @Jacob Sorber ❤️
What Linux distro are you using? On my Ubuntu 21.10, the address of x does change every run. However, if I build with "-Wl,--no-pie", then yes, it's a static address each run, so I think your results are highly dependent on what default compiler/linker options your Linux distro is using.
I was using Ubuntu 20.04.3. But, yes, good point. It's not a Linux/macOS thing as much as an OS/compiler/linker thing. Those were just the two environments that I had handy.
Great!!!!!!!!!!!!!!!!
source code available during office hours at , that was what came to mind when listening to this, i cant seem to figure out why , can someone help ?
Thanks
my question is, why doesn't realloc always return the same pointer? so let's say we need 2MB of memory but there is not a chunk available on our physical memory, so our data is moved further. but all that needs to be changed is the virtual address to physical address map.
similarly, this could be done for the variables in stack too. right?
I'd assume virtual to physical address maps are using page addresses or even bigger blocks, so it's basically impossible to move small memory blocks. I also don't know much about structure of processor and/or memory, which i will hopefully learn more about later.
Yes. Now my question too. Why doesn't OS return the same address to the program every time realloc is called??
Likely because it couldn't fit in the virtual address space. If you have an allocation at 0x4 of size 2 and an allocation at 0x6 of size 2, then if you realloc the 0x4 alloc to a size of say 10 it would be overlapping with the other alloc if it used the same base address,so it returns a different address
@@DaNinjasDen oh thx for the explanation. it makes sense now. since pointer arithmetics are done on the virtual address, you have to change the virtual address too.
@@DaNinjasDen let's assume that there is no data in 0x6. Then if we want to extend the block that starts at 0x4 by another 2 bytes (total size will be 4) then will the OS decide to keep the original block in its place and add the required 2 bytes to it by allocating 0x6 and 0x7 to the original block??
Yeah but what if you need to know the actual address? What then?
Why would you want to know the actual address? Under what circumstance?
The physical address? In the cases, where you’d need that, you probably aren’t using virtual memory anyway. On a normal personal computer, there’s really no way for to find out since the OS manages the pages that map virtual and physical address space.
In systems, where some of the physical memory address space is actually some other device, you’re probably not dealing with virtual memory anyway and so it’s out of scope for this video.
@@anthonynjoroge5780 I don't know. But are you saying there are absolutely ZERO use cases in which you have to know the actual address?
Read from /proc//pagemap. Sudo is required though.
Probably you don't need it since it's abstracted away on OS level. When you NEED to know, you're pretty much interacting with hardware directly, no OS, no virtual memory and stuff. I used physical memory addresses when I was hacking Gameboy games where there's no OS at all, and you get direct access to Video RAM, IORAM, ROM addresses etc.
This is so low level that I missed bad paintings for better understanding
Jokes on it, I manipulate the crap out of it everyday!
We have a very abusive relationship
Fing OSs lying to us all the time🤬