Upgrade to Pro — share decks privately, control downloads, hide ads and more …

№ 2016/3. Массивы, указатели, память

№ 2016/3. Массивы, указатели, память

Лекция о работе с массивами, указателями, о видах памяти и конкретно о динамической памяти.

More Decks by Основы Программирования

Other Decks in Programming

Transcript

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

    ӋҸӐӐӄҺҸ arrࡈ ӐӍӐӑӍӨәҿһӍ ӄӃ N ӢӊҿӋҿӌӑӍҺ ӎӏӍӄӃҺӍӊӛӌӍһӍ ӑӄӎҸ Tࡇ T arr[N]; һҾҿ N ࡴ ӈӍӌӐӑҸӌӑҸ ҺӏҿӋҿӌӄ ӈӍӋӎӄӊӨӗӄӄࡏ хӏӄӋҿӏ ӍҹӜӨҺӊҿӌӄӨ ӄ ӄӌӄӗӄҸӊӄӃҸӗӄӄ ӋҸӐӐӄҺҸࡇ int xs[4] = {20, 10}; // {20, 10, 0, 0} int ys[] = {20, 10}; // {20, 10} ߟ
  2. фҾӌӍӋҿӏӌӝҿ ӋҸӐӐӄҺӝࡇ ҾӍӐӑӒӎ ӈ ӢӊҿӋҿӌӑҸӋ T arr[N]ࡇ ࡏ ࡏ ࡏ

    ࡏ ࡏ ࡏ ࡏ ࡏ ࡏ ࡏ ࡏ ࡏ ࡏ ࡏ ࡏ ࡏ 0 ࡏ ࡏ 1 ࡏ ࡏ ࡏ ࡏ ࡊ ࡏ ࡏ ࡏ ࡏ N-1 эӑҿӌӄҿ ӢӊҿӋҿӌӑҸ ӄӃ ӋҸӐӐӄҺҸ ӎӍ ӄӌҾҿӈӐӒ iࡇ T value = arr[i]; кҸӎӄӐӛ ӢӊҿӋҿӌӑҸ Һ ӋҸӐӐӄҺ ӎӍ ӄӌҾҿӈӐӒ iࡇ arr[i] = new_value; ߠ
  3. еҺӒӋҿӏӌӝҿ ӋҸӐӐӄҺӝ T arr[N][M]ࡇ ࡏ ࡏ ࡏ ࡏ ࡏ ࡏ

    0 ࡏ ࡏ ࡏ ࡏ ࡊ ࡏ ࡏ ࡏ ࡏ M-1 ࡏ ࡏ 0 ࡏ ࡏ ࡏ ࡏ ࡏ ࡏ ࡏ ࡏ ࡏ ࡏ ࡏ ࡏ ࡏ ࡏ ࡏ ࡏ ࡏ ࡏ ࡏ ࡏ ࡏ ࡏ ࡏ ࡏ ࡏ ࡏ ࡏ N-1 ࡏ ࡏ ࡏ ࡏ ࡏ ࡏ ࡏ ࡏ ࡏ ࡏ int m[2][3] = { {1, 2, 3}, {4, 5, 6}}; int two = m[0][1]; m[1][2] = 66; ߡ
  4. хҿӏҿҾҸӖҸ ӋҸӐӐӄҺӍҺ Һ ӔӒӌӈӗӄӄ кҸӖҸӐӑӒӧࡈ ӏҸӃӋҿӏ ӋҸӐӐӄҺҸ ӎҿӏҿҾҸҿӑӐӨ ӍӑҾҿӊӛӌӝӋ ӎҸӏҸӋҿӑӏӍӋࡇ

    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) { // ... } ߣ
  5. хҿӏҿҾҸӖҸ ӔӄӈӐӄӏӍҺҸӌӌӝӕ ӋҸӐӐӄҺӍҺ Һ ӔӒӌӈӗӄӄ тҸӐӐӄҺӝ ӂҿӐӑӈӍ ӔӄӈӐӄӏӍҺҸӌӌӝӕ ӏҸӃӋҿӏӌӍӐӑҿӅ ӎҿӏҿҾҸӧӑӐӨ

    ҹҿӃ ҾӍӎӍӊӌӄӑҿӊӛӌӝӕ ӎҸӏҸӋҿӑӏӍҺࡇ 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; } ߤ
  6. бӍӃҺӏҸәҿӌӄҿ ӋҸӐӐӄҺӍҺ ӄӃ ӔӒӌӈӗӄӅ 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 ߥ
  7. ࢀбӍӃҺӏҸәҿӌӄҿࢁ ӋҸӐӐӄҺӍҺ ӄӃ ӔӒӌӈӗӄӅ 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 ߝߜ
  8. щӈҸӃҸӑҿӊӛ ӈҸӈ ҸҾӏҿӐ ࡏ ࡏ ࡏ ߡߝߤ ࡏ ࡏ ߟߞߜ

    ࡏ 0x20 ࡏ 0x23 ࡏ 0x40 ࡏ 0x43 int x = 518; int y = 320; ߝߞ
  9. щӈҸӃҸӑҿӊӛ ӈҸӈ ҸҾӏҿӐ ࡏ ࡏ ࡏ ߡߝߤ ࡏ ࡏ ߟߞߜ

    ࡏ 0x20 ࡏ 0x23 ࡏ 0x40 ࡏ 0x43 int x = 518; int y = 320; int* ptr; ߝߞ
  10. щӈҸӃҸӑҿӊӛ ӈҸӈ ҸҾӏҿӐ ࡏ ࡏ ࡏ ߡߝߤ ࡏ ࡏ ߟߞߜ

    ࡏ 0x20 ࡏ 0x23 ࡏ 0x40 ࡏ 0x43 int x = 518; int y = 320; int* ptr; // & - взятие адреса // * - разыменование указателя ptr = &x; printf("%p %d\n", ptr, *ptr); // 0x20 518 ptr = &y; printf("%p %d\n", ptr, *ptr); // 0x40 320 ߝߞ
  11. щӈҸӃҸӑҿӊӛ ӈҸӈ ҸҾӏҿӐ ࡏ ࡏ ࡏ ߡߝߤ ࡏ ࡏ ߟߞߜ

    ࡏ 0x20 ࡏ 0x23 ࡏ 0x40 ࡏ 0x43 int x = 518; int y = 320; int* ptr; // & - взятие адреса // * - разыменование указателя ptr = &x; printf("%p %d\n", ptr, *ptr); // 0x20 518 ptr = &y; printf("%p %d\n", ptr, *ptr); // 0x40 320 *ptr = 999; ߝߞ
  12. щӈҸӃҸӑҿӊӛ ӈҸӈ ҸҾӏҿӐ ࡏ ࡏ ࡏ ߡߝߤ ࡏ ࡏ ߥߥߥ

    ࡏ 0x20 ࡏ 0x23 ࡏ 0x40 ࡏ 0x43 int x = 518; int y = 320; int* ptr; // & - взятие адреса // * - разыменование указателя ptr = &x; printf("%p %d\n", ptr, *ptr); // 0x20 518 ptr = &y; printf("%p %d\n", ptr, *ptr); // 0x40 320 *ptr = 999; ߝߞ
  13. щӈҸӃҸӑҿӊӛ ӈҸӈ ҸҾӏҿӐ ࡏ ࡏ ࡏ ߡߝߤ ࡏ ࡏ ߥߥߥ

    ࡏ 0x20 ࡏ 0x23 ࡏ 0x40 ࡏ 0x43 int x = 518; int y = 320; 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 ߝߞ
  14. хӏӄӋҿӏࡇ ҺӍӃҺӏҸӑ ҾҺӒӕ ӃӌҸӖҿӌӄӅ ӄӃ ӔӒӌӈӗӄӄ ࡏ ࡏ ࡏ 0x20

    ࡏ 0x23 ࡏ q ࡏ ࡏ 0x40 ࡏ 0x43 ࡏ r ࡏ ࡐࡐࡐ ࡏ ࡐࡐࡐ void divide( int a, int b, int* pq, int* pr) { *pq = a / b; *pr = a % b; } int x = 123, y = 10; int q, r; ߝߟ
  15. хӏӄӋҿӏࡇ ҺӍӃҺӏҸӑ ҾҺӒӕ ӃӌҸӖҿӌӄӅ ӄӃ ӔӒӌӈӗӄӄ ࡏ ࡏ ࡏ 0x20

    ࡏ 0x23 ࡏ q ࡏ ࡏ 0x40 ࡏ 0x43 ࡏ r ࡏ ࡐࡐࡐ ࡏ ࡐࡐࡐ void divide( int a, int b, // 123, 10 int* pq, // 0x20 int* pr) // 0x40 { *pq = a / b; *pr = a % b; } int x = 123, y = 10; int q, r; divide(x, y, &q, &r); ߝߟ
  16. хӏӄӋҿӏࡇ ҺӍӃҺӏҸӑ ҾҺӒӕ ӃӌҸӖҿӌӄӅ ӄӃ ӔӒӌӈӗӄӄ ࡏ ࡏ ࡏ 0x20

    ࡏ 0x23 ࡏ q ࡏ ࡏ 0x40 ࡏ 0x43 ࡏ r ࡏ ࡐࡐࡐ ࡏ ࡐࡐࡐ void divide( int a, int b, // 123, 10 int* pq, // 0x20 int* pr) // 0x40 { *pq = a / b; // *(0x20) = 12 *pr = a % b; } int x = 123, y = 10; int q, r; divide(x, y, &q, &r); ߝߟ
  17. хӏӄӋҿӏࡇ ҺӍӃҺӏҸӑ ҾҺӒӕ ӃӌҸӖҿӌӄӅ ӄӃ ӔӒӌӈӗӄӄ ࡏ ࡏ ࡏ 0x20

    ࡏ 0x23 ࡏ q ࡏ ࡏ 0x40 ࡏ 0x43 ࡏ r ࡏ ߝߞ ࡏ ࡐࡐࡐ void divide( int a, int b, // 123, 10 int* pq, // 0x20 int* pr) // 0x40 { *pq = a / b; // *(0x20) = 12 *pr = a % b; } int x = 123, y = 10; int q, r; divide(x, y, &q, &r); ߝߟ
  18. хӏӄӋҿӏࡇ ҺӍӃҺӏҸӑ ҾҺӒӕ ӃӌҸӖҿӌӄӅ ӄӃ ӔӒӌӈӗӄӄ ࡏ ࡏ ࡏ 0x20

    ࡏ 0x23 ࡏ q ࡏ ࡏ 0x40 ࡏ 0x43 ࡏ r ࡏ ߝߞ ࡏ ࡐࡐࡐ void divide( int a, int b, // 123, 10 int* pq, // 0x20 int* pr) // 0x40 { *pq = a / b; // *(0x20) = 12 *pr = a % b; // *(0x40) = 3 } int x = 123, y = 10; int q, r; divide(x, y, &q, &r); ߝߟ
  19. хӏӄӋҿӏࡇ ҺӍӃҺӏҸӑ ҾҺӒӕ ӃӌҸӖҿӌӄӅ ӄӃ ӔӒӌӈӗӄӄ ࡏ ࡏ ࡏ 0x20

    ࡏ 0x23 ࡏ q ࡏ ࡏ 0x40 ࡏ 0x43 ࡏ r ࡏ ߝߞ ࡏ ߟ void divide( int a, int b, // 123, 10 int* pq, // 0x20 int* pr) // 0x40 { *pq = a / b; // *(0x20) = 12 *pr = a % b; // *(0x40) = 3 } int x = 123, y = 10; int q, r; divide(x, y, &q, &r); ߝߟ
  20. хӏӄӋҿӏࡇ ҺӍӃҺӏҸӑ ҾҺӒӕ ӃӌҸӖҿӌӄӅ ӄӃ ӔӒӌӈӗӄӄ ࡏ ࡏ ࡏ 0x20

    ࡏ 0x23 ࡏ q ࡏ ࡏ 0x40 ࡏ 0x43 ࡏ r ࡏ ߝߞ ࡏ ߟ void divide( int a, int b, // 123, 10 int* pq, // 0x20 int* pr) // 0x40 { *pq = a / b; // *(0x20) = 12 *pr = a % b; // *(0x40) = 3 } 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 ߝߟ
  21. хӏҿҾӐӑҸҺӊҿӌӄҿ ӒӈҸӃҸӑҿӊӨ ࡏ ࡏ ࡏ 0x20 ࡏ x ࡏ ߟߣ

    ࡏ ࡏ 0x80 ࡏ 0x83 ࡏ px char x = 37; char* px = &x; ߝߠ
  22. хӏҿҾӐӑҸҺӊҿӌӄҿ ӒӈҸӃҸӑҿӊӨ ࡏ ࡏ ࡏ 0x20 ࡏ x ࡏ ߟߣ

    ࡏ ࡏ 0x80 ࡏ 0x83 ࡏ px ࡏ 0x20 char x = 37; char* px = &x; ߝߠ
  23. хӏҿҾӐӑҸҺӊҿӌӄҿ ӒӈҸӃҸӑҿӊӨ ࡏ ࡏ ࡏ 0x20 ࡏ x ࡏ ߟߣ

    ࡏ ࡏ 0x80 ࡏ 0x83 ࡏ px ࡏ 0x20 ࡏ ࡏ 0xE0 ࡏ 0xE4 ࡏ ppx char x = 37; char* px = &x; char** ppx = &px; ߝߠ
  24. хӏҿҾӐӑҸҺӊҿӌӄҿ ӒӈҸӃҸӑҿӊӨ ࡏ ࡏ ࡏ 0x20 ࡏ x ࡏ ߟߣ

    ࡏ ࡏ 0x80 ࡏ 0x83 ࡏ px ࡏ 0x20 ࡏ ࡏ 0xE0 ࡏ 0xE4 ࡏ ppx ࡏ 0x80 char x = 37; char* px = &x; char** ppx = &px; ߝߠ
  25. хӏҿҾӐӑҸҺӊҿӌӄҿ ӒӈҸӃҸӑҿӊӨ ࡏ ࡏ ࡏ 0x20 ࡏ x ࡏ ߟߣ

    ࡏ ࡏ 0x80 ࡏ 0x83 ࡏ px ࡏ 0x20 ࡏ ࡏ 0xE0 ࡏ 0xE4 ࡏ ppx ࡏ 0x80 char x = 37; char* px = &x; char** ppx = &px; **ppx = 42; ߝߠ
  26. хӏҿҾӐӑҸҺӊҿӌӄҿ ӒӈҸӃҸӑҿӊӨ ࡏ ࡏ ࡏ 0x20 ࡏ x ࡏ ߠߞ

    ࡏ ࡏ 0x80 ࡏ 0x83 ࡏ px ࡏ 0x20 ࡏ ࡏ 0xE0 ࡏ 0xE4 ࡏ ppx ࡏ 0x80 char x = 37; char* px = &x; char** ppx = &px; **ppx = 42; ߝߠ
  27. хӏҿҾӐӑҸҺӊҿӌӄҿ ӋҸӐӐӄҺҸ ࡏ ࡏ ࡏ 0x20 ࡏ a[0] ࡏ ࡏ

    0x22 ࡏ a[1] ࡏ ࡏ 0x24 ࡏ a[2] ࡏ ߝߜ ࡏ ߞߜ ࡏ ߟߜ short a[3] = {10, 20, 30}; ߝߡ
  28. хӏҿҾӐӑҸҺӊҿӌӄҿ ӋҸӐӐӄҺҸ ࡏ ࡏ ࡏ 0x20 ࡏ a[0] ࡏ ࡏ

    0x22 ࡏ a[1] ࡏ ࡏ 0x24 ࡏ a[2] ࡏ ߝߜ ࡏ ߞߜ ࡏ ߟߜ short a[3] = {10, 20, 30}; short* p = &(a[0]); // 0x20 short* p1 = p + 1; // 0x22 short* p2 = p + 2; // 0x24 ߝߡ
  29. хӏҿҾӐӑҸҺӊҿӌӄҿ ӋҸӐӐӄҺҸ ࡏ ࡏ ࡏ 0x20 ࡏ a[0] ࡏ ࡏ

    0x22 ࡏ a[1] ࡏ ࡏ 0x24 ࡏ a[2] ࡏ ߝߜ ࡏ ߞߜ ࡏ ߟߜ short a[3] = {10, 20, 30}; short* p = &(a[0]); // 0x20 short* p1 = p + 1; // 0x22 short* p2 = p + 2; // 0x24 *p1 = 200; ߝߡ
  30. хӏҿҾӐӑҸҺӊҿӌӄҿ ӋҸӐӐӄҺҸ ࡏ ࡏ ࡏ 0x20 ࡏ a[0] ࡏ ࡏ

    0x22 ࡏ a[1] ࡏ ࡏ 0x24 ࡏ a[2] ࡏ ߝߜ ࡏ ߞߜߜ ࡏ ߟߜ short a[3] = {10, 20, 30}; short* p = &(a[0]); // 0x20 short* p1 = p + 1; // 0x22 short* p2 = p + 2; // 0x24 *p1 = 200; ߝߡ
  31. хӏҿҾӐӑҸҺӊҿӌӄҿ ӋҸӐӐӄҺҸ ࡏ ࡏ ࡏ 0x20 ࡏ a[0] ࡏ ࡏ

    0x22 ࡏ a[1] ࡏ ࡏ 0x24 ࡏ a[2] ࡏ ߝߜ ࡏ ߞߜߜ ࡏ ߟߜ short a[3] = {10, 20, 30}; short* p = &(a[0]); // 0x20 short* p1 = p + 1; // 0x22 short* p2 = p + 2; // 0x24 *p1 = 200; *p2 = *p1 + *p2; ߝߡ
  32. хӏҿҾӐӑҸҺӊҿӌӄҿ ӋҸӐӐӄҺҸ ࡏ ࡏ ࡏ 0x20 ࡏ a[0] ࡏ ࡏ

    0x22 ࡏ a[1] ࡏ ࡏ 0x24 ࡏ a[2] ࡏ ߝߜ ࡏ ߞߜߜ ࡏ ߞߟߜ short a[3] = {10, 20, 30}; short* p = &(a[0]); // 0x20 short* p1 = p + 1; // 0x22 short* p2 = p + 2; // 0x24 *p1 = 200; *p2 = *p1 + *p2; ߝߡ
  33. хӏҿҾӐӑҸҺӊҿӌӄҿ ӋҸӐӐӄҺҸ ࡏ ࡏ ࡏ 0x20 ࡏ a[0] ࡏ ࡏ

    0x22 ࡏ a[1] ࡏ ࡏ 0x24 ࡏ a[2] ࡏ ߝߜ ࡏ ߞߜߜ ࡏ ߞߟߜ short a[3] = {10, 20, 30}; short* p = &(a[0]); // 0x20 short* p1 = p + 1; // 0x22 short* p2 = p + 2; // 0x24 *(p+1) = 200; *(p+2) = *(p+1) + *(p+2); ߝߡ
  34. хӏҿҾӐӑҸҺӊҿӌӄҿ ӋҸӐӐӄҺҸ ࡏ ࡏ ࡏ 0x20 ࡏ a[0] ࡏ ࡏ

    0x22 ࡏ a[1] ࡏ ࡏ 0x24 ࡏ a[2] ࡏ ߝߜ ࡏ ߞߜߜ ࡏ ߞߟߜ short a[3] = {10, 20, 30}; short* p = &(a[0]); // 0x20 short* p1 = p + 1; // 0x22 short* p2 = p + 2; // 0x24 *(p+1) = 200; *(p+2) = *(p+1) + *(p+2); // *(x + y) == x[y] ߝߡ
  35. хӏҿҾӐӑҸҺӊҿӌӄҿ ӋҸӐӐӄҺҸ ࡏ ࡏ ࡏ 0x20 ࡏ a[0] ࡏ ࡏ

    0x22 ࡏ a[1] ࡏ ࡏ 0x24 ࡏ a[2] ࡏ ߝߜ ࡏ ߞߜߜ ࡏ ߞߟߜ short a[3] = {10, 20, 30}; short* p = &(a[0]); // 0x20 short* p1 = p + 1; // 0x22 short* p2 = p + 2; // 0x24 p[1] = 200; p[2] = p[1] + p[2]; // *(x + y) == x[y] ߝߡ
  36. хӏҿҾӐӑҸҺӊҿӌӄҿ ӋҸӐӐӄҺҸ ࡏ ࡏ ࡏ 0x20 ࡏ a[0] ࡏ ࡏ

    0x22 ࡏ a[1] ࡏ ࡏ 0x24 ࡏ a[2] ࡏ ߝߜ ࡏ ߞߜߜ ࡏ ߞߟߜ short a[3] = {10, 20, 30}; short* p = a; // 0x20 short* p1 = p + 1; // 0x22 short* p2 = p + 2; // 0x24 p[1] = 200; p[2] = p[1] + p[2]; // *(x + y) == x[y] ߝߡ
  37. хӏҿҾӐӑҸҺӊҿӌӄҿ ҾҺӒӋҿӏӌӍһӍ ӋҸӐӐӄҺҸ ࡏ ࡏ ࡏ 0x20 ࡏ a[0][0] ࡏ

    ࡏ 0x22 ࡏ a[0][1] ࡏ ࡏ 0x24 ࡏ a[0][2] ࡏ ࡏ 0x26 ࡏ a[1][0] ࡏ ࡏ 0x28 ࡏ a[1][1] ࡏ ࡏ 0x2A ࡏ a[1][2] ࡏ ߝߜ ࡏ ߞߜ ࡏ ߟߜ ࡏ ߠߜ ࡏ ߡߜ ࡏ ߢߜ short a[2][3] = { {10, 20, 30}, {40, 50, 60}}; a[i][j] == *(a + i*3 + j); ߝߢ
  38. хӏҿҾӐӑҸҺӊҿӌӄҿ ҾҺӒӋҿӏӌӍһӍ ӋҸӐӐӄҺҸ ࡏ ࡏ ࡏ 0x20 ࡏ a[0][0] ࡏ

    ࡏ 0x22 ࡏ a[0][1] ࡏ ࡏ 0x24 ࡏ a[0][2] ࡏ ࡏ 0x26 ࡏ a[1][0] ࡏ ࡏ 0x28 ࡏ a[1][1] ࡏ ࡏ 0x2A ࡏ a[1][2] ࡏ ߝߜ ࡏ ߞߜ ࡏ ߟߜ ࡏ ߠߜ ࡏ ߡߜ ࡏ ߢߜ short a[2][3] = { {10, 20, 30}, {40, 50, 60}}; a[i][j] == *(a + i*3 + j); short* p = a; a[i][j] == p[i*3 + j]; ߝߢ
  39. чӑҸӑӄӖҿӐӈҸӨ ӎҸӋӨӑӛ хӏӄӋҿӏӝࡇ ࡄ һӊӍҹҸӊӛӌӝҿ ӎҿӏҿӋҿӌӌӝҿࡇ char game_field[100][100]ࡈ ࡄ ӐӑӏӍӈӍҺӝҿ

    ӊӄӑҿӏҸӊӝࡇ "Hello world!"ࡈ ࡄ ࡏࡏࡏ чҺӍӅӐӑҺҸࡇ ࡄ ҺӝҾҿӊӨҿӑӐӨ ҺӍ ҺӏҿӋӨ ӐӑҸӏӑҸ ӎӏӍһӏҸӋӋӝࡈ ࡄ ӍӐҺӍҹӍӂҾҸҿӑӐӨ ҺӍ ҺӏҿӋӨ ӃҸҺҿӏӘҿӌӄӨ ӎӏӍһӏҸӋӋӝࡈ ࡄ ӄӌӄӗӄҸӊӄӃӄӏӒҿӑӐӨ ӌӒӊҿҺӝӋӄ ӃӌҸӖҿӌӄӨӋӄࡈ ࡄ ӔӄӈӐӄӏӍҺҸӌӌӝӅ ӏҸӃӋҿӏࡏ ߝߤ
  40. ЯҺӑӍӋҸӑӄӖҿӐӈҸӨ ӎҸӋӨӑӛ хӏӄӋҿӏӝࡇ ࡄ ӊӍӈҸӊӛӌӝҿ ӎҿӏҿӋҿӌӌӝҿࡇ {...; int tmp; ...}ࡈ

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

    ࡄ ӄӃӌҸӖҸӊӛӌӍ ӌҿ ӄӌӄӗӄҸӊӄӃӄӏӍҺҸӌҸࡈ ࡄ ӏҸӃӋҿӏ ӃҸҾҸҿӑӐӨ ҾӄӌҸӋӄӖҿӐӈӄࡏ ߞߜ
  42. щӈҸӃҸӑҿӊӛࡈ ӌӄӈӒҾҸ ӌҿ ӒӈҸӃӝҺҸӧәӄӅ NULL ࡴ ӐӎҿӗӄҸӊӛӌӍҿ ӃӌҸӖҿӌӄҿࡈ ӐӄӋҺӍӊӄӃӄӏӒӧәҿҿࡈ ӖӑӍ

    ӒӈҸӃҸӑҿӊӛ ӌҿ ӒӈҸӃӝҺҸҿӑ ӌӄ ӌҸ ӈҸӈӍӅ ӢӊҿӋҿӌӑ ӎҸӋӨӑӄࡏ int* ptr = NULL; int value = *ptr; // run time error *ptr = 37; // run time error цҸӃӌӝҿ ӌҸӃҺҸӌӄӨࡈ ӌӍ ӐӒӑӛ ӍҾӌҸ ࡫ɯƚǃȒƚȘɾŏɾǞȩȘ ǀŏʕȀɾࡈ ɯƚǃǀŏʕȀɾࡈ ŏżżƚɯɯ ʲǞȩȀŏɾǞȩȘࡈ ࢀхӏӍһӏҸӋӋҸ ҺӝӎӍӊӌӄӊҸ ӌҿҾӍӎӒӐӑӄӋӒӧ ӍӎҿӏҸӗӄӧࡊࢁࡈ ࡊ࡬ࡏ ߞߟ
  43. щӈҸӃҸӑҿӊӛࡈ ӒӈҸӃӝҺҸӧәӄӅ ӕӍӑӛ ӈӒҾҸ void* ࡴ ӐӎҿӗӄҸӊӛӌӝӅ ӑӄӎ ӒӈҸӃҸӑҿӊӨࡈ ӈӍӑӍӏӝӅ

    ӋӍӂҿӑ ӒӈҸӃӝҺҸӑӛ ӌҸ ӊӧҹӍӅ ҸҾӏҿӐ Һ ӎҸӋӨӑӄࡏ тӍӂҿӑ ҹӝӑӛ ӎӏӄҺҿҾҿӌ ӈ ӊӧҹӍһӍ ҾӏӒһӍӋӒ ӑӄӎӒ ӒӈҸӃҸӑҿӊҿӅ ӄ ӍҹӏҸӑӌӍࡏ int x = 37; int* px = &x; void* p = px; int* py = p; тӄӌӒӑӈҸ ӔӄӊӍӐӍӔӄӄࡇ ࢀпҸӈӍҺҸ ӎӏӄӏӍҾҸ ʲȩǞƇࡐࢁ ࡴ ӐӎӏӍӐӄӊ ӒӖӄӑҿӊӛࡈ ࡊ ߞߠ
  44. бӝҾҿӊҿӌӄҿ ҹӊӍӈҸ ӎҸӋӨӑӄ void* malloc(size_t size); ыӒӌӈӗӄӨ ҺӝҾҿӊӨҿӑ ҹӊӍӈ ӎҸӋӨӑӄ

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

    жӐӊӄ ptr ӏҸҺҿӌ NULLࡈ ӌӄӖҿһӍ ӌҿ ҾҿӊҸҿӑࡏ хӍӐӊҿ ҺӝӃӍҺҸ ӃӌҸӖҿӌӄҿ ӒӈҸӃҸӑҿӊӨ ptr ӍӐӑҸҿӑӐӨ ӎӏҿӂӌӄӋࡈ ӌӍ ӏҸӃӝӋҿӌӍҺӝҺҸӑӛ ҿһӍ ӌҿӊӛӃӨࡏ уҿӄӐӎӍӊӛӃӒҿӋӒӧ ӎҸӋӨӑӛ ӌӒӂӌӍ ӍҹӨӃҸӑҿӊӛӌӍ ӍӐҺӍҹӍӂҾҸӑӛࡈ ӄӌҸӖҿ ӏҸӌӍ ӄӊӄ ӎӍӃҾӌӍ ӍӌҸ ӋӍӂҿӑ ӈӍӌӖӄӑӛӐӨ ࡫ӒӑҿӖӈҸ ӎҸӋӨӑӄ࡬ࡏ ߞߢ
  46. хӏӄӋҿӏࡇ ӋҸӐӐӄҺ ҾӄӌҸӋӄӖҿӐӈӍһӍ ӏҸӃӋҿӏҸ ࡏ int n = 5; int*

    p; // ??? p = malloc(n * sizeof(int)); if (p == NULL) { /* error */ } ߞߣ
  47. хӏӄӋҿӏࡇ ӋҸӐӐӄҺ ҾӄӌҸӋӄӖҿӐӈӍһӍ ӏҸӃӋҿӏҸ ࡏ ࡏ ࡏ ࡏ ࡏ ࡏ

    ࡏ ࡐࡐࡐ ࡏ ࡐࡐࡐ ࡏ 0x100 ࡏ 0x113 ࡏ ࡐࡐࡐ ࡏ ࡐࡐࡐ ࡏ ࡐࡐࡐ int n = 5; int* p; // 0x100 p = malloc(n * sizeof(int)); if (p == NULL) { /* error */ } ߞߣ
  48. хӏӄӋҿӏࡇ ӋҸӐӐӄҺ ҾӄӌҸӋӄӖҿӐӈӍһӍ ӏҸӃӋҿӏҸ ࡏ ࡏ ࡏ ࡏ ࡏ ࡏ

    ࡏ ࡐࡐࡐ ࡏ ࡐࡐࡐ ࡏ 0x100 ࡏ 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; ߞߣ
  49. хӏӄӋҿӏࡇ ӋҸӐӐӄҺ ҾӄӌҸӋӄӖҿӐӈӍһӍ ӏҸӃӋҿӏҸ ࡏ ࡏ ࡏ ࡏ ࡏ ࡏ

    ࡏ ࡐࡐࡐ ࡏ ࡐࡐࡐ ࡏ 0x100 ࡏ 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; ߞߣ
  50. хӏӄӋҿӏࡇ ӋҸӐӐӄҺ ҾӄӌҸӋӄӖҿӐӈӍһӍ ӏҸӃӋҿӏҸ ࡏ ࡏ ࡏ ࡏ ࡏ ࡏ

    ࡏ ࡐࡐࡐ ࡏ ࡐࡐࡐ ࡏ 0x100 ࡏ 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); ߞߣ
  51. хӏӄӋҿӏࡇ ӋҸӐӐӄҺ ҾӄӌҸӋӄӖҿӐӈӍһӍ ӏҸӃӋҿӏҸ ࡏ ࡏ 0x100 ࡏ 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); ߞߣ
  52. лӃӋҿӌҿӌӄҿ ӏҸӃӋҿӏҸ ҹӊӍӈҸ ӎҸӋӨӑӄ void* realloc(void* ptr, size_t size); ыӒӌӈӗӄӨ

    ӄӃӋҿӌӨҿӑ ӏҸӃӋҿӏ ҹӊӍӈҸ ӎҸӋӨӑӄ ҾӍ size ҹҸӅӑࡏ б ӐӊӒӖҸҿ ӒӐӎҿӘӌӍһӍ ӄӃӋҿӌҿӌӄӨ ӏҸӃӋҿӏҸ ҺӍӃҺӏҸәҸҿӑ ӒӈҸӃҸӑҿӊӛ ӌҸ ӌҸӖҸӊӍ ҹӊӍӈҸࡈ ӄӌҸӖҿ NULLࡏ ыӒӌӈӗӄӨ ӋӍӂҿӑ ӈҸӈ ӒӋҿӌӛӘҸӑӛ ӏҸӃӋҿӏࡈ ӑҸӈ ӄ ӒҺҿӊӄӖӄҺҸӑӛࡏ бӍӃӋӍӂӌӍ ӎҿӏҿӋҿәҿӌӄҿ ӐӍҾҿӏӂӄӋӍһӍ ӎҸӋӨӑӄࡈ ӎӏӄ ӢӑӍӋ ҺӍӃҺӏҸәҸҿӑӐӨ ӒӈҸӃҸӑҿӊӛ ӌҸ ӌӍҺӍҿ ӋҿӐӑӍӏҸӐӎӍӊӍӂҿӌӄҿࡏ б ӐӊӒӖҸҿ ӌҿӒӐӎҿӘӌӍһӍ ӄӃӋҿӌҿӌӄӨ ӏҸӃӋҿӏҸࡈ ӄӃӌҸӖҸӊӛӌӝӅ ҹӊӍӈ ӎҸӋӨӑӄ ӌҿ ӍӐҺӍҹӍӂҾҸҿӑӐӨࡏ ߞߤ
  53. бӍӃҺӏҸәҿӌӄҿ ӋҸӐӐӄҺӍҺ ӄӃ ӔӒӌӈӗӄӅ 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); ߞߥ
  54. хӊӍӐӈӄҿ ҾӄӌҸӋӄӖҿӐӈӄҿ ҾҺӒӋҿӏӌӝҿ ӋҸӐӐӄҺӝ int* arr = malloc(n * m

    * sizeof(int)); if (arr == NULL) { /* error */ } // arr[i*m + j] = 37; free(arr); ߟߜ
  55. еӄӌҸӋӄӖҿӐӈӄҿ ӋҸӐӐӄҺӝ ӋҸӐӐӄҺӍҺࡇ ӄҾҿӨ ࡏ ࡏ ࡏ 0x20 ࡏ a[0]

    ࡏ ࡏ 0x24 ࡏ a[1] ࡏ 0x100 ࡏ 0x200 ࡏ ࡏ ࡏ 0x100 ࡏ a[0][0] ࡏ ࡏ 0x102 ࡏ a[0][1] ࡏ ࡏ 0x104 ࡏ a[0][2] ࡏ ࡏ 0x200 ࡏ a[1][0] ࡏ ࡏ 0x202 ࡏ a[1][1] ࡏ ࡏ 0x204 ࡏ a[1][2] ࡏ ߝߜ ࡏ ߞߜ ࡏ ߟߜ ࡏ ߠߜ ࡏ ߡߜ ࡏ ߢߜ short** a = /* 0x20 */; // {{10, 20, 30}, {40, 50, 60}} a[i][j] == *(*(a + i) + j) ߟߝ
  56. еӄӌҸӋӄӖҿӐӈӄҿ ҾҺӒӋҿӏӌӝҿ ӋҸӐӐӄҺӝ ӋҸӐӐӄҺӍҺ 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); ߟߞ