Thread: How is memory addressed?

  1. #1
    Registered User laughing_man's Avatar
    Join Date
    Jun 2009
    Location
    Bombay, India
    Posts
    26

    How is memory addressed?

    Are locations in the RAM addressed per word or per byte? I am using a 32 bit machine, so I think that it means that on my PC a word would be 4 bytes.

  2. #2
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    It uses virtual addressing.

    Read this : Virtual address space - Wikipedia, the free encyclopedia
    Last edited by MutantJohn; 09-20-2014 at 12:28 PM.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    The answer depends on Hardware (CPU/MCU) and the Operating System (OS).

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    Registered User laughing_man's Avatar
    Join Date
    Jun 2009
    Location
    Bombay, India
    Posts
    26
    +mutantJohn : No not that way.

    +stahta01 : I have an Intel Atom CPU, on a Gateway netbook, and Windows XP on it.

  5. #5
    Registered User laughing_man's Avatar
    Join Date
    Jun 2009
    Location
    Bombay, India
    Posts
    26
    Consider this

    Code:
    #include <stdio.h>
    
    int main(int argc, char argv[]){
    	char ch1,ch2,ch3,ch4;
    	ch1 = 'a';
    	ch2 = 'A';
    	ch3 = 'Z';
    	ch4 = -1;
    	printf("%c = %d = %x  @ %x\n",ch1,ch1,ch1,&ch1);
    	printf("%c = %d = %x  @ %x\n",ch2,ch2,ch2,&ch2);
    	printf("%c = %d = %x  @ %x\n",ch3,ch3,ch3,&ch3);
    	printf("%c = %d = %x  @ %x\n",ch4,ch4,ch4,&ch4);
    }
    
    //output
    a = 97 = 61  @ 12ff87
    A = 65 = 41  @ 12ff86
    Z = 90 = 5a  @ 12ff85
    .. = -1 = ffffffff  @ 12ff84
    If you see the hexadecimal values and addresses of ONLY ch1, ch2 and ch3 then it seems that, ok, char is occupying 1 byte of memory and each addresses are 1 stop after another. But when I see the stuff for ch4 it shows that char is taking 4 bytes (1 memory word for 32 bit computer) of memory. Even then the address hops by only 1 number. So what should I think? I have learnt that char uses only 1 byte. So does that mean that type X uses only m of n bytes where n bytes = 1 word? If so then does that mean that in a way the other 3 bytes are wasted? Is that why many programmers tend to store flags as bits in a single variable instead of having multiple variables?

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    The addresses shown indicate that there are indeed only one byte per char.
    Address 12FF84 (char4) can not hold more than one byte if the address for
    for char3 is 12FF85.

    Not sure why hex value of ch4 is printed using 6 characters.

    -

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > But when I see the stuff for ch4 it shows that char is taking 4 bytes (1 memory word for 32 bit computer) of memory.
    No, ch4 occupies 1 byte, just the same as all the other variables in your code.
    Use sizeof(ch4) to verify this.

    What you're seeing is the result of integer promotion by passing ch4 to printf (parameters which match the ... of the printf prototype are always default promoted). ch4 is prompted to an int (so 0xFF becomes 0xFFFFFFFF), and you print it as such.

    Use the correct format (perhaps %hhx) if you want to print only FF

    Also, to print addresses, use the %p format.
    As in
    printf("%p\n", (void*)&var );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User laughing_man's Avatar
    Join Date
    Jun 2009
    Location
    Bombay, India
    Posts
    26
    So according to your explanation a memory address is present for each byte. And each byte does not utilize the entire memory word in my case.

  9. #9
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    On a PC, from a program's perspective, memory is addressed per byte. The actual RAM width may be larger, which isn't an issue for reads, but for writes, you'd need a read / modify (the byte) / write cycle. Dynamic rams require a read / write sequence to perform writes, so the dynamic ram controller could take advantage of this by doing a read / modify / write sequence without any time penalty on writes. With a write cache, a sequential series of byte writes will probably end up combined into a ram width size before an actual write takes place.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > So according to your explanation a memory address is present for each byte.
    Yes.

    > And each byte does not utilize the entire memory word in my case.
    No.

    You're being confused by your poor choice of printf formats into thinking that FFFFFFFF somehow represents that a char occupies (or contains) 32 bits of data - it doesn't.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by laughing_man View Post
    Are locations in the RAM addressed per word or per byte? I am using a 32 bit machine, so I think that it means that on my PC a word would be 4 bytes.
    It'll depend on the architecture. You could have an architecture where only 32-bit words can be addressed, and they are labeled 0, 1, 2, 3... etc.

    But you might have an architecture where only 32-bit words can be addressed and yet they are labeled 0, 4, 8, 12... etc.

    In nature, most chips will have address modes based on byte addresses, with one or more larger word sizes also possible, though with alignment requirements. For instance, the address of the second 32-bit quantity in RAM is 4 (not 1), but you are only allowed to access addresses 0, 4, 8, 12 in this mode (a 32-bit access to address 3 would result in an alignment exception).

    Some platforms (x86) allow misaligned access, but incur a huge speed penalty when it happens.

    In practice, most architecture you would encounter will allow byte-level access because there are so many use cases for that. A very specialized processor might lack the ability.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  12. #12
    Registered User laughing_man's Avatar
    Join Date
    Jun 2009
    Location
    Bombay, India
    Posts
    26
    Thank you everyone for being patient with me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 10-15-2012, 05:16 PM
  2. Replies: 10
    Last Post: 07-10-2012, 04:28 AM
  3. Dynamic memory and realloc(), freeing memory
    By C_Sparky in forum C Programming
    Replies: 6
    Last Post: 10-06-2010, 07:55 PM
  4. Allocate memory inside allocated memory block?
    By Heidi_Nayak in forum C Programming
    Replies: 14
    Last Post: 04-15-2009, 04:19 PM
  5. Replies: 2
    Last Post: 09-28-2006, 01:06 PM