L02-memory Q about storing and accessing data in memory

119 views
Skip to first unread message

Reece McGowan

unread,
Sep 12, 2020, 4:58:27 PM9/12/20
to cmpt-295-sfu
# Lecture Question

Week : 1

Slide deck name: L02-memory

Hello, I had a question about storing data in memory. I think a few people had similar questions during the Friday lecture but I'm still confused. On a 64-bit machine, the word size is 8 bytes. Each memory address points to 1 "chunk" of memory that is 8 bytes long then, right? And the address specifically points to the first byte of the chunk of memory. So on a 64 bit machine, there are 2^64 potential addresses, some of which are listed on the right of the following image. 

So is the following picture legal?


My confusion comes from the fact that there's some language about how you can't address a chunk of memory partway through it, you need to access all the data. So I guess I understood that as you can only use the addresses listed on the right, i.e., you can only access the beginning of a word. Or does that refer to the fact that I can't access 0x19, 0x1A, or 0x1B for the int highlighted in red? I have to access the int from 0x18. But I can access the char located at 0x1C, even though it's halfway through a word, correct? So does the 2^64 potential addresses include all the locations between the beginning of words as well?

Any help would be appreciated, thank you.

Arrvindh Shriraman

unread,
Sep 13, 2020, 3:20:38 PM9/13/20
to cmpt-295-sfu
Great Question!
I will go over this in class. But here is the intuition. Hope it helps.

-- First, the visual representation in the slides in just to indicate word start and end points so that is easy to read. IT IS NOT HOW MEMORY IS ORGANIZED.

Memory is just a flat byte array.
------------------------------
Given a 64 bit system memory is just 2^64 byte array with indices 0 to 2^64 - 1.  All the addresses can be accessed and touched.

The question is given a data type (e.g., int or char) where in this byte array should we allocate it i.e., what is the starting address should we assign it. 
The starting address is actually related to how many bytes the data type itself requires. This is where alignment comes in.

Alignment
----------
The reason you need aligned is because only certain types of reads and writes can be efficiently performed by the underlying hardware. 
Each read specifies a start address and a certain number of bytes to read. When the start address is a multiple of number of bytes to read, the access is highly efficient. This is known as aligned data.  


On a 64 bit system
A char (one byte)    :    1-byte aligned.
A short (two bytes) : 2-byte aligned (only even addresses. Bottom bit of address is 0).
An int (four bytes)   : 4-byte aligned (only addresses which are multiples of 4. Bottom two bits of address are 0).
A long (eight bytes) : 8-byte aligned (only addresses which are multiples of 8. Bottom 3 bits of address are 0).
A double (eight bytes)     : 8-byte aligned.
A long long (eight bytes) : 8-byte aligned.
Any pointer (eight bytes) : 8-byte aligned.

This means that the data's starting address is a multiple of the data size (note that is says data size. Which has nothing to do with word size.)


---------------------------------------------------------------------------
A memory access (read/wirte) is aligned if data size = n bytes long and data start address is n-byte aligned (multiple of n). 

When a memory access is not aligned, it is said to be misaligned. 

A pointer that refers to primitive data that is n bytes long is said to be aligned if it is only allowed to contain addresses that are n-byte aligned, otherwise it is said to be unaligned. 

A memory pointer that refers to a data aggregate (a struct or array in C) is aligned if (and only if) each primitive datum in the aggregate is aligned.

A memory address a is said to be n-byte aligned when a is a multiple of n bytes

An n-byte aligned address would have a minimum of log2(n) least-significant zeros when expressed in binary.
--------------------------------------------------------------------------
I have included a new sample program under Syllabus: Week 1 : Additional notes. Int_align
Reply all
Reply to author
Forward
0 new messages