Whenever I see code like this:
void test() {
u8 x[] = {0, 22, 43};
}
I'm imagining a block of memory stored in ram with the following layout:
suppose x started at address 0x00.
0x00 00000000
0x01 00010110
0x02 00101011
so that at address 0x00 we have the binary of 0, at address 0x01 we have the binary of 22, and we have the binary of 43 at address 0x02.
But does that imply ram stores really 8 bits for every memory address?
Or is it structured a bit like this, with a cell width of say 32 bit:
0x00 00000000 00000000 00000000 00000000
0x01 00000000 00000000 00000000 00010110
0x02 00000000 00000000 00000000 00101011
Is it electronically implemented like that? I'm really referring to modern ram implementations, no those simpler 8 bit versions on old computers.
Take a look at this other instance:
void test() {
u16 y[] = { 0, 32, 11 };
}
Now every element is 16 bit wide. What tricks me is that I can cast it to a u8 pointer and literally access every byte of every 16bit element in the array. So I'm imagining the memory layout for this is:
0x00 00000000
0x01 00000000
0x02 00100000
0x03 00000000
0x04 00001011
0x05 00000000
or is it a bit like this:
0x00 00000000 00000000 00000000 00000000
0x02 00000000 00000000 00000000 00100000
0x04 00000000 00000000 00000000 00001011
Where at address 0x00 you in reality can access the address 0x01 from 0x00 itself. Can someone help me clarify how DRAM is internally structured? (Nothing too complicated just a high-level view for me to understand what's the right way to think about it).
Does this have some correlation to the fact that CPUs want addresses to be aligned to multiples of two? I don't know where I heard that, but I'm really curious how this all works.
Modification of this:
I literally have no idea what protocol it is used for DRAM and CPU to communicate, I'm always assuming there's an address line of 32 bit for the address, and another 32 bit data line to read/write data from/to.
So in the second example where I showed the u16 array, to read from RAM you have to set the address lines to address 0x00, then read all at once the 16 bits from the data lines. But, if you again cast the data as a u8 pointer and read it, then you can still say to the code to read at address 0x00, and get the first byte of the first 16 bit element of the array, then at address 0x01 read the second byte of the first 16 bit element, and so you would put address 0x00 on the address line for both readings and the extrapolate from the data line the byte by shifting? I've never understood that.