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...]

КОМЕНТАРІ • 40

  • @umpoucosobreconhecimentos
    @umpoucosobreconhecimentos 2 роки тому +30

    I am learning a lot with your low level code explanation videos. It is very useful to me. Thank you very much

    • @JacobSorber
      @JacobSorber  2 роки тому +7

      You are welcome! Glad I could help.

  • @olegfare4625
    @olegfare4625 2 роки тому +6

    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

  • @georgecop9538
    @georgecop9538 2 роки тому +5

    Yeah, OSes provide a lot of abstractions *eg. device drivers* from the core functions like inb(),outb() or memset()

  • @tranqilo6453
    @tranqilo6453 2 роки тому +6

    Best Comp Sci professor I have never paid for, thanks for sharing your knowledge sir.

    • @KangJangkrik
      @KangJangkrik 2 роки тому

      You paid him with ads, by the way

  • @monad_tcp
    @monad_tcp 2 роки тому +3

    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

    • @mr.mirror1213
      @mr.mirror1213 2 роки тому

      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

  • @smrtfasizmu6161
    @smrtfasizmu6161 2 роки тому +1

    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.

  • @Gauthamphongalkar
    @Gauthamphongalkar Рік тому +1

    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?

  • @foadsf
    @foadsf 2 роки тому +1

    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. 🖖

  • @starc0w
    @starc0w 2 роки тому

    Top! Thanks!

  • @hakimcherfi217
    @hakimcherfi217 2 роки тому

    Awesome video !

  • @tanchienhao
    @tanchienhao 11 місяців тому

    Awesome video

  • @raghavsrivastava2910
    @raghavsrivastava2910 2 роки тому +5

    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.

    • @LFSDK
      @LFSDK 2 роки тому +9

      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.

    • @JacobSorber
      @JacobSorber  2 роки тому +9

      What LFalch said. And, I'm running macOS natively, and Linux on a virtual machine, using vagrant/virtualbox.

    • @raghavsrivastava2910
      @raghavsrivastava2910 2 роки тому +1

      Thank you @LFalch , @Jacob Sorber ❤️

  • @funkykong9001
    @funkykong9001 2 роки тому +5

    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.

    • @JacobSorber
      @JacobSorber  2 роки тому +3

      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.

  • @arrahul316
    @arrahul316 2 роки тому

    Great!!!!!!!!!!!!!!!!

  • @JusticeNDOU
    @JusticeNDOU 2 роки тому

    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 ?

  • @dwaynestgeorge2558
    @dwaynestgeorge2558 Рік тому

    Thanks

  • @mehregankbi
    @mehregankbi 2 роки тому +2

    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?

    • @wumi2419
      @wumi2419 2 роки тому +1

      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.

    • @ohwow2074
      @ohwow2074 2 роки тому

      Yes. Now my question too. Why doesn't OS return the same address to the program every time realloc is called??

    • @DaNinjasDen
      @DaNinjasDen 2 роки тому

      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

    • @mehregankbi
      @mehregankbi 2 роки тому +2

      @@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.

    • @ohwow2074
      @ohwow2074 2 роки тому

      @@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??

  • @YouBuyWeRush
    @YouBuyWeRush 2 роки тому +6

    Yeah but what if you need to know the actual address? What then?

    • @anthonynjoroge5780
      @anthonynjoroge5780 2 роки тому +4

      Why would you want to know the actual address? Under what circumstance?

    • @LFSDK
      @LFSDK 2 роки тому +4

      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.

    • @YouBuyWeRush
      @YouBuyWeRush 2 роки тому

      @@anthonynjoroge5780 I don't know. But are you saying there are absolutely ZERO use cases in which you have to know the actual address?

    • @bamberghh1691
      @bamberghh1691 2 роки тому +2

      Read from /proc//pagemap. Sudo is required though.

    • @rmaster934
      @rmaster934 2 роки тому +1

      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.

  • @krumpy8259
    @krumpy8259 2 роки тому

    This is so low level that I missed bad paintings for better understanding

  • @XenoTravis
    @XenoTravis 2 роки тому

    Jokes on it, I manipulate the crap out of it everyday!
    We have a very abusive relationship

  • @xrafter
    @xrafter 2 роки тому

    Fing OSs lying to us all the time🤬