MMGameslogo  MMGames
TwitterSharebutton  FacebookSharebutton   
learn through sufferingC Language
learn through sufferingC Language

Variables and Memory

Variables exist in memory.
第5章でのvariableのDescriptionHourに、variableはメモリに作られるとDescriptionしました。
However, they didn't explain the specific details at all.
It was because you didn't need to know that much if you were just dealing with variables.

However, in order to understand the concept of pointers that we will be dealing with shortly,
You need to understand how variables are stored in memory.

As explained in the previous section, computer memory is like a giant, single-row locker.
And each of these lockers remembers a state of on or off.
We refer to a single locker as one bit, and a group of eight lockers as one byte.
Memory locations are numbered and distinguished on a byte-by-byte basis.

Variables declared in a program are also stored in this memory with assigned numbers.
"It's just that it would be difficult to distinguish them just by number, so we're giving them names."
When compiled into an executable, variable names are converted to numbers.

In short, all variables are created in memory.
First, please keep in mind that they are being distinguished by numbers.

C is a programming language like this.
以後、この章では、C languageの実態に迫る展開が多々見られます。
今までの章でDescriptionされたなかったことの多くがこの章で明らかにされます。
その中では、C languageの文法などを批判するような展開も多く見られます。

そもそも、C languageは、デニス・リッチーという人物が、
自minute達で使うためだけに作ったprogramming言語であって、
世界中で使われるようになるとはまったく考えていなかったのis.
その為、作った人の都合に合わせたような部minuteが見られ、
普通の人々には不親切な文法の言語になっています。

しかし、それでも、C languageは世界中で使われているのis.
所々不可解な部minuteはありますが、もっとも汎用性のある言語であり、
小さな部品から、gameプログラムまで、width広く使用されています。

Display the memory address.
In the preceding section, we explained that all variables are created in memory and distinguished by their assigned numbers.
Actually, the numbers assigned to variables can be checked within the program.

Finding the number is surprisingly easy; you just need to use the %p specifier with the printf function.
However, you must prepend an ampersand (&) before variable names.
The following program is an example that displays the number of the int variable i.

Source code
#include <stdio.h>

int main(void)
{
    int i;
    printf("%p\n", &i);
    return 0;
}

The output of this program may be as follows.

Execution results
0012FF80

These are the results obtained in the author’s environment, although the numerical values may vary.
It depends on the computer and compiler being used.

Which memory address is assigned to a given variable.
which is automatically determined by the compiler (or, more accurately, the linker),
Furthermore, this value can also change due to the operating system's virtual memory functionality.

This number is in hexadecimal notation.It is 1245056 in decimal.
That's because it's displayed in a hard-to-understand format like hexadecimal.
As explained in the preceding paragraph, computer numbers are stored in binary.
In binary, the number of digits increases by powers of two (2, 4, 8, 16, and so on).
In base-10, the number of digits increases as you move along (e.g., 10, 100, 1000, and so on).
In hexadecimal, the number of digits increases with 16, 256, 4096, and so on.

As you may notice, decimal and binary don't mesh well.
Hexadecimal is convenient because it works well with binary.

Why do computers use binary?
突然ですが、あなたは両手の指10本でいくつまで数を数えられますか?
普通は10までですが、実は、理論的には1024まで数えられます。
指一本をbinary digits1桁に対応させて数えればいいのis.
この様に、decimal numberではなくbinary digitsで数えた方が、
回路を小型にできます。

However, here, hexadecimal representations aren't particularly important.
The key is that the assigned numbers for the variables are easily accessible.
The number assigned to this variable is called an address.

キーワード
【address】

The numerical address in memory assigned to a variable.


You can think of it as having the exact same meaning as a URL address.
The number that indicates the location of a variable is what we call an address.
Numbered variables
In the previous section, we only examined one variable, but this time we will examine the addresses of multiple variables.
Just to reiterate, what we're calling the locker number assigned to a variable, we're referring to as the address.
The following program is an example of displaying the addresses of three integer variables.

Source code
#include <stdio.h>

int main(void)
{
    int i1, i2, i3;
    printf("i1(%p)\n", &i1);
    printf("i2(%p)\n", &i2);
    printf("i3(%p)\n", &i3);
    return 0;
}

The output of this program may be as follows.

Execution results
i1(0012FF78) decimal numberでは1245048
i2(0012FF7C) decimal numberでは1245052
i3(0012FF80) decimal numberでは1245056

This is an example run in the author's environment.The numbers vary depending on the environment.
It appears that the sequential numbers are assigned with an offset of 4 to int variables.
It's simply because the size of an int is 4 bytes.

"Since 4 bytes is 4x8=32, it's 32 bits, or 32 digits in binary."
"It can store numbers from 0 to 4294967295 (approximately 4.3 billion) when negative values are not included."
"In the case of 2 bytes (16 bits), the range is from 0 to 65535."

Variables of types other than int also have different sizes depending on the compiler.
However, a char data type is always exactly 1 byte.
"to store character codes within the range of 0 to 255,"
"That's because you need to make sure it's always up to 255, or you'll run into problems."
いにしえのパソコンの中にはcharが7ビットというものも存在したらしいですが・・・

This time they are consecutive, but the addresses of variables do not always have to be consecutive.
Because these three variables are used separately.
It doesn't matter if we're in different places.
Some compilers may produce reversed order.
Array indices
Like variable numbers, arrays can also display addresses.
Tedious as it may be, an address is the locker number assigned to a variable.
For arrays, you don't need to add & at the beginning.
"However, since individual array elements are treated like variables, it naturally requires the & operator."
The following program is an example that displays an array and the addresses of its elements.

Source code
#include <stdio.h>

int main(void)
{
    int array[10];
    printf("array___(%p)\n", array);
    printf("array[0](%p)\n", &array[0]);
    printf("array[1](%p)\n", &array[1]);
    printf("array[2](%p)\n", &array[2]);
    return 0;
}

The output of this program may be as follows.

Execution results
array___(0012FF5C) decimal numberでは 1245020
array<0>(0012FF5C) decimal numberでは 1245020
array<1>(0012FF60) decimal numberでは 1245024
array<2>(0012FF64) decimal numberでは 1245028

It appears that arrays are also allocated in consecutive blocks of 4 bytes.
Furthermore, it seems that when an array name is specified, it resolves to the index of the array's first element.

In fact, all the tricks of the array are revealed here.
In fact, the array name represented the address of the array's first element.
"When referring to each element, I labeled them with numbers like [0], [1]. This means..."
This means to refer to the memory address of array name + element number.

Essentially, if you establish an initial address, you can increment it by adding a number to it.
It allows you to express a state where numerous variables are lined up.

However, what's a bit peculiar here is that the actual address increases by 4 when the element number is 1.
"This mystery should be solvable immediately if you remember the size of an int."

In a 32-bit compiler, the size of an int is 4 bytes.
In essence, the address of the next integer variable is the initial address plus 4.
Similarly, the address of the int variable two positions ahead is the initial address + 8.

We will explain the workings of this array in more detail later.


About This Site

Learning C language through suffering (Kushi C) is
This is the definitive introduction to the C language.
It systematically explains the basic functions of the C language.
The quality is equal to or higher than commercially available books.

Part 0: Program Overview
  1. What is a program?
Chapter 3: Displaying on the Screen
  1. String Display
  2. newline character
  3. Practice Problem 3
Chapter 4: Displaying and Calculating Numbers
  1. Display of numbers
  2. Basic calculations
  3. Numeric types
  4. Practice Problem 4
Chapter 6: Input from the Keyboard
  1. input function
  2. The fear of input
  3. Practice Problem 6
Chapter 9: Repeating a Fixed Number of Times
  1. Iterative sentence
  2. How Loops Work
  3. Practice Problem 9
Chapter 10: Repeating Without Knowing the Number of Times
  1. Unspecified loop
  2. Input validation
  3. Practice Problem 10
Chapter 13: Handling Multiple Variables at Once
  1. Handling multiple variables collectively.
  2. Arrays
  3. Practice Problem 13
Chapter 19: Dynamic Arrays
  1. Create arrays freely.
  2. Practice Problem 19