Canterbury Christ Church University

Computer Memory

Course Code: U19952

Course Name: Fundamentals of Computer Systems

Credits: 20

Module Leader: Ali Jaddoa

Credit to Seb Blair
Canterbury Christ Church University

Outline

  • We will cover introdution to memory

  • Then we will look into locality, heap and stack concepts

  • Finally, we will use the programming language C to see how memory is allocated for data.

Fundamental of Computer Systems: U19952-2023-2024
Canterbury Christ Church University

Memory

  • Anything that stores information
Characteristics
  • Cost (Time and materiles)
  • Speed (Should be fast)
  • Density (size, physical and logical)
  • Non-volatile/volatile??
  • Read (from) and write (to)
  • Power
  • Durability
  • Removable
Fundamental of Computer Systems: U19952-2023-2024
Canterbury Christ Church University

Types of Memory

Fundamental of Computer Systems: U19952-2023-2024
Canterbury Christ Church University

Memory Access

Fundamental of Computer Systems: U19952-2023-2024
Canterbury Christ Church University

Buses

Fundamental of Computer Systems: U19952-2023-2024
Canterbury Christ Church University

Data Flow

Fundamental of Computer Systems: U19952-2023-2024
Canterbury Christ Church University

Random Access Memory(RAM)

  • Temporary storage for all computer data
  • Also used as a temporary workspace/ scratch space for running applications
  • ‘Random access’ is the ability read/ write any single byte (via byte addressability)
  • This differs from ‘sequential access memory ’ (e.g. magnetic tape )
    center
Fundamental of Computer Systems: U19952-2023-2024
Canterbury Christ Church University

RAM (SDRAM/DDR)

Standard (Approximate Year Introduced) Operating Voltage Amount of Data Transferred (Words per Cycle) Associated RAM Clock Rates Approximate Transfer Rates
SDRAM (1993) 3.3 V 1 66 – 133 MHz 100 – 166 MT/s
DDR SDRAM (2000) 2.6 V, 2.5 V 2 100 – 200 MHz 200 – 400 MT/s
DDR2 SDRAM (2003) 1.8 V, 1.55 V 4 200 – 400 MHz 400 – 1066 MT/s
DDR3 SDRAM (2007) 1.5 V, 1.35 V 8 400 MHz – 1066 MHz 800 – 2133 MT/s
DDR4 SDRAM (2014) 1.2 V 8 1066 – 1600 MHz 1600 – 3200 MT/s
Fundamental of Computer Systems: U19952-2023-2024
Canterbury Christ Church University

Generally RAM is volatile and mainly comes in two variety's:

center

Fundamental of Computer Systems: U19952-2023-2024
Canterbury Christ Church University

Cache Analogy

vertical center

Every time you (CPU ) like to eat or drink (Access) something (Data) you first look into refrigerator (Cache), because it saves lot of time (Fast)! ​

If the item (Data) is not there you have to spend extra time (Slow) to get it (Data) from the supermarket (RAM or main memory)​

Fundamental of Computer Systems: U19952-2023-2024
Canterbury Christ Church University
Fundamental of Computer Systems: U19952-2023-2024
Canterbury Christ Church University

Principle of Locality

Programs tend to use data and instructions with addresses near or equal to those they have used recently​

​- Temporal Locality: Recently referenced items are likely to be referenced again in the near future​

center

  • Spatial Locality: Items with nearby addresses tend to be referenced close together in time​

center

Fundamental of Computer Systems: U19952-2023-2024
Canterbury Christ Church University

Locality Example

int sum = 0;​
int a[5]={1,2,3,4,5};
for ( int i = 0; i < n; i++ )​
{​
  sum += a[i];​
}
  • Data
    • Access array elements a[i] in succession – Spatial Locality
    • Reference sum each iteration – Temporal Locality ​
  • Instructions​
    • Reference instructions in sequence – Spatial Locality​
    • Cycle through loop repeatedly - Temporal Locality​
Fundamental of Computer Systems: U19952-2023-2024
Canterbury Christ Church University

Available Address

By convention, we express these addresses in base 16 numbers:

  • the smallest possible address is 0x00000000 (where the 0x means base 16),
  • and the largest possible address could be 0xFFFFFFFF.
Which is what in decimal?

Fundamental of Computer Systems: U19952-2023-2024
Canterbury Christ Church University

Stack and Heap

Fundamental of Computer Systems: U19952-2023-2024
Canterbury Christ Church University

Stack, Static and Heap

C has 4 different pools of memory.

  • Machine Code: stores the code being executed
  • static: global variable storage, permanent for the entire run of the program.
  • stack: local variable storage (automatic, continuous memory).
  • heap: dynamic storage (large pool of memory, not allocated in contiguous order).
Fundamental of Computer Systems: U19952-2023-2024
Canterbury Christ Church University
Fundamental of Computer Systems: U19952-2023-2024
Canterbury Christ Church University

Static

Static memory persists throughout the entire life of the program, and is usually used to store things like global variables, or variables created with the static clause. For example:

int somenumber = 5;

On many systems this variable uses 4 bytes of memory. This memory can come from one of two places. If a variable is declared outside of a function, it is considered global, meaning it is accessible anywhere in the program. Global variables are static,

static int somenumber = 5;
Fundamental of Computer Systems: U19952-2023-2024
Canterbury Christ Church University

Stack

  • The stack segment is near the top of memory with high address

  • Every time a function is called, the machine allocates some stack memory for it.

  • The allocation and deallocation for stack memory is automatically done

  • Stores variables used on the inside of a function (including the main() function).

Fundamental of Computer Systems: U19952-2023-2024
Canterbury Christ Church University

Stack

  • It’s a LIFO, “Last-In,-First-Out”, structure. Every time a function declares a new variable it is “pushed” onto the stack.

  • The stack is managed by the CPU, there is no ability to modify it

  • Variables are allocated and freed automatically
  • The stack it not limitless – most have an upper bound
  • The stack grows and shrinks as variables are created and destroyed
  • Stack variables only exist whilst the function that created them exists
Fundamental of Computer Systems: U19952-2023-2024
Canterbury Christ Church University

Stack Overflow

A stack overflow occurs if the call stack pointer exceeds the stack bound. The call stack may consist of a limited amount of address space, often determined at the start of the program.

Fundamental of Computer Systems: U19952-2023-2024
Canterbury Christ Church University

Heap

The heap is the diametrical opposite of the stack.

  • The heap is managed by the programmer, the ability to modify it is somewhat boundless

  • The heap is large, and is usually limited by the physical memory available in an embedded environment and in a PC it is stored within paging files on main memory (SSD)

  • This is memory that is not automatically managed – you have to explicitly allocate (using functions such as malloc(),calloc(),realloc()), and deallocate (free()) the memory.

  • The heap requires pointers to access it

Fundamental of Computer Systems: U19952-2023-2024
Canterbury Christ Church University
#include <stdio.h>
#include <stdlib.h>

int main(void) 
{
    int y = 4; char *str; 

    printf("stack memory: %d\n", y);

    str = malloc(100*sizeof(char)); 
    str[0] = 'm';
    for(int i =0; i< 100; i++)
    {    
        printf("heap memory: %c\n", str[i]); 
    }
    free(str); 
    printf("heap memory: %c\n", str[0]);
    return 0;
}
Fundamental of Computer Systems: U19952-2023-2024
Canterbury Christ Church University

Memory Allocation

a character, 1 byte of memory which is:

an integer or a float, 4 byte of memory which is:

a double value, 8 byte of memory which is :

Fundamental of Computer Systems: U19952-2023-2024
Canterbury Christ Church University

Memory Allocation: Pointers and Addresssing

  • In C/C++/C# you can access a variables address using the & (Ampersand) and * (Asterisk) symbol.​
  • With ‘address of’ & we can reference the variable’s address when used with itself. ​
  • A ‘pointer’ * is a variable that stores the address of another variable. ​
  • Be warned, playing with unprotected memory is dangerous and can cause systems to crash and even become unrecoverable.​
Fundamental of Computer Systems: U19952-2023-2024
Canterbury Christ Church University

Memory Allocation: C

int main ()​
{ // The variable has its own address (unknown to us now)​
  int n = 11; ​
  // this variable stores the address of the other variable​
  int *ptrToN = n; ​
  printf(“n’s address: %d and %d ptrToN value \n”, &n, ptrToN); ​
  printf(“n’s value: %d and ptrToN points to value %d \n”, n, *ptrToN); ​
  return 0;​
}

Output to terminal:
n’s address: 0x7fff20494e4c and 0x7fff20494e4c ptrToN value​
n’s value: 11 and ptrToN points to value 11​
Fundamental of Computer Systems: U19952-2023-2024
Canterbury Christ Church University

Memory Allocation Array: C

int main ()​
{​
  int n = 11, i;​
  char ptr[11] = "hello world";​

  for (i = 0; i < n; i++)
  {​
    printf ("\t%p      ||   ptr[%d]    =    %c\n", &ptr[i],i,ptr[i]);​
  }​
  
  printf("\t%p      ||   ptr[]     =  %c \n", &ptr,*ptr);​
  
  return 0;​
}​
Fundamental of Computer Systems: U19952-2023-2024
Canterbury Christ Church University

Memory Mangagment Lab

  • Link to the Lab (Walkthrough): Here.
  • Link to the Lab Repo on Github, we will be using Coderspaces: Here
  • Assessment Workshop, providing formative feedback for Assignment-1-Part-1
Fundamental of Computer Systems: U19952-2023-2024

## General Memory Layout ![bg right:50% 80%](../../figures/stackVsHeap.png) - **Machine Code**: stores the code being executed >> - **stack**: stores local variables (static nature) >> - **heap**: dynamic memory for programmer to allocate >>