CodeGym /Courses /Python SELF EN /RAM and Data Representation

RAM and Data Representation

Python SELF EN
Level 52 , Lesson 1
Available

8.1 RAM

Random Access Memory (RAM) of a computer is a collection of memory cells, each having a unique address. These cells can store different types of data like numbers, characters, and pointers. When a program runs, it places its data and instructions in this memory for quick access.

Example of memory cells:

0 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19
20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37 38 39
40 41 42 43 44 45 46 47 48 49
50 51 52 53 54 55 56 57 58 59
60 61 62 63 64 65 66 67 68 69
70 71 72 73 74 75 76 77 78 79

Each cell has its number, often called the memory address or simply address. If your computer has 8 GB of memory, it means it has 8 billion such cells where you can store something useful.

Bits and Bytes

Each cell can store one byte of information. Every byte consists of 8 bits, and each bit can be either 0 or 1. Example (for positive integers):

Number Bit Representation Byte-Aligned
0 0 00000000
1 1 00000001
100 1100100 01100100
1000 1111101000 00000011-11101000
1,000,000,000 111011100110101100101000000000 00111011-10011010-11001010-00000000

The more bytes a variable takes, the more values it can store. Example:

  • 1 byte — 256 unique values
  • 2 bytes — 65 thousand unique values
  • 3 bytes — 16 million
  • 4 bytes — 4 billion

8.2 How Data is Represented in Memory

Data Types and Their Representation

Numeric Data Inside the Processor and in Memory

  • Integers: stored in binary form. The size can vary (1 byte, 2 bytes, 4 bytes).
  • Floating-point numbers: stored in floating-point format (e.g., IEEE 754 format for 4-byte and 8-byte numbers).

x = 42  # Integer
y = 3.14  # Floating-point number

Important! In Python, the built-in types int and float are full-fledged classes capable of handling complex computations with numbers of arbitrary length. However, if you use libraries for scientific computations or AI, you'll encounter the data format I mentioned above.

Character Data In Computer Memory

Characters and strings are stored in memory as sequences of bytes. For example, in ASCII encoding, each character takes 1 byte; in UTF-8, it can take 1 to 4 bytes.

In Python 3.x, strings use UTF-8 encoding by default, but you can read files where text is stored in other formats or send data over the network not in UTF-8 encoding.

Example:


char = 'A'  # Character
string = "Hello, world!"  # String

Individual characters in Python don't have their type — the str type is used for them as well. However, in memory, these strings are stored character by character. One character usually takes 1–4 bytes.

Pointers

Pointers store addresses of other memory cells. They allow programs to work with dynamic data structures and manage memory more efficiently.

Example:


list = [1, 2, 3, 4]  # List
list_pointer = id(list)  # Pointer to the start of the list

8.3 Examples of Data Representation in RAM

1. Representation of Integers

Integers are stored in memory as binary numbers (bits). Depending on the data type, they can occupy different amounts of bytes. For example, int usually takes 4 bytes (32 bits).

This is how the number 42 will be represented in memory:

00000000 00000000 00000000 00101010

2. Representation of Floating-point Numbers

Floating-point numbers (for example, the type float) are stored in memory in floating-point format, usually according to the IEEE 754 standard. A float usually takes 4 bytes (32 bits), while a double takes 8 bytes (64 bits).

Important! These are standard data types bound to memory and the processor. The float type in Python corresponds to the commonly used double type and occupies 8 bytes.

This is how the number 3.14 will be represented in memory:

01000000 01001000 11110110 01100110

3. Representation of Characters and Strings

Characters (for example, the type char) are stored in memory as byte sequences. Strings are arrays of characters. In languages like C/C++, strings end with a zero byte (\0), but in Python, it's not the case.

This is how the string Hello will be represented in memory:

'H' 'e' 'l' 'l' 'o'

Which in turn will be represented as 0s and 1s:

01001000 01100101 01101100 01101100 01101111

8.4 Dynamic Memory Addressing

Dynamic memory is allocated and released during program execution as needed. All objects you create in Python are created in this memory.

It is divided into two types:

Heap: The area of memory from which dynamic objects are allocated. Management of this memory is done through allocation functions (for example, malloc in C) and release functions (for example, free in C).

Stack: The area of memory used to store local variables and function call data. Memory is automatically allocated and released when entering and leaving a function.

Again, I can't provide a Python example since it's too high-level for such actions. I can, however, give an example in C:


// Dynamically allocating memory for an array of 10 integers
int *dynamic_var = (int *)malloc(sizeof(int) * 10); 

// Releasing memory
free(dynamic_var);  

Memory Addressing is the process of determining a unique address for each memory cell. Each address points to a specific location in memory that can contain data or instructions.

Types of Addressing

Physical Addressing: direct access to physical addresses of memory cells. Managed by hardware (e.g., memory controller).

Virtual Addressing: uses a memory management mechanism, such as paged memory or segmented memory, to provide processes with isolated and protected address spaces.

2
Task
Python SELF EN, level 52, lesson 1
Locked
Binary Representation
Binary Representation
2
Task
Python SELF EN, level 52, lesson 1
Locked
Convert a number to decimal
Convert a number to decimal
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION