r/AskComputerScience • u/timidpippin • 3h ago
Dumb question maybe, but how much memory gets used just assigning addresses to memory?
If every byte has to have its own unique address, how is that actually kept track of? Is it just generated on the fly, or is there some equal amount of memory set aside just for producing and labeling those unique addresses?
If the addresses that already hold data aren't each stored somewhere individually, how does it avoid overwriting memory that's already in use?
And how much does ASLR affect all of this?
3
Upvotes
3
u/VirtuteECanoscenza 3h ago
If you mean the physical address nothing stores it. It's the position of the byte.
If you mean virtual addresses, you don't need to store reach one because virtual addresses typically use contiguous blocks of physical memory so you can only map at a higher level this reducing the number of things to track.
If you wanted to have a virtual address space where each physical byte can have an arbitrary address then yes your page table would be bigger than the original storage.
2
u/Single_Duck_4660 3h ago
Page tables stores the addresses I think
2
u/Some-Dog5000 3h ago
Whatever memory your operating system is using, part of that is dedicated to memory management. It's not constant, but it doesn't use a lot of memory, for the same reason that the index of a book is much more compact than the actual book.
The page table is the most relevant data structure here. A special piece of hardware, called the memory management unit, assists the OS with managing the page table by doing the "producing and labeling of addresses" (more accurately, translating a memory address that a program requests into a physical address).
1
u/dontwantgarbage 2h ago
Addresses are just numbers. If you have a bunch of boxes, you can take a pen and write “1” on one box, “2” on another box, etc. you didn’t “get” the numbers from anywhere. You just made them up and assigned each one to a box.
Now, of course, you shouldn’t write “1” one two different boxes because if somebody asks you for box 1, you won’t know which one to give them. But that’s on you.
For physical addresses, the motherboard circuitry assigns ranges of numbers to memory chips. So if somebody asks for byte 12345, it asks (say) memory chip in the second socket, “I now you have 10240 bytes inside you. Please give me byte 2105.”
For virtual addresses, the operating system tells the CPU, “if somebody asks for virtual byte 12345, go get physical byte 22585.” (Which the motherboard circuitry converts to “hey, memory chip in the third socket, give me byte 2105.”) This virtual-to-physical mapping itself requires memory.
ASLR is a policy that operating systems have that they will change up what virtual addresses they use for various things. Today the high score is stored at 12345 but tomorrow it is stored at 22585. This makes it harder for bad guys to attack the high score table because it keeps moving around.
(Sometimes hardware gets messed up and accidentally writes “1” on two different boxes. This is known as an address conflict and the result is that when the circuitry asks chip 2 for byte 2105, both chip 2 and chip 3 reply and things go downhill from there.)
1
u/stevevdvkpe 2h ago
The address of each memory location isn't stored in memory along with the contents of the location like a label. Memory is arranged as a fixed list of storage locations and an address is the position in that list. The 1000th item in the list doesn't have to be labeled with the number 1000; the memory hardware can just be asked "give me memory location 1000" and it finds that location by its position.
Programs have to keep track of the memory locations they are using to avoid overwriting memory locations that are already in use.
ASLR (Address Space Layout Randomization) is picking different base locations for portions of memory that don't need to reside at fixed locations, so that attacks that depend on overwriting a value at some known location can't easily predict where it will be. When a program is iniitalized it can be told "your data starts at this address and your stack starts at this address" and each program gets a different random starting address for its data and stack regions.
1
u/MasterGeekMX BSCS 2h ago
Thing is that you don't store each byte all the time.
Yes, each Byte get's it's own address, but in the end of the day they use the same amount of data. Take for example a street where 100 houses exist. While that is a lot, you can name every singe one with two numbers (assuming that the first house is number 0 and the last is 99).
Or a real world example, if you have 32 bits, you can count up to 4,294,967,296. That is the number of Bytes on 4 GB. This means that with the same 32 bits, you can address every single Byte in 4GB. No need to store a table with each, as naturally each are wired so the only way to access them is giving the address of each.
Now, when programs run, at the surface level there is no record of where each Byte lives directly, which is where I think your question lives. Instead programs simply "know" where they left variables, with instructions being constructed by the compiler to be done in such a way that all the code that access a given variable always works in some specific memory address.
0
u/LawElectrical2434 3h ago
Let's keep it simple, we have to differentiate between stack and heap.
Stack:
Like a growing stack of tasks... Imagine putting a paper with instructions. One instructions tells you to take another paper of instructions. You take it and put it on top of this one. You now only read the top page of instructions, it might tell you to take another paper, then you move on to that. As you see, that is a load of instructions, growing as a stack. Those papers can also hold variables. We do not need to track where they are, since, they are on the paper and we only read the top paper, even though we can look at references from variables handed over from below. But then, the reference tells us where it is. Fun fact, but not important, stacks grow from higher addresses to lower addresses... So think stalactites, not stalagmites.
Heap:
That is a form of memory that is random access, means you can get to any point. It also survives the destruction of your instruction paper, meaning you worked if off. But for that, you need to track where it is. And you have a logic where to find the new gap. An allocator usually does this. A common strategy is halving the space. You ask it for 100 bytes, it looks through the memory and finds it still empty, so, it halves it, looks at the first halve and finds 100 bytes free, so it halves it again, until it doesn't, then it gives you the address of the last halved point. Has the advantage of finding a right-sized gap quickly and the downside of leaving holes. Always a trade off. You can actually exchange allocators in applications and see if they are sped up by it. There is no optimal allocator for all programs. And that one quite simply tracks the memory spent in a table. It needs to know if it is allocated. But luckily it doesn't need to track all the memory, otherwise, it would need as much space as memory. It tracks usually an address and an offset to that address, which is a range. From here to there, allocated as one package.
And that's pretty much it. An easy overview. The details can and have filled multiple PhD theses.