Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The answer to the second part of you question (on most modern OSs) is virtual memory.</p> <p>You start at the hardware layer with physical memory. That's the stuff you can actually poke with your finger. This is what the operating system sees. The operating system lets you run processes on an abstraction called virtual memory.</p> <p>Each process gets its own virtual memory space. So it can pretend that it's the only process running, and it has tons of memory. Then each time you access memory, you supply a virtual address, which gets mapped to a physical address. The operating system keeps a table of which virtual address gets mapped to which actual physical addresses in RAM. Typically this is done with some special hardware as well (an MMU, memory management unit) for performance reasons, but you could do it 100% in software too.</p> <p>So when you say 0x000 in your program, that's a virtual address. It gets translated into a physical address by the computer when you read or write. So in another process, the same virtual address 0x000 maps to a different physical address. This system lets you write your program without knowing exactly how much RAM is available, or what address your program will be loaded into. It also prevents your program from trashing memory that belongs to another program.</p> <p>As for the first part, absolutely. Different types of data take different amounts of memory. You have to know how much space you need when you lay out your data structures. There are also byte-alignment issues to keep in mind. Multi-byte data types (eg floating point numbers) often have to start at an address that is divisible by 2 or 4 or the number of bytes it takes to store a float -- it's a requirement of the processor or the RAM. So you can't just crunch all your data together, one byte after the next, you have to lay it out in a specific order like fitting together the pieces of a puzzle if you want to minimize unused memory.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload