• Название:

    Лекция 3 Массивы, указатели и память (с заметками)

  • Размер: 0.3 Мб
  • Формат: PDF
  • или
  • Название: Основы программирования - Лекция № 3, 17 марта 2016 г.
  • Автор: Владимир Парфиненко



фӐӌӍҺӝ ӎӏӍһӏҸӋӋӄӏӍҺҸӌӄӨ
сҿӈӗӄӨ ॏ ߟࡈ ߝߣ ӋҸӏӑҸ ߞߜߝߢ һࡏ


http://xkcd.ru/371





тҸӐӐӄҺӝ






фҾӌӍӋҿӏӌӝҿ ӋҸӐӐӄҺӝ

тӍӑӄҺҸӗӄӨࡇ ҾӊӨ ӃҸҾҸӌӄӨ ӈҺҸҾӏҸӑӌӍһӍ ӒӏҸҺӌҿӌӄӨ ӌӒӂӌӍ ߟ
ӎҿӏҿӋҿӌӌӝҿࡇ ŏࡈ ųࡈ żࡏ Я ӖӑӍ ҾҿӊҸӑӛ Ӑ ӒӏҸҺӌҿӌӄҿӋ ߝߜࡷӍӅ Ӑӑҿӎҿӌӄࡐ
хӏҸҺӄӊӛӌӍࡈ ӌӒӂҿӌ ӋҸӐӐӄҺࡋ

тҸӐӐӄҺ ࡴ ӒӎӍӏӨҾӍӖҿӌӌӝӅ ӌҸҹӍӏ ӢӊҿӋҿӌӑӍҺ ӍҾӌӍһӍ ӑӄӎҸࡏ

аҿӃ ӄӌӄӗӄҸӊӄӃҸӗӄӄ ӋҸӐӐӄҺࡈ ӍҹӜӨҺӊҿӌӌӝӅ Һ ӔӒӌӈӗӄӄࡈ ӐӍҾҿӏӂӄӑ
ӋӒӐӍӏࡏ

фҹӜӨҺӊҿӌӄҿ ӋҸӐӐӄҺҸ arrࡈ ӐӍӐӑӍӨәҿһӍ ӄӃ N ӢӊҿӋҿӌӑӍҺ ӎӏӍӄӃҺӍӊӛӌӍһӍ ӑӄӎҸ Tࡇ

љӊҿӋҿӌӑӝࡈ ҾӊӨ ӈӍӑӍӏӝӕ ӌҿӑ ӨҺӌӍ ӃҸҾҸӌӌӍһӍ ӃӌҸӖҿӌӄӨࡈ
ӄӌӄӗӄҸӊӄӃӄӏӒӧӑӐӨ ӌӒӊӨӋӄࡏ

T arr[N];

һҾҿ N ࡴ ӈӍӌӐӑҸӌӑҸ ҺӏҿӋҿӌӄ ӈӍӋӎӄӊӨӗӄӄࡏ

цҸӃӋҿӏ ӋҸӐӐӄҺҸ ӋӍӂҿӑ ҹӝӑӛ ҺӝӖӄӐӊҿӌ ҸҺӑӍӋҸӑӄӖҿӐӈӄࡈ ӖӑӍ ӋӍӂҿӑ
ҹӝӑӛ ҾӍҺӍӊӛӌӍ ӒҾӍҹӌӝӋࡏ

хӏӄӋҿӏ ӍҹӜӨҺӊҿӌӄӨ ӄ ӄӌӄӗӄҸӊӄӃҸӗӄӄ ӋҸӐӐӄҺҸࡇ
int xs[4] = {20, 10}; // {20, 10, 0, 0}
int ys[] = {20, 10}; // {20, 10}

ߟ





фҾӌӍӋҿӏӌӝҿ ӋҸӐӐӄҺӝࡇ ҾӍӐӑӒӎ ӈ ӢӊҿӋҿӌӑҸӋ

еӍӐӑӒӎ ӎӍ ӌҿҾӍӎӒӐӑӄӋӍӋӒ ӄӌҾҿӈӐӒ ࡫ࡷߝࡈ Ÿࡈ ߞࡁŸ࡬ ࡴ ӌҿӍӎӏҿҾҿӊҿӌӌӍҿ
ӎӍҺҿҾҿӌӄҿࡏ

T arr[N]ࡇ






0ࡏ

1ࡏ











ࡊࡏ




N-1

эӑҿӌӄҿ ӢӊҿӋҿӌӑҸ ӄӃ ӋҸӐӐӄҺҸ ӎӍ ӄӌҾҿӈӐӒ iࡇ
T value = arr[i];

кҸӎӄӐӛ ӢӊҿӋҿӌӑҸ Һ ӋҸӐӐӄҺ ӎӍ ӄӌҾҿӈӐӒ iࡇ
arr[i] = new_value;
ߠ





еҺӒӋҿӏӌӝҿ ӋҸӐӐӄҺӝ

хҿӏҺҸӨ ӏҸӃӋҿӏӌӍӐӑӛ ࡴ ӈӍӊӄӖҿӐӑҺӍ ӐӑӏӍӈࡈ ӈҸӈ Һ ӊӄӌҿӅӌӍӅ
Ҹӊһҿҹӏҿࡏ

T arr[N][M]ࡇ


0ࡏ




ࡊࡏ










M-1


ࡏࡏࡏ













N-1











0ࡏ

int m[2][3] = {
{1, 2, 3},
{4, 5, 6}};



ɾʶȩ ӎӍӊӒӖӄӑ ӃӌҸӖҿӌӄҿ ߞࡈ Ҹ ҺӋҿӐӑӍ ߢ ҹӒҾҿӑ ӃҸӎӄӐҸӌӍ ߢߢࡏ

int two = m[0][1];
m[1][2] = 66;

ߡ





тӌӍһӍӋҿӏӌӝҿ ӋҸӐӐӄҺӝ

T arr[N1][N2][...][Nk] ࡴ ҺӐҿ ҸӌҸӊӍһӄӖӌӍࡏ

ߢ





хҿӏҿҾҸӖҸ ӋҸӐӐӄҺӍҺ Һ ӔӒӌӈӗӄӄ

ɯǞˌƚ࡙ɾ ࡴ ӐӎҿӗӄҸӊӛӌӝӅ ҹҿӃӃӌҸӈӍҺӝӅ ӑӄӎ ҾҸӌӌӝӕࡈ ӈӍӑӍӏӍһӍ
һҸӏҸӌӑӄӏӍҺҸӌӌӍ ӕҺҸӑҸҿӑ ҾӊӨ ӕӏҸӌҿӌӄӨ ӏҸӃӋҿӏҸ ӊӧҹӍһӍ ӋҸӐӐӄҺҸ
ӌҸ ҾҸӌӌӍӅ ҸӏӕӄӑҿӈӑӒӏҿࡏ

кҸӖҸӐӑӒӧࡈ ӏҸӃӋҿӏ ӋҸӐӐӄҺҸ ӎҿӏҿҾҸҿӑӐӨ ӍӑҾҿӊӛӌӝӋ ӎҸӏҸӋҿӑӏӍӋࡇ
int sum_values(int values[], size_t size) {
int sum = 0;
for (size_t i = 0; i < size; i++) {
sum += values[i];
}
return sum;
}

еӊӨ ӋӌӍһӍӋҿӏӌӝӕ ӋҸӐӐӄҺӍҺ ӃӌҸӖҿӌӄӨ ҺӐҿӕ ӏҸӃӋҿӏӌӍӐӑҿӅࡈ ӈӏӍӋҿ
ӎҿӏҺӍӅ ӔӄӈӐӄӏӒӧӑӐӨࡏ

int sum_pairs(int pairs[][2], size_t size) {
// ...
}
ߣ





хҿӏҿҾҸӖҸ ӔӄӈӐӄӏӍҺҸӌӌӝӕ ӋҸӐӐӄҺӍҺ Һ ӔӒӌӈӗӄӄ
тҸӐӐӄҺӝ ӂҿӐӑӈӍ ӔӄӈӐӄӏӍҺҸӌӌӝӕ ӏҸӃӋҿӏӌӍӐӑҿӅ ӎҿӏҿҾҸӧӑӐӨ ҹҿӃ
ҾӍӎӍӊӌӄӑҿӊӛӌӝӕ ӎҸӏҸӋҿӑӏӍҺࡇ
int sum_matrix(int matrix[3][3]) {
int sum = 0;
for (size_t i = 0; i < 3; i++) {
for (size_t j = 0; j < 3; j++) {
sum += matrix[i][j];
}
}
return sum;
}

ߤ





бӍӃҺӏҸәҿӌӄҿ ӋҸӐӐӄҺӍҺ ӄӃ ӔӒӌӈӗӄӅ

ыӍӏӋҸӊӛӌӍ һӍҺӍӏӨࡈ ӔӒӌӈӗӄӨ Һ + ҺӍӍҹәҿ ӌҿ ӋӍӂҿӑ ӄӋҿӑӛ ӋҸӐӐӄҺ Һ
ӈҸӖҿӐӑҺҿ ҺӍӃҺӏҸәҸҿӋӍһӍ ӃӌҸӖҿӌӄӨࡏ
ɯǞˌƚ ࡴ ӌҿ ӋӍӂҿӑ ҹӝӑӛ ӄӐӎӍӊӛӃӍҺҸӌҸ ҾӊӨ ӄӌӄӗӄҸӊӄӃҸӗӄӄ ӋҸӐӐӄҺҸࡈ
ӑࡏ ӈࡏ ӌҿ ӨҺӊӨҿӑӐӨ ӈӍӌӐӑҸӌӑӍӅ ҺӏҿӋҿӌӄ ӈӍӋӎӄӊӨӗӄӄࡏ

int[] reverse(int arr[], size_t size) {
// compile time error
int result[size]; // compile time error
// ...
return result; // run time error
}

аӍӊҿҿ ӑӍһӍࡈ ӑҸӈӍӅ ӋҸӐӐӄҺ ӒӌӄӖӑӍӂӄӑӐӨ ӎӍӐӊҿ ҺӍӃҺӏҸәҿӌӄӨ ӄӃ
ӔӒӌӈӗӄӄࡈ ӌӍ Ӎҹ ӢӑӍӋ ӎӍӃӂҿࡏ
аӍӊҿҿ ӑӍһӍࡈ Һ ӋҸӐӐӄҺӌӒӧ ӎҿӏҿӋҿӌӌӒӧ ӌҿӊӛӃӨ ӌӄӖҿһӍ
ࢀӎӏӄӐҺӍӄӑӛࢁࡈ ӖӑӍ ҾҿӊҸӑӛ Ӑ ӏҿӃӒӊӛӑҸӑӍӋࡐ

int original[] = {1, 2, 3};
int reversed[] = reverse(original, 3);
// compile time error

ߥ





ࢀбӍӃҺӏҸәҿӌӄҿࢁ ӋҸӐӐӄҺӍҺ ӄӃ ӔӒӌӈӗӄӅ

хҿӏҿӈӊҸҾӝҺҸҿӋ ӍӑҺҿӑӐӑҺҿӌӌӍӐӑӛ ӃҸ ӐӍӃҾҸӌӄҿ ӋҸӐӐӄҺҸ ӌҸ
ҺӝӃӝҺҸӧәӒӧ ӔӒӌӈӗӄӧࡏ

void reverse(int dst[], int src[], size_t size) {
for (size_t i = 0; i < size; i++) {
dst[i] = src[size - i - 1];
}
}
int original[3] = {1, 2, 3};
int reversed[3];
reverse(reversed, original, 3);
// use reversed

ߝߜ





щӈҸӃҸӑҿӊӄ






щӈҸӃҸӑҿӊӛ ӈҸӈ ҸҾӏҿӐ
ߡߝߤ


0x20

ЯҾӏҿӐӍӋ ҹӊӍӈҸ ӎҸӋӨӑӄ ӐӖӄӑҸҿӑӐӨ ӌӍӋҿӏ ҿһӍ ӐҸӋӍһӍ ӋӊҸҾӘҿһӍ
ҹҸӅӑҸࡏ

ߟߞߜ
0x23

0x40

ǞȘɾࡁ ࡴ ӒӈҸӃҸӑҿӊӛ ӌҸ ǞȘɾࡈ ӑࡏҿࡏ ҸҾӏҿӐ ӎҸӋӨӑӄࡈ һҾҿ ӊҿӂӄӑ ǞȘɾࡏ

0x43

кҸӋҿӑӛӑҿࡈ ʿ ӎӍӋҿӌӨӊӍ ӐҺӍҿ ӃӌҸӖҿӌӄҿࡈ ӕӍӑӨ ӎӏӨӋӝӕ ӃҸӎӄӐҿӅ Һ ӌҿһӍ
ӌҿ ҹӝӊӍࡏ

int x = 518;
int y = 320;

ߝߞ





щӈҸӃҸӑҿӊӛ ӈҸӈ ҸҾӏҿӐ
ߡߝߤ


0x20
int x = 518;
int y = 320;

ЯҾӏҿӐӍӋ ҹӊӍӈҸ ӎҸӋӨӑӄ ӐӖӄӑҸҿӑӐӨ ӌӍӋҿӏ ҿһӍ ӐҸӋӍһӍ ӋӊҸҾӘҿһӍ
ҹҸӅӑҸࡏ

ߟߞߜ
0x23

0x40

ǞȘɾࡁ ࡴ ӒӈҸӃҸӑҿӊӛ ӌҸ ǞȘɾࡈ ӑࡏҿࡏ ҸҾӏҿӐ ӎҸӋӨӑӄࡈ һҾҿ ӊҿӂӄӑ ǞȘɾࡏ

0x43

кҸӋҿӑӛӑҿࡈ ʿ ӎӍӋҿӌӨӊӍ ӐҺӍҿ ӃӌҸӖҿӌӄҿࡈ ӕӍӑӨ ӎӏӨӋӝӕ ӃҸӎӄӐҿӅ Һ ӌҿһӍ
ӌҿ ҹӝӊӍࡏ

int* ptr;

ߝߞ





щӈҸӃҸӑҿӊӛ ӈҸӈ ҸҾӏҿӐ
ߡߝߤ


0x20
int x = 518;
int y = 320;

ЯҾӏҿӐӍӋ ҹӊӍӈҸ ӎҸӋӨӑӄ ӐӖӄӑҸҿӑӐӨ ӌӍӋҿӏ ҿһӍ ӐҸӋӍһӍ ӋӊҸҾӘҿһӍ
ҹҸӅӑҸࡏ

ߟߞߜ
0x23

0x40

ǞȘɾࡁ ࡴ ӒӈҸӃҸӑҿӊӛ ӌҸ ǞȘɾࡈ ӑࡏҿࡏ ҸҾӏҿӐ ӎҸӋӨӑӄࡈ һҾҿ ӊҿӂӄӑ ǞȘɾࡏ

0x43

кҸӋҿӑӛӑҿࡈ ʿ ӎӍӋҿӌӨӊӍ ӐҺӍҿ ӃӌҸӖҿӌӄҿࡈ ӕӍӑӨ ӎӏӨӋӝӕ ӃҸӎӄӐҿӅ Һ ӌҿһӍ
ӌҿ ҹӝӊӍࡏ

int* ptr;
// & - взятие адреса
// * - разыменование указателя
ptr = &x;
printf("%p %d\n", ptr, *ptr); // 0x20 518
ptr = &y;
printf("%p %d\n", ptr, *ptr); // 0x40 320

ߝߞ





щӈҸӃҸӑҿӊӛ ӈҸӈ ҸҾӏҿӐ
ߡߝߤ


0x20
int x = 518;
int y = 320;

ЯҾӏҿӐӍӋ ҹӊӍӈҸ ӎҸӋӨӑӄ ӐӖӄӑҸҿӑӐӨ ӌӍӋҿӏ ҿһӍ ӐҸӋӍһӍ ӋӊҸҾӘҿһӍ
ҹҸӅӑҸࡏ

ߟߞߜ
0x23

0x40

ǞȘɾࡁ ࡴ ӒӈҸӃҸӑҿӊӛ ӌҸ ǞȘɾࡈ ӑࡏҿࡏ ҸҾӏҿӐ ӎҸӋӨӑӄࡈ һҾҿ ӊҿӂӄӑ ǞȘɾࡏ

0x43

кҸӋҿӑӛӑҿࡈ ʿ ӎӍӋҿӌӨӊӍ ӐҺӍҿ ӃӌҸӖҿӌӄҿࡈ ӕӍӑӨ ӎӏӨӋӝӕ ӃҸӎӄӐҿӅ Һ ӌҿһӍ
ӌҿ ҹӝӊӍࡏ

int* ptr;
// & - взятие адреса
// * - разыменование указателя
ptr = &x;
printf("%p %d\n", ptr, *ptr); // 0x20 518
ptr = &y;
printf("%p %d\n", ptr, *ptr); // 0x40 320
*ptr = 999;

ߝߞ





щӈҸӃҸӑҿӊӛ ӈҸӈ ҸҾӏҿӐ
ߥߥߥ

ߡߝߤ


0x20
int x = 518;
int y = 320;

ЯҾӏҿӐӍӋ ҹӊӍӈҸ ӎҸӋӨӑӄ ӐӖӄӑҸҿӑӐӨ ӌӍӋҿӏ ҿһӍ ӐҸӋӍһӍ ӋӊҸҾӘҿһӍ
ҹҸӅӑҸࡏ

0x23

0x40

ǞȘɾࡁ ࡴ ӒӈҸӃҸӑҿӊӛ ӌҸ ǞȘɾࡈ ӑࡏҿࡏ ҸҾӏҿӐ ӎҸӋӨӑӄࡈ һҾҿ ӊҿӂӄӑ ǞȘɾࡏ

0x43

кҸӋҿӑӛӑҿࡈ ʿ ӎӍӋҿӌӨӊӍ ӐҺӍҿ ӃӌҸӖҿӌӄҿࡈ ӕӍӑӨ ӎӏӨӋӝӕ ӃҸӎӄӐҿӅ Һ ӌҿһӍ
ӌҿ ҹӝӊӍࡏ

int* ptr;
// & - взятие адреса
// * - разыменование указателя
ptr = &x;
printf("%p %d\n", ptr, *ptr); // 0x20 518
ptr = &y;
printf("%p %d\n", ptr, *ptr); // 0x40 320
*ptr = 999;

ߝߞ





щӈҸӃҸӑҿӊӛ ӈҸӈ ҸҾӏҿӐ
ߥߥߥ

ߡߝߤ


0x20
int x = 518;
int y = 320;

ЯҾӏҿӐӍӋ ҹӊӍӈҸ ӎҸӋӨӑӄ ӐӖӄӑҸҿӑӐӨ ӌӍӋҿӏ ҿһӍ ӐҸӋӍһӍ ӋӊҸҾӘҿһӍ
ҹҸӅӑҸࡏ

0x23

0x40

ǞȘɾࡁ ࡴ ӒӈҸӃҸӑҿӊӛ ӌҸ ǞȘɾࡈ ӑࡏҿࡏ ҸҾӏҿӐ ӎҸӋӨӑӄࡈ һҾҿ ӊҿӂӄӑ ǞȘɾࡏ

0x43

кҸӋҿӑӛӑҿࡈ ʿ ӎӍӋҿӌӨӊӍ ӐҺӍҿ ӃӌҸӖҿӌӄҿࡈ ӕӍӑӨ ӎӏӨӋӝӕ ӃҸӎӄӐҿӅ Һ ӌҿһӍ
ӌҿ ҹӝӊӍࡏ

int* ptr;
// & - взятие адреса
// * - разыменование указателя
ptr = &x;
printf("%p %d\n", ptr, *ptr); // 0x20 518
ptr = &y;
printf("%p %d\n", ptr, *ptr); // 0x40 320
*ptr = 999;
printf("%d %d\n", x, y); // 518 999
ߝߞ





хӏӄӋҿӏࡇ ҺӍӃҺӏҸӑ ҾҺӒӕ ӃӌҸӖҿӌӄӅ ӄӃ ӔӒӌӈӗӄӄ


0x20
void divide(
int a, int b,
int* pq,
int* pr)
{
*pq = a / b;
*pr = a % b;
}

ʾࡈ ʿ ӎҿӏҿҾҸӧӑӐӨ ӎӍ ӃӌҸӖҿӌӄӧࡈ ɛࡈ ɟ ӎҿӏҿҾҸӧӑӐӨ ӎӍ ӒӈҸӃҸӑҿӊӧࡏ

q

r

ࡐࡐࡐ

ࡐࡐࡐ
0x23

0x40

тӍҾӄӔӄӈҸӗӄӨ ŏ ӄ ų Һ ӔӒӌӈӗӄӄ ӌӄӈҸӈ ӌҿ ӄӃӋҿӌӄӑ ӃӌҸӖҿӌӄӨ ʾࡈ ʿࡏ
фҾӌҸӈӍ ӃҸӎӄӐӛ ӎӍ ӒӈҸӃҸӑҿӊӨӋ ɔɛࡈ ɔɟ ӌҿӎӍӐӏҿҾӐӑҺҿӌӌӍ ӋҿӌӨҿӑ
ӃӌҸӖҿӌӄӨ ɛࡈ ɟࡏ

0x43

int x = 123, y = 10;
int q, r;

ߝߟ





хӏӄӋҿӏࡇ ҺӍӃҺӏҸӑ ҾҺӒӕ ӃӌҸӖҿӌӄӅ ӄӃ ӔӒӌӈӗӄӄ


0x20

ʾࡈ ʿ ӎҿӏҿҾҸӧӑӐӨ ӎӍ ӃӌҸӖҿӌӄӧࡈ ɛࡈ ɟ ӎҿӏҿҾҸӧӑӐӨ ӎӍ ӒӈҸӃҸӑҿӊӧࡏ

q

r

ࡐࡐࡐ

ࡐࡐࡐ
0x23

void divide(
int a, int b, // 123, 10
int* pq, // 0x20
int* pr) // 0x40
{
*pq = a / b;
*pr = a % b;
}

0x40

тӍҾӄӔӄӈҸӗӄӨ ŏ ӄ ų Һ ӔӒӌӈӗӄӄ ӌӄӈҸӈ ӌҿ ӄӃӋҿӌӄӑ ӃӌҸӖҿӌӄӨ ʾࡈ ʿࡏ
фҾӌҸӈӍ ӃҸӎӄӐӛ ӎӍ ӒӈҸӃҸӑҿӊӨӋ ɔɛࡈ ɔɟ ӌҿӎӍӐӏҿҾӐӑҺҿӌӌӍ ӋҿӌӨҿӑ
ӃӌҸӖҿӌӄӨ ɛࡈ ɟࡏ

0x43

int x = 123, y = 10;
int q, r;
divide(x, y, &q, &r);

ߝߟ





хӏӄӋҿӏࡇ ҺӍӃҺӏҸӑ ҾҺӒӕ ӃӌҸӖҿӌӄӅ ӄӃ ӔӒӌӈӗӄӄ


0x20

ʾࡈ ʿ ӎҿӏҿҾҸӧӑӐӨ ӎӍ ӃӌҸӖҿӌӄӧࡈ ɛࡈ ɟ ӎҿӏҿҾҸӧӑӐӨ ӎӍ ӒӈҸӃҸӑҿӊӧࡏ

q

r

ࡐࡐࡐ

ࡐࡐࡐ
0x23

void divide(
int a, int b, // 123, 10
int* pq, // 0x20
int* pr) // 0x40
{
*pq = a / b; // *(0x20) = 12
*pr = a % b;
}

0x40

тӍҾӄӔӄӈҸӗӄӨ ŏ ӄ ų Һ ӔӒӌӈӗӄӄ ӌӄӈҸӈ ӌҿ ӄӃӋҿӌӄӑ ӃӌҸӖҿӌӄӨ ʾࡈ ʿࡏ
фҾӌҸӈӍ ӃҸӎӄӐӛ ӎӍ ӒӈҸӃҸӑҿӊӨӋ ɔɛࡈ ɔɟ ӌҿӎӍӐӏҿҾӐӑҺҿӌӌӍ ӋҿӌӨҿӑ
ӃӌҸӖҿӌӄӨ ɛࡈ ɟࡏ

0x43

int x = 123, y = 10;
int q, r;
divide(x, y, &q, &r);

ߝߟ





хӏӄӋҿӏࡇ ҺӍӃҺӏҸӑ ҾҺӒӕ ӃӌҸӖҿӌӄӅ ӄӃ ӔӒӌӈӗӄӄ


0x20

ʾࡈ ʿ ӎҿӏҿҾҸӧӑӐӨ ӎӍ ӃӌҸӖҿӌӄӧࡈ ɛࡈ ɟ ӎҿӏҿҾҸӧӑӐӨ ӎӍ ӒӈҸӃҸӑҿӊӧࡏ

q

r

ߝߞ

ࡐࡐࡐ
0x23

void divide(
int a, int b, // 123, 10
int* pq, // 0x20
int* pr) // 0x40
{
*pq = a / b; // *(0x20) = 12
*pr = a % b;
}

0x40

тӍҾӄӔӄӈҸӗӄӨ ŏ ӄ ų Һ ӔӒӌӈӗӄӄ ӌӄӈҸӈ ӌҿ ӄӃӋҿӌӄӑ ӃӌҸӖҿӌӄӨ ʾࡈ ʿࡏ
фҾӌҸӈӍ ӃҸӎӄӐӛ ӎӍ ӒӈҸӃҸӑҿӊӨӋ ɔɛࡈ ɔɟ ӌҿӎӍӐӏҿҾӐӑҺҿӌӌӍ ӋҿӌӨҿӑ
ӃӌҸӖҿӌӄӨ ɛࡈ ɟࡏ

0x43

int x = 123, y = 10;
int q, r;
divide(x, y, &q, &r);

ߝߟ





хӏӄӋҿӏࡇ ҺӍӃҺӏҸӑ ҾҺӒӕ ӃӌҸӖҿӌӄӅ ӄӃ ӔӒӌӈӗӄӄ


0x20

ʾࡈ ʿ ӎҿӏҿҾҸӧӑӐӨ ӎӍ ӃӌҸӖҿӌӄӧࡈ ɛࡈ ɟ ӎҿӏҿҾҸӧӑӐӨ ӎӍ ӒӈҸӃҸӑҿӊӧࡏ

q

r

ߝߞ

ࡐࡐࡐ
0x23

void divide(
int a, int b, // 123, 10
int* pq, // 0x20
int* pr) // 0x40
{
*pq = a / b; // *(0x20) = 12
*pr = a % b; // *(0x40) = 3
}

0x40

тӍҾӄӔӄӈҸӗӄӨ ŏ ӄ ų Һ ӔӒӌӈӗӄӄ ӌӄӈҸӈ ӌҿ ӄӃӋҿӌӄӑ ӃӌҸӖҿӌӄӨ ʾࡈ ʿࡏ
фҾӌҸӈӍ ӃҸӎӄӐӛ ӎӍ ӒӈҸӃҸӑҿӊӨӋ ɔɛࡈ ɔɟ ӌҿӎӍӐӏҿҾӐӑҺҿӌӌӍ ӋҿӌӨҿӑ
ӃӌҸӖҿӌӄӨ ɛࡈ ɟࡏ

0x43

int x = 123, y = 10;
int q, r;
divide(x, y, &q, &r);

ߝߟ





хӏӄӋҿӏࡇ ҺӍӃҺӏҸӑ ҾҺӒӕ ӃӌҸӖҿӌӄӅ ӄӃ ӔӒӌӈӗӄӄ


0x20

ʾࡈ ʿ ӎҿӏҿҾҸӧӑӐӨ ӎӍ ӃӌҸӖҿӌӄӧࡈ ɛࡈ ɟ ӎҿӏҿҾҸӧӑӐӨ ӎӍ ӒӈҸӃҸӑҿӊӧࡏ

q

r

тӍҾӄӔӄӈҸӗӄӨ ŏ ӄ ų Һ ӔӒӌӈӗӄӄ ӌӄӈҸӈ ӌҿ ӄӃӋҿӌӄӑ ӃӌҸӖҿӌӄӨ ʾࡈ ʿࡏ

ߝߞ

ߟ

фҾӌҸӈӍ ӃҸӎӄӐӛ ӎӍ ӒӈҸӃҸӑҿӊӨӋ ɔɛࡈ ɔɟ ӌҿӎӍӐӏҿҾӐӑҺҿӌӌӍ ӋҿӌӨҿӑ
ӃӌҸӖҿӌӄӨ ɛࡈ ɟࡏ

0x23

void divide(
int a, int b, // 123, 10
int* pq, // 0x20
int* pr) // 0x40
{
*pq = a / b; // *(0x20) = 12
*pr = a % b; // *(0x40) = 3
}

0x40

0x43

int x = 123, y = 10;
int q, r;
divide(x, y, &q, &r);

ߝߟ





хӏӄӋҿӏࡇ ҺӍӃҺӏҸӑ ҾҺӒӕ ӃӌҸӖҿӌӄӅ ӄӃ ӔӒӌӈӗӄӄ


0x20

ʾࡈ ʿ ӎҿӏҿҾҸӧӑӐӨ ӎӍ ӃӌҸӖҿӌӄӧࡈ ɛࡈ ɟ ӎҿӏҿҾҸӧӑӐӨ ӎӍ ӒӈҸӃҸӑҿӊӧࡏ

q

r

тӍҾӄӔӄӈҸӗӄӨ ŏ ӄ ų Һ ӔӒӌӈӗӄӄ ӌӄӈҸӈ ӌҿ ӄӃӋҿӌӄӑ ӃӌҸӖҿӌӄӨ ʾࡈ ʿࡏ

ߝߞ

ߟ

фҾӌҸӈӍ ӃҸӎӄӐӛ ӎӍ ӒӈҸӃҸӑҿӊӨӋ ɔɛࡈ ɔɟ ӌҿӎӍӐӏҿҾӐӑҺҿӌӌӍ ӋҿӌӨҿӑ
ӃӌҸӖҿӌӄӨ ɛࡈ ɟࡏ

0x23

void divide(
int a, int b, // 123, 10
int* pq, // 0x20
int* pr) // 0x40
{
*pq = a / b; // *(0x20) = 12
*pr = a % b; // *(0x40) = 3
}

0x40

0x43

int x = 123, y = 10;
int q, r;
divide(x, y, &q, &r);
printf("%d = %d * %d + %d\n",
x, y, q, r);
// 123 = 10 * 12 + 3
ߝߟ





хӏҿҾӐӑҸҺӊҿӌӄҿ ӒӈҸӃҸӑҿӊӨ

÷ࡁ ҾӊӨ ӊӧҹӍһӍ ÷ ҺӐҿһҾҸ ӃҸӌӄӋҸҿӑ ӍҾӄӌҸӈӍҺӍҿ ӈӍӊӄӖҿӐӑҺӍ ҹҸӅӑ ӌҸ
ӍҾӌӍӅ ӎӊҸӑӔӍӏӋҿࡏ фҹӝӖӌӍ ӑҸӈࡇ ߟߞࡷҹӄӑӌӝҿ ӎӊҸӑӔӍӏӋӝ ࡴ ߠ ҹҸӅӑҸࡈ
ߢߠࡷҹӄӑӌӝҿ ࡴ ߤ ҹҸӅӑࡏ

x


ЯҾӏҿӐ ӕӏҸӌӄӑӐӨ ӈҸӈ ҹҿӃӃӌҸӈӍҺӍҿ ӖӄӐӊӍࡏ

ߟߣ

чҿӅӖҸӐ ӌҿ ҹӒҾҿӋ ӎӍҾӏӍҹӌӍ ӍӐӑҸӌҸҺӊӄҺҸӑӛӐӨ ӌҸ ҾҺӍӅӌӝӕ
ӒӈҸӃҸӑҿӊӨӕࡏ

0x20

char x = 37;

ߝߠ





хӏҿҾӐӑҸҺӊҿӌӄҿ ӒӈҸӃҸӑҿӊӨ

÷ࡁ ҾӊӨ ӊӧҹӍһӍ ÷ ҺӐҿһҾҸ ӃҸӌӄӋҸҿӑ ӍҾӄӌҸӈӍҺӍҿ ӈӍӊӄӖҿӐӑҺӍ ҹҸӅӑ ӌҸ
ӍҾӌӍӅ ӎӊҸӑӔӍӏӋҿࡏ фҹӝӖӌӍ ӑҸӈࡇ ߟߞࡷҹӄӑӌӝҿ ӎӊҸӑӔӍӏӋӝ ࡴ ߠ ҹҸӅӑҸࡈ
ߢߠࡷҹӄӑӌӝҿ ࡴ ߤ ҹҸӅӑࡏ

px

x


ЯҾӏҿӐ ӕӏҸӌӄӑӐӨ ӈҸӈ ҹҿӃӃӌҸӈӍҺӍҿ ӖӄӐӊӍࡏ

ߟߣ
0x20

0x80

чҿӅӖҸӐ ӌҿ ҹӒҾҿӋ ӎӍҾӏӍҹӌӍ ӍӐӑҸӌҸҺӊӄҺҸӑӛӐӨ ӌҸ ҾҺӍӅӌӝӕ
ӒӈҸӃҸӑҿӊӨӕࡏ

0x83

char x = 37;
char* px = &x;

ߝߠ





хӏҿҾӐӑҸҺӊҿӌӄҿ ӒӈҸӃҸӑҿӊӨ



÷ࡁ ҾӊӨ ӊӧҹӍһӍ ÷ ҺӐҿһҾҸ ӃҸӌӄӋҸҿӑ ӍҾӄӌҸӈӍҺӍҿ ӈӍӊӄӖҿӐӑҺӍ ҹҸӅӑ ӌҸ
ӍҾӌӍӅ ӎӊҸӑӔӍӏӋҿࡏ фҹӝӖӌӍ ӑҸӈࡇ ߟߞࡷҹӄӑӌӝҿ ӎӊҸӑӔӍӏӋӝ ࡴ ߠ ҹҸӅӑҸࡈ
ߢߠࡷҹӄӑӌӝҿ ࡴ ߤ ҹҸӅӑࡏ

x

px

ߟߣ

0x20

0x20

0x80

ЯҾӏҿӐ ӕӏҸӌӄӑӐӨ ӈҸӈ ҹҿӃӃӌҸӈӍҺӍҿ ӖӄӐӊӍࡏ
чҿӅӖҸӐ ӌҿ ҹӒҾҿӋ ӎӍҾӏӍҹӌӍ ӍӐӑҸӌҸҺӊӄҺҸӑӛӐӨ ӌҸ ҾҺӍӅӌӝӕ
ӒӈҸӃҸӑҿӊӨӕࡏ

0x83

char x = 37;
char* px = &x;

ߝߠ





хӏҿҾӐӑҸҺӊҿӌӄҿ ӒӈҸӃҸӑҿӊӨ



÷ࡁ ҾӊӨ ӊӧҹӍһӍ ÷ ҺӐҿһҾҸ ӃҸӌӄӋҸҿӑ ӍҾӄӌҸӈӍҺӍҿ ӈӍӊӄӖҿӐӑҺӍ ҹҸӅӑ ӌҸ
ӍҾӌӍӅ ӎӊҸӑӔӍӏӋҿࡏ фҹӝӖӌӍ ӑҸӈࡇ ߟߞࡷҹӄӑӌӝҿ ӎӊҸӑӔӍӏӋӝ ࡴ ߠ ҹҸӅӑҸࡈ
ߢߠࡷҹӄӑӌӝҿ ࡴ ߤ ҹҸӅӑࡏ

x

px

ߟߣ

0x20

0x20

0x80

ppx
0x83

0xE0

ЯҾӏҿӐ ӕӏҸӌӄӑӐӨ ӈҸӈ ҹҿӃӃӌҸӈӍҺӍҿ ӖӄӐӊӍࡏ
чҿӅӖҸӐ ӌҿ ҹӒҾҿӋ ӎӍҾӏӍҹӌӍ ӍӐӑҸӌҸҺӊӄҺҸӑӛӐӨ ӌҸ ҾҺӍӅӌӝӕ
ӒӈҸӃҸӑҿӊӨӕࡏ

0xE4

char x = 37;
char* px = &x;
char** ppx = &px;

ߝߠ





хӏҿҾӐӑҸҺӊҿӌӄҿ ӒӈҸӃҸӑҿӊӨ



÷ࡁ ҾӊӨ ӊӧҹӍһӍ ÷ ҺӐҿһҾҸ ӃҸӌӄӋҸҿӑ ӍҾӄӌҸӈӍҺӍҿ ӈӍӊӄӖҿӐӑҺӍ ҹҸӅӑ ӌҸ
ӍҾӌӍӅ ӎӊҸӑӔӍӏӋҿࡏ фҹӝӖӌӍ ӑҸӈࡇ ߟߞࡷҹӄӑӌӝҿ ӎӊҸӑӔӍӏӋӝ ࡴ ߠ ҹҸӅӑҸࡈ
ߢߠࡷҹӄӑӌӝҿ ࡴ ߤ ҹҸӅӑࡏ

x

px

ppx

ߟߣ

0x20

0x80

0x20

0x80

0x83

0xE0

ЯҾӏҿӐ ӕӏҸӌӄӑӐӨ ӈҸӈ ҹҿӃӃӌҸӈӍҺӍҿ ӖӄӐӊӍࡏ
чҿӅӖҸӐ ӌҿ ҹӒҾҿӋ ӎӍҾӏӍҹӌӍ ӍӐӑҸӌҸҺӊӄҺҸӑӛӐӨ ӌҸ ҾҺӍӅӌӝӕ
ӒӈҸӃҸӑҿӊӨӕࡏ

0xE4

char x = 37;
char* px = &x;
char** ppx = &px;

ߝߠ





хӏҿҾӐӑҸҺӊҿӌӄҿ ӒӈҸӃҸӑҿӊӨ



÷ࡁ ҾӊӨ ӊӧҹӍһӍ ÷ ҺӐҿһҾҸ ӃҸӌӄӋҸҿӑ ӍҾӄӌҸӈӍҺӍҿ ӈӍӊӄӖҿӐӑҺӍ ҹҸӅӑ ӌҸ
ӍҾӌӍӅ ӎӊҸӑӔӍӏӋҿࡏ фҹӝӖӌӍ ӑҸӈࡇ ߟߞࡷҹӄӑӌӝҿ ӎӊҸӑӔӍӏӋӝ ࡴ ߠ ҹҸӅӑҸࡈ
ߢߠࡷҹӄӑӌӝҿ ࡴ ߤ ҹҸӅӑࡏ

x

px

ppx

ߟߣ

0x20

0x80

0x20

0x80

0x83

0xE0

ЯҾӏҿӐ ӕӏҸӌӄӑӐӨ ӈҸӈ ҹҿӃӃӌҸӈӍҺӍҿ ӖӄӐӊӍࡏ
чҿӅӖҸӐ ӌҿ ҹӒҾҿӋ ӎӍҾӏӍҹӌӍ ӍӐӑҸӌҸҺӊӄҺҸӑӛӐӨ ӌҸ ҾҺӍӅӌӝӕ
ӒӈҸӃҸӑҿӊӨӕࡏ

0xE4

char x = 37;
char* px = &x;
char** ppx = &px;
**ppx = 42;

ߝߠ





хӏҿҾӐӑҸҺӊҿӌӄҿ ӒӈҸӃҸӑҿӊӨ



÷ࡁ ҾӊӨ ӊӧҹӍһӍ ÷ ҺӐҿһҾҸ ӃҸӌӄӋҸҿӑ ӍҾӄӌҸӈӍҺӍҿ ӈӍӊӄӖҿӐӑҺӍ ҹҸӅӑ ӌҸ
ӍҾӌӍӅ ӎӊҸӑӔӍӏӋҿࡏ фҹӝӖӌӍ ӑҸӈࡇ ߟߞࡷҹӄӑӌӝҿ ӎӊҸӑӔӍӏӋӝ ࡴ ߠ ҹҸӅӑҸࡈ
ߢߠࡷҹӄӑӌӝҿ ࡴ ߤ ҹҸӅӑࡏ

x

px

ppx

ߠߞ

0x20

0x80

0x20

0x80

0x83

0xE0

ЯҾӏҿӐ ӕӏҸӌӄӑӐӨ ӈҸӈ ҹҿӃӃӌҸӈӍҺӍҿ ӖӄӐӊӍࡏ
чҿӅӖҸӐ ӌҿ ҹӒҾҿӋ ӎӍҾӏӍҹӌӍ ӍӐӑҸӌҸҺӊӄҺҸӑӛӐӨ ӌҸ ҾҺӍӅӌӝӕ
ӒӈҸӃҸӑҿӊӨӕࡏ

0xE4

char x = 37;
char* px = &x;
char** ppx = &px;
**ppx = 42;

ߝߠ





хӏҿҾӐӑҸҺӊҿӌӄҿ ӋҸӐӐӄҺҸ



тҸӐӐӄҺ ӕӏҸӌӄӑӐӨ ӈҸӈ ӌҿӎӏҿӏӝҺӌӝӅ ҹӊӍӈ ӎҸӋӨӑӄࡈ ӢӊҿӋҿӌӑӝ
ӏҸӐӎӍӊҸһҸӧӑӐӨ ӎӍӐӊҿҾӍҺҸӑҿӊӛӌӍࡏ

a[0]

a[1]

a[2]

ߝߜ

ߞߜ

ߟߜ

0x20

0x22

хӏӄ ӎӏӄҹҸҺӊҿӌӄӄ ӃӌҸӖҿӌӄӨ Ș ӈ ӒӈҸӃҸӑҿӊӧࡈ ҿһӍ ӃӌҸӖҿӌӄҿ
ӒҺҿӊӄӖӄҺҸҿӑӐӨ ӌҸ ࡫Ș ࡁ ӏҸӃӋҿӏ ӑӄӎҸ ҾҸӌӌӝӕ ӎӍҾ ӒӈҸӃҸӑҿӊҿӋ࡬ࡏ
тҸӐӐӄҺӌҸӨ ӎҿӏҿӋҿӌӌҸӨ ࡴ ҺӐҿһӍ ӊӄӘӛ ӒӈҸӃҸӑҿӊӛ ӌҸ ӌҸӖҸӊӍ
ӋҸӐӐӄҺҸࡏ

0x24

short a[3] = {10, 20, 30};

ߝߡ





хӏҿҾӐӑҸҺӊҿӌӄҿ ӋҸӐӐӄҺҸ



тҸӐӐӄҺ ӕӏҸӌӄӑӐӨ ӈҸӈ ӌҿӎӏҿӏӝҺӌӝӅ ҹӊӍӈ ӎҸӋӨӑӄࡈ ӢӊҿӋҿӌӑӝ
ӏҸӐӎӍӊҸһҸӧӑӐӨ ӎӍӐӊҿҾӍҺҸӑҿӊӛӌӍࡏ

a[0]

a[1]

a[2]

ߝߜ

ߞߜ

ߟߜ

0x20

0x22

хӏӄ ӎӏӄҹҸҺӊҿӌӄӄ ӃӌҸӖҿӌӄӨ Ș ӈ ӒӈҸӃҸӑҿӊӧࡈ ҿһӍ ӃӌҸӖҿӌӄҿ
ӒҺҿӊӄӖӄҺҸҿӑӐӨ ӌҸ ࡫Ș ࡁ ӏҸӃӋҿӏ ӑӄӎҸ ҾҸӌӌӝӕ ӎӍҾ ӒӈҸӃҸӑҿӊҿӋ࡬ࡏ
тҸӐӐӄҺӌҸӨ ӎҿӏҿӋҿӌӌҸӨ ࡴ ҺӐҿһӍ ӊӄӘӛ ӒӈҸӃҸӑҿӊӛ ӌҸ ӌҸӖҸӊӍ
ӋҸӐӐӄҺҸࡏ

0x24

short a[3] = {10, 20, 30};
short* p = &(a[0]); // 0x20
short* p1 = p + 1; // 0x22
short* p2 = p + 2; // 0x24

ߝߡ





хӏҿҾӐӑҸҺӊҿӌӄҿ ӋҸӐӐӄҺҸ



тҸӐӐӄҺ ӕӏҸӌӄӑӐӨ ӈҸӈ ӌҿӎӏҿӏӝҺӌӝӅ ҹӊӍӈ ӎҸӋӨӑӄࡈ ӢӊҿӋҿӌӑӝ
ӏҸӐӎӍӊҸһҸӧӑӐӨ ӎӍӐӊҿҾӍҺҸӑҿӊӛӌӍࡏ

a[0]

a[1]

a[2]

ߝߜ

ߞߜ

ߟߜ

0x20
short a[3] = {10, 20, 30};

0x22

хӏӄ ӎӏӄҹҸҺӊҿӌӄӄ ӃӌҸӖҿӌӄӨ Ș ӈ ӒӈҸӃҸӑҿӊӧࡈ ҿһӍ ӃӌҸӖҿӌӄҿ
ӒҺҿӊӄӖӄҺҸҿӑӐӨ ӌҸ ࡫Ș ࡁ ӏҸӃӋҿӏ ӑӄӎҸ ҾҸӌӌӝӕ ӎӍҾ ӒӈҸӃҸӑҿӊҿӋ࡬ࡏ
тҸӐӐӄҺӌҸӨ ӎҿӏҿӋҿӌӌҸӨ ࡴ ҺӐҿһӍ ӊӄӘӛ ӒӈҸӃҸӑҿӊӛ ӌҸ ӌҸӖҸӊӍ
ӋҸӐӐӄҺҸࡏ

0x24
*p1 = 200;

short* p = &(a[0]); // 0x20
short* p1 = p + 1; // 0x22
short* p2 = p + 2; // 0x24

ߝߡ





хӏҿҾӐӑҸҺӊҿӌӄҿ ӋҸӐӐӄҺҸ



тҸӐӐӄҺ ӕӏҸӌӄӑӐӨ ӈҸӈ ӌҿӎӏҿӏӝҺӌӝӅ ҹӊӍӈ ӎҸӋӨӑӄࡈ ӢӊҿӋҿӌӑӝ
ӏҸӐӎӍӊҸһҸӧӑӐӨ ӎӍӐӊҿҾӍҺҸӑҿӊӛӌӍࡏ

a[0]

a[1]

a[2]

ߝߜ

ߞߜߜ

ߟߜ

0x20
short a[3] = {10, 20, 30};

0x22

хӏӄ ӎӏӄҹҸҺӊҿӌӄӄ ӃӌҸӖҿӌӄӨ Ș ӈ ӒӈҸӃҸӑҿӊӧࡈ ҿһӍ ӃӌҸӖҿӌӄҿ
ӒҺҿӊӄӖӄҺҸҿӑӐӨ ӌҸ ࡫Ș ࡁ ӏҸӃӋҿӏ ӑӄӎҸ ҾҸӌӌӝӕ ӎӍҾ ӒӈҸӃҸӑҿӊҿӋ࡬ࡏ
тҸӐӐӄҺӌҸӨ ӎҿӏҿӋҿӌӌҸӨ ࡴ ҺӐҿһӍ ӊӄӘӛ ӒӈҸӃҸӑҿӊӛ ӌҸ ӌҸӖҸӊӍ
ӋҸӐӐӄҺҸࡏ

0x24
*p1 = 200;

short* p = &(a[0]); // 0x20
short* p1 = p + 1; // 0x22
short* p2 = p + 2; // 0x24

ߝߡ





хӏҿҾӐӑҸҺӊҿӌӄҿ ӋҸӐӐӄҺҸ



тҸӐӐӄҺ ӕӏҸӌӄӑӐӨ ӈҸӈ ӌҿӎӏҿӏӝҺӌӝӅ ҹӊӍӈ ӎҸӋӨӑӄࡈ ӢӊҿӋҿӌӑӝ
ӏҸӐӎӍӊҸһҸӧӑӐӨ ӎӍӐӊҿҾӍҺҸӑҿӊӛӌӍࡏ

a[0]

a[1]

a[2]

ߝߜ

ߞߜߜ

ߟߜ

0x20
short a[3] = {10, 20, 30};

0x22

хӏӄ ӎӏӄҹҸҺӊҿӌӄӄ ӃӌҸӖҿӌӄӨ Ș ӈ ӒӈҸӃҸӑҿӊӧࡈ ҿһӍ ӃӌҸӖҿӌӄҿ
ӒҺҿӊӄӖӄҺҸҿӑӐӨ ӌҸ ࡫Ș ࡁ ӏҸӃӋҿӏ ӑӄӎҸ ҾҸӌӌӝӕ ӎӍҾ ӒӈҸӃҸӑҿӊҿӋ࡬ࡏ
тҸӐӐӄҺӌҸӨ ӎҿӏҿӋҿӌӌҸӨ ࡴ ҺӐҿһӍ ӊӄӘӛ ӒӈҸӃҸӑҿӊӛ ӌҸ ӌҸӖҸӊӍ
ӋҸӐӐӄҺҸࡏ

0x24
*p1 = 200;
*p2 = *p1 + *p2;

short* p = &(a[0]); // 0x20
short* p1 = p + 1; // 0x22
short* p2 = p + 2; // 0x24

ߝߡ





хӏҿҾӐӑҸҺӊҿӌӄҿ ӋҸӐӐӄҺҸ



тҸӐӐӄҺ ӕӏҸӌӄӑӐӨ ӈҸӈ ӌҿӎӏҿӏӝҺӌӝӅ ҹӊӍӈ ӎҸӋӨӑӄࡈ ӢӊҿӋҿӌӑӝ
ӏҸӐӎӍӊҸһҸӧӑӐӨ ӎӍӐӊҿҾӍҺҸӑҿӊӛӌӍࡏ

a[0]

a[1]

a[2]

ߝߜ

ߞߜߜ

ߞߟߜ

0x20
short a[3] = {10, 20, 30};

0x22

хӏӄ ӎӏӄҹҸҺӊҿӌӄӄ ӃӌҸӖҿӌӄӨ Ș ӈ ӒӈҸӃҸӑҿӊӧࡈ ҿһӍ ӃӌҸӖҿӌӄҿ
ӒҺҿӊӄӖӄҺҸҿӑӐӨ ӌҸ ࡫Ș ࡁ ӏҸӃӋҿӏ ӑӄӎҸ ҾҸӌӌӝӕ ӎӍҾ ӒӈҸӃҸӑҿӊҿӋ࡬ࡏ
тҸӐӐӄҺӌҸӨ ӎҿӏҿӋҿӌӌҸӨ ࡴ ҺӐҿһӍ ӊӄӘӛ ӒӈҸӃҸӑҿӊӛ ӌҸ ӌҸӖҸӊӍ
ӋҸӐӐӄҺҸࡏ

0x24
*p1 = 200;
*p2 = *p1 + *p2;

short* p = &(a[0]); // 0x20
short* p1 = p + 1; // 0x22
short* p2 = p + 2; // 0x24

ߝߡ





хӏҿҾӐӑҸҺӊҿӌӄҿ ӋҸӐӐӄҺҸ



тҸӐӐӄҺ ӕӏҸӌӄӑӐӨ ӈҸӈ ӌҿӎӏҿӏӝҺӌӝӅ ҹӊӍӈ ӎҸӋӨӑӄࡈ ӢӊҿӋҿӌӑӝ
ӏҸӐӎӍӊҸһҸӧӑӐӨ ӎӍӐӊҿҾӍҺҸӑҿӊӛӌӍࡏ

a[0]

a[1]

a[2]

ߝߜ

ߞߜߜ

ߞߟߜ

0x20
short a[3] = {10, 20, 30};

0x22

хӏӄ ӎӏӄҹҸҺӊҿӌӄӄ ӃӌҸӖҿӌӄӨ Ș ӈ ӒӈҸӃҸӑҿӊӧࡈ ҿһӍ ӃӌҸӖҿӌӄҿ
ӒҺҿӊӄӖӄҺҸҿӑӐӨ ӌҸ ࡫Ș ࡁ ӏҸӃӋҿӏ ӑӄӎҸ ҾҸӌӌӝӕ ӎӍҾ ӒӈҸӃҸӑҿӊҿӋ࡬ࡏ
тҸӐӐӄҺӌҸӨ ӎҿӏҿӋҿӌӌҸӨ ࡴ ҺӐҿһӍ ӊӄӘӛ ӒӈҸӃҸӑҿӊӛ ӌҸ ӌҸӖҸӊӍ
ӋҸӐӐӄҺҸࡏ

0x24
*(p+1) = 200;
*(p+2) = *(p+1) + *(p+2);

short* p = &(a[0]); // 0x20
short* p1 = p + 1; // 0x22
short* p2 = p + 2; // 0x24

ߝߡ





хӏҿҾӐӑҸҺӊҿӌӄҿ ӋҸӐӐӄҺҸ



тҸӐӐӄҺ ӕӏҸӌӄӑӐӨ ӈҸӈ ӌҿӎӏҿӏӝҺӌӝӅ ҹӊӍӈ ӎҸӋӨӑӄࡈ ӢӊҿӋҿӌӑӝ
ӏҸӐӎӍӊҸһҸӧӑӐӨ ӎӍӐӊҿҾӍҺҸӑҿӊӛӌӍࡏ

a[0]

a[1]

a[2]

ߝߜ

ߞߜߜ

ߞߟߜ

0x20
short a[3] = {10, 20, 30};
short* p = &(a[0]); // 0x20
short* p1 = p + 1; // 0x22
short* p2 = p + 2; // 0x24

0x22

хӏӄ ӎӏӄҹҸҺӊҿӌӄӄ ӃӌҸӖҿӌӄӨ Ș ӈ ӒӈҸӃҸӑҿӊӧࡈ ҿһӍ ӃӌҸӖҿӌӄҿ
ӒҺҿӊӄӖӄҺҸҿӑӐӨ ӌҸ ࡫Ș ࡁ ӏҸӃӋҿӏ ӑӄӎҸ ҾҸӌӌӝӕ ӎӍҾ ӒӈҸӃҸӑҿӊҿӋ࡬ࡏ
тҸӐӐӄҺӌҸӨ ӎҿӏҿӋҿӌӌҸӨ ࡴ ҺӐҿһӍ ӊӄӘӛ ӒӈҸӃҸӑҿӊӛ ӌҸ ӌҸӖҸӊӍ
ӋҸӐӐӄҺҸࡏ

0x24
*(p+1) = 200;
*(p+2) = *(p+1) + *(p+2);
// *(x + y) == x[y]

ߝߡ





хӏҿҾӐӑҸҺӊҿӌӄҿ ӋҸӐӐӄҺҸ



тҸӐӐӄҺ ӕӏҸӌӄӑӐӨ ӈҸӈ ӌҿӎӏҿӏӝҺӌӝӅ ҹӊӍӈ ӎҸӋӨӑӄࡈ ӢӊҿӋҿӌӑӝ
ӏҸӐӎӍӊҸһҸӧӑӐӨ ӎӍӐӊҿҾӍҺҸӑҿӊӛӌӍࡏ

a[0]

a[1]

a[2]

ߝߜ

ߞߜߜ

ߞߟߜ

0x20
short a[3] = {10, 20, 30};
short* p = &(a[0]); // 0x20
short* p1 = p + 1; // 0x22
short* p2 = p + 2; // 0x24

0x22

хӏӄ ӎӏӄҹҸҺӊҿӌӄӄ ӃӌҸӖҿӌӄӨ Ș ӈ ӒӈҸӃҸӑҿӊӧࡈ ҿһӍ ӃӌҸӖҿӌӄҿ
ӒҺҿӊӄӖӄҺҸҿӑӐӨ ӌҸ ࡫Ș ࡁ ӏҸӃӋҿӏ ӑӄӎҸ ҾҸӌӌӝӕ ӎӍҾ ӒӈҸӃҸӑҿӊҿӋ࡬ࡏ
тҸӐӐӄҺӌҸӨ ӎҿӏҿӋҿӌӌҸӨ ࡴ ҺӐҿһӍ ӊӄӘӛ ӒӈҸӃҸӑҿӊӛ ӌҸ ӌҸӖҸӊӍ
ӋҸӐӐӄҺҸࡏ

0x24
p[1] = 200;
p[2] = p[1] + p[2];
// *(x + y) == x[y]

ߝߡ





хӏҿҾӐӑҸҺӊҿӌӄҿ ӋҸӐӐӄҺҸ



тҸӐӐӄҺ ӕӏҸӌӄӑӐӨ ӈҸӈ ӌҿӎӏҿӏӝҺӌӝӅ ҹӊӍӈ ӎҸӋӨӑӄࡈ ӢӊҿӋҿӌӑӝ
ӏҸӐӎӍӊҸһҸӧӑӐӨ ӎӍӐӊҿҾӍҺҸӑҿӊӛӌӍࡏ

a[0]

a[1]

a[2]

ߝߜ

ߞߜߜ

ߞߟߜ

0x20
short a[3] = {10, 20, 30};
short* p = a; // 0x20
short* p1 = p + 1; // 0x22
short* p2 = p + 2; // 0x24

0x22

хӏӄ ӎӏӄҹҸҺӊҿӌӄӄ ӃӌҸӖҿӌӄӨ Ș ӈ ӒӈҸӃҸӑҿӊӧࡈ ҿһӍ ӃӌҸӖҿӌӄҿ
ӒҺҿӊӄӖӄҺҸҿӑӐӨ ӌҸ ࡫Ș ࡁ ӏҸӃӋҿӏ ӑӄӎҸ ҾҸӌӌӝӕ ӎӍҾ ӒӈҸӃҸӑҿӊҿӋ࡬ࡏ
тҸӐӐӄҺӌҸӨ ӎҿӏҿӋҿӌӌҸӨ ࡴ ҺӐҿһӍ ӊӄӘӛ ӒӈҸӃҸӑҿӊӛ ӌҸ ӌҸӖҸӊӍ
ӋҸӐӐӄҺҸࡏ

0x24
p[1] = 200;
p[2] = p[1] + p[2];
// *(x + y) == x[y]

ߝߡ





хӏҿҾӐӑҸҺӊҿӌӄҿ ҾҺӒӋҿӏӌӍһӍ ӋҸӐӐӄҺҸ

тҸӐӐӄҺ ӕӏҸӌӄӑӐӨ ӈҸӈ ӌҿӎӏҿӏӝҺӌӝӅ ҹӊӍӈ ӎҸӋӨӑӄࡈ ӢӊҿӋҿӌӑӝ
ӏҸӐӎӍӊҸһҸӧӑӐӨ ӎӍӐӊҿҾӍҺҸӑҿӊӛӌӍࡇ ӐӑӏӍӈҸ ӃҸ ӐӑӏӍӈӍӅࡏ
ߟ ࡴ ӢӑӍ ӘӄӏӄӌҸ ӍҾӌӍӅ ӐӑӏӍӈӄࡏ

a[0][0] a[0][1] a[0][2] a[1][0] a[1][1] a[1][2]



ߝߜ
0x20

ߞߜ
0x22

ߟߜ
0x24

ߠߜ
0x26

ߡߜ
0x28

ߢߜ
0x2A

short a[2][3] = {
{10, 20, 30},
{40, 50, 60}};

short* p = a;

a[i][j] == *(a + i*3 + j);

a[i][j] == p[i*3 + j];

ߝߢ





бӄҾӝ ӎҸӋӨӑӄ






чӑҸӑӄӖҿӐӈҸӨ ӎҸӋӨӑӛ

п ӐӑҸӑӄӖҿӐӈӍӅ ӎҸӋӨӑӄ ӑҸӈӂҿ ӍӑӌӍӐӨӑӐӨ ɯɾŏɾǞżࡷӎҿӏҿӋҿӌӌӝҿࡏ

хӏӄӋҿӏӝࡇ
ࡄ һӊӍҹҸӊӛӌӝҿ ӎҿӏҿӋҿӌӌӝҿࡇ char game_field[100][100]ࡈ
ࡄ ӐӑӏӍӈӍҺӝҿ ӊӄӑҿӏҸӊӝࡇ "Hello world!"ࡈ
ࡄ ࡏࡏࡏ
чҺӍӅӐӑҺҸࡇ
ࡄ ҺӝҾҿӊӨҿӑӐӨ ҺӍ ҺӏҿӋӨ ӐӑҸӏӑҸ ӎӏӍһӏҸӋӋӝࡈ
ࡄ ӍӐҺӍҹӍӂҾҸҿӑӐӨ ҺӍ ҺӏҿӋӨ ӃҸҺҿӏӘҿӌӄӨ ӎӏӍһӏҸӋӋӝࡈ
ࡄ ӄӌӄӗӄҸӊӄӃӄӏӒҿӑӐӨ ӌӒӊҿҺӝӋӄ ӃӌҸӖҿӌӄӨӋӄࡈ
ࡄ ӔӄӈӐӄӏӍҺҸӌӌӝӅ ӏҸӃӋҿӏࡏ
ߝߤ





ЯҺӑӍӋҸӑӄӖҿӐӈҸӨ ӎҸӋӨӑӛ
хӏӄӋҿӏӝࡇ
ࡄ ӊӍӈҸӊӛӌӝҿ ӎҿӏҿӋҿӌӌӝҿࡇ {...; int tmp; ...}ࡈ
ࡄ ҸӏһӒӋҿӌӑӝ ӔӒӌӈӗӄӅࡇ (..., double speed, ...)ࡈ
чҺӍӅӐӑҺҸࡇ
ࡄ ҺӝҾҿӊӨҿӑӐӨ ӎӏӄ ҺӕӍҾҿ Һ ӍҹӜҿӋӊӧәӄӅ ҹӊӍӈࡈ
ࡄ ӍӐҺӍҹӍӂҾҸҿӑӐӨ ӎӏӄ ҺӝӕӍҾҿ ӄӃ ӍҹӜҿӋӊӧәҿһӍ ҹӊӍӈҸࡈ
ࡄ ӄӃӌҸӖҸӊӛӌӍ ӌҿ ӄӌӄӗӄҸӊӄӃӄӏӍҺҸӌҸࡈ
ࡄ ӔӄӈӐӄӏӍҺҸӌӌӝӅ ӏҸӃӋҿӏࡏ
ߝߥ





еӄӌҸӋӄӖҿӐӈҸӨ ӎҸӋӨӑӛ

уҿҾӍӐӑҸӑӈӄ ҸҺӑӍӋҸӑӄӖҿӐӈӍӅ ӄ ӐӑҸӑӄӖҿӐӈӍӅࡇ ӔӄӈӐӄӏӍҺҸӌӌӝӅ
ӏҸӃӋҿӏ ӄ ӌҿҾӍӐӑҸӑӍӖӌӍ һӄҹӈӍҿ ҺӏҿӋӨ ӂӄӃӌӄࡏ
чҸӋӝӅ һӄҹӈӄӅ ӄ ӐҸӋӝӅ ӐӊӍӂӌӝӅ Һ ӏҸҹӍӑҿ ҺӄҾ ӎҸӋӨӑӄࡏ

чҺӍӅӐӑҺҸࡇ
ࡄ ҺӝҾҿӊӨҿӑӐӨ ӄ ӍӐҺӍҹӍӂҾҸҿӑӐӨ ӎӍ ӃҸӎӏӍӐӒ ӎӏӍһӏҸӋӋӝࡈ
ࡄ ӄӃӌҸӖҸӊӛӌӍ ӌҿ ӄӌӄӗӄҸӊӄӃӄӏӍҺҸӌҸࡈ
ࡄ ӏҸӃӋҿӏ ӃҸҾҸҿӑӐӨ ҾӄӌҸӋӄӖҿӐӈӄࡏ

ߞߜ





еӄӌҸӋӄӖҿӐӈҸӨ ӎҸӋӨӑӛ






аӄҹӊӄӍӑҿӖӌӝҿ ӔӒӌӈӗӄӄ

уҸ ӐӊҸӅҾҸӕ Ӌӝ ҹӒҾҿӋ ӍӎӒӐӈҸӑӛ ࢆࡍǞȘżȀʕƇƚ ࣔɯɾƇȀǞųࡏǕ࣏ࢆࡏ

еӊӨ ӏҸҹӍӑӝ Ӑ ҾӄӌҸӋӄӖҿӐӈӍӅ ӎҸӋӨӑӛӧ ӄӐӎӍӊӛӃӒҿӑӐӨ ҹӄҹӊӄӍӑҿӈҸ stdlib.hࡇ
ࡄ NULLࡈ
ࡄ mallocࡈ
ࡄ callocࡈ
ࡄ reallocࡈ
ࡄ freeࡏ

ߞߞ





щӈҸӃҸӑҿӊӛࡈ ӌӄӈӒҾҸ ӌҿ ӒӈҸӃӝҺҸӧәӄӅ

эӄӐӊҿӌӌӍҿ ӃӌҸӖҿӌӄҿ ŸĆ ࣌࣌ ߜࡏ

NULL ࡴ ӐӎҿӗӄҸӊӛӌӍҿ ӃӌҸӖҿӌӄҿࡈ ӐӄӋҺӍӊӄӃӄӏӒӧәҿҿࡈ ӖӑӍ ӒӈҸӃҸӑҿӊӛ ӌҿ
ӒӈҸӃӝҺҸҿӑ ӌӄ ӌҸ ӈҸӈӍӅ ӢӊҿӋҿӌӑ ӎҸӋӨӑӄࡏ
int* ptr = NULL;
int value = *ptr; // run time error
*ptr = 37; // run time error

цҸӃӌӝҿ ӌҸӃҺҸӌӄӨࡈ ӌӍ ӐӒӑӛ ӍҾӌҸ ࡫ɯƚǃȒƚȘɾŏɾǞȩȘ ǀŏʕȀɾࡈ ɯƚǃǀŏʕȀɾࡈ ŏżżƚɯɯ ʲǞȩȀŏɾǞȩȘࡈ
ࢀхӏӍһӏҸӋӋҸ ҺӝӎӍӊӌӄӊҸ ӌҿҾӍӎӒӐӑӄӋӒӧ ӍӎҿӏҸӗӄӧࡊࢁࡈ ࡊ࡬ࡏ

ߞߟ





щӈҸӃҸӑҿӊӛࡈ ӒӈҸӃӝҺҸӧәӄӅ ӕӍӑӛ ӈӒҾҸ

фҹӨӃҸӑҿӊӛӌӍ ӎӏӍӖӄӑҸӑӛ ӏҸӐӐӈҸӃ ӎӍ ӐӐӝӊӈҿࡏ

void* ࡴ ӐӎҿӗӄҸӊӛӌӝӅ ӑӄӎ ӒӈҸӃҸӑҿӊӨࡈ ӈӍӑӍӏӝӅ ӋӍӂҿӑ ӒӈҸӃӝҺҸӑӛ ӌҸ ӊӧҹӍӅ
ҸҾӏҿӐ Һ ӎҸӋӨӑӄࡏ тӍӂҿӑ ҹӝӑӛ ӎӏӄҺҿҾҿӌ ӈ ӊӧҹӍһӍ ҾӏӒһӍӋӒ ӑӄӎӒ ӒӈҸӃҸӑҿӊҿӅ ӄ
ӍҹӏҸӑӌӍࡏ
int x =
int* px
void* p
int* py

37;
= &x;
= px;
= p;

тӄӌӒӑӈҸ ӔӄӊӍӐӍӔӄӄࡇ ࢀпҸӈӍҺҸ ӎӏӄӏӍҾҸ ʲȩǞƇࡐࢁ ࡴ ӐӎӏӍӐӄӊ ӒӖӄӑҿӊӛࡈ ࡊ

ߞߠ





бӝҾҿӊҿӌӄҿ ҹӊӍӈҸ ӎҸӋӨӑӄ

ȒŏȀȀȩż ࣌ ›ƚȒȩɟʿ °+ŏɾƚ
żŏȀȀȩż ࣌ °+ŏɾƚ ŏȘƇ +Ȁƚŏɟ

void* malloc(size_t size);
ыӒӌӈӗӄӨ ҺӝҾҿӊӨҿӑ ҹӊӍӈ ӎҸӋӨӑӄ ӏҸӃӋҿӏӍӋ size ҹҸӅӑ ӄ ҺӍӃҺӏҸәҸҿӑ ӒӈҸӃҸӑҿӊӛ
ӌҸ ӌҸӖҸӊӍ ҹӊӍӈҸࡏ б ӐӊӒӖҸҿࡈ ҿӐӊӄ ӎҸӋӨӑӛ ҺӝҾҿӊӄӑӛ ӌҿ ӎӍӊӒӖӄӊӍӐӛࡈ ҺӍӃҺӏҸәҸҿӑ
NULLࡏ
void* calloc(size_t num, size_t size);
ыӒӌӈӗӄӨ ҺӝҾҿӊӨҿӑ ҹӊӍӈ ӎҸӋӨӑӄ ӏҸӃӋҿӏӍӋ num*size ҹҸӅӑࡈ ӃҸӌӒӊӨҿӑ ҿһӍ ӄ
ҺӍӃҺӏҸәҸҿӑ ӒӈҸӃҸӑҿӊӛ ӌҸ ӌҸӖҸӊӍࡏ б ӐӊӒӖҸҿࡈ ҿӐӊӄ ӎҸӋӨӑӛ ҺӝҾҿӊӄӑӛ ӌҿ
ӎӍӊӒӖӄӊӍӐӛࡈ ҺӍӃҺӏҸәҸҿӑ NULLࡏ

ߞߡ





фӐҺӍҹӍӂҾҿӌӄҿ ҹӊӍӈҸ ӎҸӋӨӑӄ

щӑҿӖӈӄ ӎҸӋӨӑӄ ӌҸ ӐҿӏҺҿӏӌӝӕ ӎӏӄӊӍӂҿӌӄӨӕࡈ ӈӍӑӍӏӝҿ ҾӍӊӂӌӝ
ӏҸҹӍӑҸӑӛ ӌҿӐӈӍӊӛӈӍ ӊҿӑࡈ ӌҿҾӍӎӒӐӑӄӋӝࡏ
щӑҿӖӈӄ ӎҸӋӨӑӄ Һ ҾҿӐӈӑӍӎӌӝӕ ӎӏӄӊӍӂҿӌӄӨӕ ӌҿ ӑҸӈ ӈӏӄӑӄӖӌӝࡈ ӌӍ
ӋӍһӒӑ ӄ ӎӏӄӖӄӌӨӑӛ ӌҿӒҾӍҹӐӑҺҸ ࡫ӐӋࡏ ӐӍҺӏҿӋҿӌӌӝҿ ҹӏҸӒӃҿӏӝ࡬ࡏ

void free(void* ptr);
ыӒӌӈӗӄӨ ӍӐҺӍҹӍӂҾҸҿӑ ҹӊӍӈ ӎҸӋӨӑӄࡏ жӐӊӄ ptr ӏҸҺҿӌ NULLࡈ ӌӄӖҿһӍ ӌҿ ҾҿӊҸҿӑࡏ
хӍӐӊҿ ҺӝӃӍҺҸ ӃӌҸӖҿӌӄҿ ӒӈҸӃҸӑҿӊӨ ptr ӍӐӑҸҿӑӐӨ ӎӏҿӂӌӄӋࡈ ӌӍ
ӏҸӃӝӋҿӌӍҺӝҺҸӑӛ ҿһӍ ӌҿӊӛӃӨࡏ
уҿӄӐӎӍӊӛӃӒҿӋӒӧ ӎҸӋӨӑӛ ӌӒӂӌӍ ӍҹӨӃҸӑҿӊӛӌӍ ӍӐҺӍҹӍӂҾҸӑӛࡈ ӄӌҸӖҿ ӏҸӌӍ ӄӊӄ
ӎӍӃҾӌӍ ӍӌҸ ӋӍӂҿӑ ӈӍӌӖӄӑӛӐӨ ࡫ӒӑҿӖӈҸ ӎҸӋӨӑӄ࡬ࡏ

ߞߢ





хӏӄӋҿӏࡇ ӋҸӐӐӄҺ ҾӄӌҸӋӄӖҿӐӈӍһӍ ӏҸӃӋҿӏҸ

ɯǞˌƚȩǀ ࡴ ӍӎҿӏҸӑӍӏࡈ ҺӝҾҸӧәӄӅ ӏҸӃӋҿӏ ӑӄӎӍҺ ҾҸӌӌӝӕࡏ уҸӎӏӄӋҿӏࡈ
ӌҸ vȘɾƚȀ ʾߤߢ ɯǞˌƚȩǀ࡫ǞȘɾ࡬ ࣌࣌ ߠࡏ



int n = 5;
int* p; // ???

ߞߣ





хӏӄӋҿӏࡇ ӋҸӐӐӄҺ ҾӄӌҸӋӄӖҿӐӈӍһӍ ӏҸӃӋҿӏҸ

ɯǞˌƚȩǀ ࡴ ӍӎҿӏҸӑӍӏࡈ ҺӝҾҸӧәӄӅ ӏҸӃӋҿӏ ӑӄӎӍҺ ҾҸӌӌӝӕࡏ уҸӎӏӄӋҿӏࡈ
ӌҸ vȘɾƚȀ ʾߤߢ ɯǞˌƚȩǀ࡫ǞȘɾ࡬ ࣌࣌ ߠࡏ



int n = 5;
int* p; // ???
p = malloc(n * sizeof(int));
if (p == NULL) { /* error */ }

ߞߣ





хӏӄӋҿӏࡇ ӋҸӐӐӄҺ ҾӄӌҸӋӄӖҿӐӈӍһӍ ӏҸӃӋҿӏҸ



ࡐࡐࡐ

ࡐࡐࡐ

ࡐࡐࡐ

0x100

ɯǞˌƚȩǀ ࡴ ӍӎҿӏҸӑӍӏࡈ ҺӝҾҸӧәӄӅ ӏҸӃӋҿӏ ӑӄӎӍҺ ҾҸӌӌӝӕࡏ уҸӎӏӄӋҿӏࡈ
ӌҸ vȘɾƚȀ ʾߤߢ ɯǞˌƚȩǀ࡫ǞȘɾ࡬ ࣌࣌ ߠࡏ

ࡐࡐࡐ

ࡐࡐࡐ
0x113

int n = 5;
int* p; // 0x100
p = malloc(n * sizeof(int));
if (p == NULL) { /* error */ }

ߞߣ





хӏӄӋҿӏࡇ ӋҸӐӐӄҺ ҾӄӌҸӋӄӖҿӐӈӍһӍ ӏҸӃӋҿӏҸ



ࡐࡐࡐ

ࡐࡐࡐ

ࡐࡐࡐ

0x100

ɯǞˌƚȩǀ ࡴ ӍӎҿӏҸӑӍӏࡈ ҺӝҾҸӧәӄӅ ӏҸӃӋҿӏ ӑӄӎӍҺ ҾҸӌӌӝӕࡏ уҸӎӏӄӋҿӏࡈ
ӌҸ vȘɾƚȀ ʾߤߢ ɯǞˌƚȩǀ࡫ǞȘɾ࡬ ࣌࣌ ߠࡏ

ࡐࡐࡐ

ࡐࡐࡐ
0x113

int n = 5;
int* p; // 0x100
p = malloc(n * sizeof(int));
if (p == NULL) { /* error */ }
p[0] = p[n/2] = p[n-1] = 37;

ߞߣ





хӏӄӋҿӏࡇ ӋҸӐӐӄҺ ҾӄӌҸӋӄӖҿӐӈӍһӍ ӏҸӃӋҿӏҸ



ߟߣ

ࡐࡐࡐ

0x100

ߟߣ

ɯǞˌƚȩǀ ࡴ ӍӎҿӏҸӑӍӏࡈ ҺӝҾҸӧәӄӅ ӏҸӃӋҿӏ ӑӄӎӍҺ ҾҸӌӌӝӕࡏ уҸӎӏӄӋҿӏࡈ
ӌҸ vȘɾƚȀ ʾߤߢ ɯǞˌƚȩǀ࡫ǞȘɾ࡬ ࣌࣌ ߠࡏ

ࡐࡐࡐ

ߟߣ
0x113

int n = 5;
int* p; // 0x100
p = malloc(n * sizeof(int));
if (p == NULL) { /* error */ }
p[0] = p[n/2] = p[n-1] = 37;

ߞߣ





хӏӄӋҿӏࡇ ӋҸӐӐӄҺ ҾӄӌҸӋӄӖҿӐӈӍһӍ ӏҸӃӋҿӏҸ



ߟߣ

ࡐࡐࡐ

0x100

ߟߣ

ɯǞˌƚȩǀ ࡴ ӍӎҿӏҸӑӍӏࡈ ҺӝҾҸӧәӄӅ ӏҸӃӋҿӏ ӑӄӎӍҺ ҾҸӌӌӝӕࡏ уҸӎӏӄӋҿӏࡈ
ӌҸ vȘɾƚȀ ʾߤߢ ɯǞˌƚȩǀ࡫ǞȘɾ࡬ ࣌࣌ ߠࡏ

ࡐࡐࡐ

ߟߣ
0x113

int n = 5;
int* p; // 0x100
p = malloc(n * sizeof(int));
if (p == NULL) { /* error */ }
p[0] = p[n/2] = p[n-1] = 37;
free(p);

ߞߣ





хӏӄӋҿӏࡇ ӋҸӐӐӄҺ ҾӄӌҸӋӄӖҿӐӈӍһӍ ӏҸӃӋҿӏҸ



ࡐࡐࡐ

ࡐࡐࡐ

ࡐࡐࡐ

0x100

ɯǞˌƚȩǀ ࡴ ӍӎҿӏҸӑӍӏࡈ ҺӝҾҸӧәӄӅ ӏҸӃӋҿӏ ӑӄӎӍҺ ҾҸӌӌӝӕࡏ уҸӎӏӄӋҿӏࡈ
ӌҸ vȘɾƚȀ ʾߤߢ ɯǞˌƚȩǀ࡫ǞȘɾ࡬ ࣌࣌ ߠࡏ

ࡐࡐࡐ

ࡐࡐࡐ
0x113

int n = 5;
int* p; // 0x100
p = malloc(n * sizeof(int));
if (p == NULL) { /* error */ }
p[0] = p[n/2] = p[n-1] = 37;
free(p);

ߞߣ





лӃӋҿӌҿӌӄҿ ӏҸӃӋҿӏҸ ҹӊӍӈҸ ӎҸӋӨӑӄ

уҿӊӛӃӨ ӎӄӐҸӑӛ ࢆɔɾɟ ࣌ ɟƚŏȀȀȩż࡫ɔɾɟࡈ ࡏࡏࡏ࡬ࢆ ࡴ ӒӑҿӖӈҸ ӎҸӋӨӑӄࡏ

void* realloc(void* ptr, size_t size);
ыӒӌӈӗӄӨ ӄӃӋҿӌӨҿӑ ӏҸӃӋҿӏ ҹӊӍӈҸ ӎҸӋӨӑӄ ҾӍ size ҹҸӅӑࡏ б ӐӊӒӖҸҿ ӒӐӎҿӘӌӍһӍ
ӄӃӋҿӌҿӌӄӨ ӏҸӃӋҿӏҸ ҺӍӃҺӏҸәҸҿӑ ӒӈҸӃҸӑҿӊӛ ӌҸ ӌҸӖҸӊӍ ҹӊӍӈҸࡈ ӄӌҸӖҿ NULLࡏ
ыӒӌӈӗӄӨ ӋӍӂҿӑ ӈҸӈ ӒӋҿӌӛӘҸӑӛ ӏҸӃӋҿӏࡈ ӑҸӈ ӄ ӒҺҿӊӄӖӄҺҸӑӛࡏ бӍӃӋӍӂӌӍ
ӎҿӏҿӋҿәҿӌӄҿ ӐӍҾҿӏӂӄӋӍһӍ ӎҸӋӨӑӄࡈ ӎӏӄ ӢӑӍӋ ҺӍӃҺӏҸәҸҿӑӐӨ ӒӈҸӃҸӑҿӊӛ ӌҸ
ӌӍҺӍҿ ӋҿӐӑӍӏҸӐӎӍӊӍӂҿӌӄҿࡏ
б ӐӊӒӖҸҿ ӌҿӒӐӎҿӘӌӍһӍ ӄӃӋҿӌҿӌӄӨ ӏҸӃӋҿӏҸࡈ ӄӃӌҸӖҸӊӛӌӝӅ ҹӊӍӈ ӎҸӋӨӑӄ ӌҿ
ӍӐҺӍҹӍӂҾҸҿӑӐӨࡏ

ߞߤ





бӍӃҺӏҸәҿӌӄҿ ӋҸӐӐӄҺӍҺ ӄӃ ӔӒӌӈӗӄӅ

шҿӎҿӏӛ Ӌӝ ӃӌҸҿӋࡈ ӈҸӈ Ӌӝ ӋӍӂҿӋ ҺҿӏӌӒӑӛ ӋҸӐӐӄҺ ӄӃ ӔӒӌӈӗӄӄࡏ

int* reverse(int src[], size_t size) {
int* dst = malloc(size * sizeof(int));
if (dst == NULL) { return NULL; }
for (size_t i = 0; i < size; i++) {
dst[i] = src[size - i - 1];
}
return dst;
}
int original[3] = {1, 2, 3};
int* reversed = reverse(original, 3);
// use reversed
free(reversed);
ߞߥ





хӊӍӐӈӄҿ ҾӄӌҸӋӄӖҿӐӈӄҿ ҾҺӒӋҿӏӌӝҿ ӋҸӐӐӄҺӝ

хӊӧӐӝࡇ ҺӝӐӍӈҸӨ ӎӏӍӄӃҺӍҾӄӑҿӊӛӌӍӐӑӛࡏ
тӄӌӒӐӝࡇ ҾӍӎӍӊӌӄӑҿӊӛӌҸӨ ҸӏӄӔӋҿӑӄӈҸ ӎӏӄ ҾӍӐӑӒӎҿ ӈ ӢӊҿӋҿӌӑҸӋࡏ

int* arr = malloc(n * m * sizeof(int));
if (arr == NULL) { /* error */ }
// arr[i*m + j] = 37;
free(arr);

ߟߜ





еӄӌҸӋӄӖҿӐӈӄҿ ӋҸӐӐӄҺӝ ӋҸӐӐӄҺӍҺࡇ ӄҾҿӨ



a[0]

a[1]

0x100

0x200

0x20

хӊӧӐӝࡇ ӎӏӄҺӝӖӌӝӅ ӐӄӌӑҸӈӐӄӐ ҾӍӐӑӒӎҸ ӈ ӢӊҿӋҿӌӑҸӋࡏ
тӄӌӒӐӝࡇ ҹӍӊӛӘҿҿ ӎӍӑӏҿҹӊҿӌӄҿ ӎҸӋӨӑӄࡈ ӊӄӘӌӨӨ ӈӍӐҺҿӌӌӍӐӑӛࡏ
шҸӈӍӅ ӎӍҾӕӍҾ ӄӐӎӍӊӛӃӒҿӑӐӨ ӎӍ ӒӋӍӊӖҸӌӄӧ Һ ӌҿӈӍӑӍӏӝӕ ӨӃӝӈҸӕ
ӎӏӍһӏҸӋӋӄӏӍҺҸӌӄӨ ҾӊӨ ӏҿҸӊӄӃҸӗӄӄ ӋӌӍһӍӋҿӏӌӝӕ ӋҸӐӐӄҺӍҺࡏ
уҸӎӏӄӋҿӏࡈ Һ ‰ŏʲŏࡏ

0x24

a[0][0] a[0][1] a[0][2]



ьӏҸӌӄӋ ӋҸӐӐӄҺ ӒӈҸӃҸӑҿӊҿӅ ӌҸ ҾӏӒһӄҿ ӋҸӐӐӄҺӝࡏ

a[1][0] a[1][1] a[1][2]

ߝߜ

ߞߜ

ߟߜ

ߠߜ

ߡߜ

ߢߜ

0x100

0x102

0x104

0x200

0x202

0x204

short** a = /* 0x20 */;
// {{10, 20, 30}, {40, 50, 60}}
a[i][j] == *(*(a + i) + j)
ߟߝ





еӄӌҸӋӄӖҿӐӈӄҿ ҾҺӒӋҿӏӌӝҿ ӋҸӐӐӄҺӝ ӋҸӐӐӄҺӍҺ
int** arr = malloc(n * sizeof(int*));
if (arr == NULL) { /* error */ }
for (int i = 0; i < n; i++) {
arr[i] = malloc(m * sizeof(int));
if (arr[i] == NULL) { /* error */ }
}
// arr[i][j] = 37;
for (int i = 0; i < n; i++) {
free(arr[i]);
}
free(arr);
ߟߞ





пӍӌҿӗ ӑӏҿӑӛҿӅ ӊҿӈӗӄӄ

ߟߟ