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