CodeGym /Courses /C# SELF /Memory Addressing and Variables

Memory Addressing and Variables

C# SELF
Level 2, Lesson 4
Available

1. How Memory Works

Every computer has RAM (Random Access Memory). So, what is it, what are its properties, and most importantly, why should we care?

Every program (including those written in C#) gets loaded into RAM before it runs. In RAM, you’ll find the program’s code (which the processor executes) and the program’s data (which the program itself puts into memory).

So, what is RAM and what does it look like?

Picture Excel 😎 An Excel sheet is made up of cells, and each cell has its own unique number (A1, A2, ... B1, B2). If you know the cell’s number, you can always write a value into it or get the value stored there. Computer memory works pretty much the same way.

memory-excel

The program and its data are stored in memory while it’s running. All computer memory is basically a bunch of tiny cells – bytes. Each cell has its own unique number – 0, 1, 2, 3, ...; (the numbering starts at zero). If you know the cell number, you can save some data in that cell. Or grab it from there. Some cells store the program’s code – a set of instructions for the processor, others store the program’s data. The cell number is also called the cell address.

The processor knows how to execute commands from the program loaded into memory. Pretty much all processor commands are like: grab data from some cells → do something with it → put the result into other cells.

By combining hundreds of simple commands, we get complex and useful ones.

When you declare a variable in your code, it gets a chunk of unused memory. Usually, that’s a few bytes. When you declare a variable, you have to specify the type of info the program will store in it: numbers, text, or other data. Without knowing the type, there’s no way to know how big a memory block to set aside for the variable.

Back in the early days of computers, programs just worked with memory cell numbers, but later, for programmers’ convenience, cells started getting names. A unique variable name is mostly for the programmer’s convenience: the program itself would be totally fine just using numbers.

2. Variables in Memory

In C# there are 4 main data types for storing whole numbers: byte, short, int, and long.

Type Size, bytes Name Origin
byte 1 Byte, because it takes up one byte of memory
short 2 Short for Short Integer
int 4 Short for Integer
long 8 Short for Long Integer

Also, in C# there are 2 floating-point types — float and double:

Type Size, bytes Name Origin
float 4 Short for Floating Point Number
double 8 Short for Double Float

Every time your program hits a variable declaration, it sets aside a small chunk of memory (the size depends on the variable’s type).

int a = 10;
Declaring a variable a of type int and allocating memory for it
Important!

The address of a variable is considered to be the address of the first cell in the memory block set aside for it.

C# programs aren’t allowed to access memory directly. All memory operations are tightly controlled by C# and .NET.

3. The string Type in Memory

The string type can store a lot of data, so it’s not just a data type, it’s a full-blown object.

The actual string data (the text) is put into a special object, which gets its own chunk of memory, and then the address of that object is stored in the variable, which also gets its own memory.

  • Variable a of type int takes up 4 bytes and stores the value 1.
  • Variable b of type int takes up 4 bytes and stores the value 10,555. The comma isn’t a decimal point, it’s just a thousands separator. The decimal part is separated by a dot.
  • Variable d of type double takes up 8 bytes and stores the value 13.001.
  • Variable str of type string takes up 4 bytes and stores the value D12 — the address of the first cell of the object that holds the text.

The object of type string (that holds the text) is stored in a separate memory block. The address of its first cell is stored in the variable str.

4. What Happens When We Assign

Another important thing — how assignment works. For example:

int a = 10;
int b = a;
b = 20;
Console.WriteLine(a); // 10

In this example, two cells are created in memory: one for a, one for b. When we write b = a;, the value (10) is copied, not the “box” itself. Changing b doesn’t affect a at all.

Now, same thing but with strings:

string s1 = "Hello";
string s2 = s1;
s2 = s2 + " World";
Console.WriteLine(s1); // "Hello"
Console.WriteLine(s2); // "Hello World"

Here, both variables s1 and s2 point to the same string "Hello" until you change it. But when you do s2 = s2 + " World", a new string "Hello World" is created for s2, and s1 still points to the old string "Hello".

5. Why Programmers Count from Zero

People are always surprised why in programming we almost always start counting from zero. The thing is, there are a ton of situations where counting from zero is just easier (though sometimes it’s more convenient to start from 1).

The simplest example is memory addressing. If your variable gets 4 bytes of memory and you have X – the address of the first byte, what are the addresses of all the bytes? X+0, X+1, X+2, X+3. So you get a group of bytes with indexes 0, 1, 2, 3.

Whenever you think about a relative address inside some data block, you always end up counting from zero. That’s the first and most common reason for zero-based counting.

2
Task
C# SELF, level 2, lesson 4
Locked
The God-Emperor protects
The God-Emperor protects
2
Task
C# SELF, level 2, lesson 4
Locked
Humanity Will Prevail
Humanity Will Prevail
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION