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

№ 2017/3. Динамическая память, указатели на фун...

№ 2017/3. Динамическая память, указатели на функции, символы, строки

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

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

Other Decks in Programming

Transcript

  1. ࡏ ࡏ щӈҸӃҸӑҿӊӛࡈ ӌӄӈӒҾҸ ӌҿ ӒӈҸӃӝҺҸӧәӄӅ NULL ࡴ ӐӎҿӗӄҸӊӛӌӍҿ ӈӍӌӐӑҸӌӑӌӍҿ

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

    ӒӈҸӃҸӑҿӊӨࡈ ӈӍӑӍӏӝӅ ӋӍӂҿӑ ӒӈҸӃӝҺҸӑӛ ӌҸ ӊӧҹӝҿ ҾҸӌӌӝҿ Һ ӎҸӋӨӑӄࡏ тӍӂҿӑ ҹӝӑӛ ӎӏӄҺҿҾҿӌ ӈ ӊӧҹӍӋӒ ҾӏӒһӍӋӒ ӑӄӎӒ ӒӈҸӃҸӑҿӊҿӅ ӄ ӍҹӏҸӑӌӍࡏ double x = 37; double* px = &x; void* p = px; int* py = p; шӊӒӘӗӎҾ ӚӊӐӓӖӓӚӊӊࡋ ࢂхҾӎӓӀҾ ӔӕӊӕӓӄҾ ʮȧǝƆࡓࢃ ࡶ ӖӔӕӓӖӊӐ ӘӜӊӗӅӐӡࡌ ࡎ http://thecodelesscode.com/case/5?lang=ru ߠ
  3. ࡏ ࡏ хӏҿӄӋӒәҿӐӑҺҸ ҾӄӌҸӋӄӖҿӐӈӍӅ ӎҸӋӨӑӄ ࡄ бӝҾҿӊӨҿӑӐӨ ӄ ӍӐҺӍҹӍӂҾҸҿӑӐӨ ҾӄӌҸӋӄӖҿӐӈӄ

    ӎӍ ӃҸӎӏӍӐӒ ӎӏӍһӏҸӋӋӝࡏ ࡄ цҸӃӋҿӏ ӃҸҾҸҿӑӐӨ ҾӄӌҸӋӄӖҿӐӈӄࡏ ߢ
  4. ࡏ ࡏ бӝҾҿӊҿӌӄҿ ҹӊӍӈҸ ӎҸӋӨӑӄ void* malloc(size_t size); ыӒӌӈӗӄӨ ҺӝҾҿӊӨҿӑ

    ҹӊӍӈ ӎҸӋӨӑӄ ӏҸӃӋҿӏӍӋ size ҹҸӅӑ ӄ ҺӍӃҺӏҸәҸҿӑ ӒӈҸӃҸӑҿӊӛ ӌҸ ӌҸӖҸӊӍ ҹӊӍӈҸࡏ б ӐӊӒӖҸҿࡈ ҿӐӊӄ ӎҸӋӨӑӛ ҺӝҾҿӊӄӑӛ ӌҿ ӎӍӊӒӖӄӊӍӐӛࡈ ҺӍӃҺӏҸәҸҿӑ NULLࡏ фҹӜӨҺӊҿӌҸ Һ ӃҸһӍӊӍҺӍӖӌӍӋ ӔҸӅӊҿ stdlib.hࡏ ߣ
  5. ࡏ ࡏ фӐҺӍҹӍӂҾҿӌӄҿ ҹӊӍӈҸ ӎҸӋӨӑӄ void free(void* ptr); ыӒӌӈӗӄӨ ӍӐҺӍҹӍӂҾҸҿӑ

    ҹӊӍӈ ӎҸӋӨӑӄࡏ жӐӊӄ ptr ӏҸҺҿӌ NULLࡈ ӌӄӖҿһӍ ӌҿ ҾҿӊҸҿӑࡏ фҹӜӨҺӊҿӌҸ Һ ӃҸһӍӊӍҺӍӖӌӍӋ ӔҸӅӊҿ stdlib.hࡏ хӍӐӊҿ ҺӝӃӍҺҸ ӃӌҸӖҿӌӄҿ ӒӈҸӃҸӑҿӊӨ ptr ӍӐӑҸҿӑӐӨ ӎӏҿӂӌӄӋࡈ ӌӍ ӏҸӃӝӋҿӌӍҺӝҺҸӑӛ ҿһӍ ӌҿӊӛӃӨࡏ уҿӄӐӎӍӊӛӃӒҿӋӒӧ ӎҸӋӨӑӛ ӌӒӂӌӍ ӍҹӨӃҸӑҿӊӛӌӍ ӍӐҺӍҹӍӂҾҸӑӛࡈ ӄӌҸӖҿ ӏҸӌӍ ӄӊӄ ӎӍӃҾӌӍ ӍӌҸ ӋӍӂҿӑ ӈӍӌӖӄӑӛӐӨ ࡫ӒӑҿӖӈҸ ӎҸӋӨӑӄ࡬ࡏ ߤ
  6. ࡏ ࡏ хӏӄӋҿӏࡇ ӋҸӐӐӄҺ ҾӄӌҸӋӄӖҿӐӈӍһӍ ӏҸӃӋҿӏҸ ࡏ ࡏ ࡏ ࡏ

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

    ࡏ ࡏ ࡏ ࡐࡐࡐ ࡏ ࡐࡐࡐ ࡏ 0x100 ࡏ 0x113 ࡏ ߟߣ ࡏ ߟߣ ࡏ ߟߣ int n = read_number(); // 5 int* p; // 0x100 p = malloc(n * sizeof(int)); if (p == NULL) { /* error */ } p[0] = p[n/2] = p[n-1] = 37; ߥ
  8. ࡏ ࡏ хӏӄӋҿӏࡇ ӋҸӐӐӄҺ ҾӄӌҸӋӄӖҿӐӈӍһӍ ӏҸӃӋҿӏҸ ࡏ ࡏ 0x100 ࡏ

    0x113 ࡏ ࡐࡐࡐ ࡏ ࡐࡐࡐ ࡏ ࡐࡐࡐ ࡏ ࡐࡐࡐ ࡏ ࡐࡐࡐ int n = read_number(); // 5 int* p; // 0x100 p = malloc(n * sizeof(int)); if (p == NULL) { /* error */ } p[0] = p[n/2] = p[n-1] = 37; free(p); ߥ
  9. ࡏ ࡏ ыӒӌӈӗӄӄ ӈҸӈ ҾҸӌӌӝҿ б ӨӃӝӈҿ + Ӑ ӔӒӌӈӗӄӨӋӄ

    ӋӍӂӌӍ ӏҸҹӍӑҸӑӛ ӈҸӈ Ӑ ҾҸӌӌӝӋӄࡇ double (*my_func)(double, double) = pow; double x = my_func(3, 2); // x = 9.0 ыӒӌӈӗӄӨ ࡴ ӢӑӍ ӌҸҹӍӏ ҹҸӅӑӍҺ Һ ӎҸӋӨӑӄࡈ ӈӍҾӄӏӒӧәӄӕ ӑҿӊӍ ӢӑӍӅ ӔӒӌӈӗӄӄ Ӑ ӎӍӋӍәӛӧ ӋҸӘӄӌӌӝӕ ӈӍӋҸӌҾࡏ кӌҸӖӄӑࡈ ӋӍӂӌӍ ҺӃӨӑӛ ҸҾӏҿӐ ӢӑӍһӍ ࢀӌҸҹӍӏҸ ҹҸӅӑӍҺࢁࡏ аӍӊҿҿ ӔӍӏӋҸӊӛӌӍࡇ double (*my_func)(double, double) = &pow; double x = (*my_func)(3, 2); // x = 9.0 ߝߝ
  10. ࡏ ࡏ хӏӄӋҿӏ ӖӄӐӊҿӌӌӍһӍ ҺӝӖӄӐӊҿӌӄӨ ӎӏӍӄӃҺӍҾӌӍӅ Һ ӑӍӖӈҿ double diff(double

    x, double (*f)(double)) { double dx = 0.01; return (f(x + dx) - f(x)) / dx; } double square(double x) { return x * x; } printf("%g\n", diff(M_PI/3, sin)); // 0.495662 printf("%g\n", diff(M_PI/6, cos)); // -0.504322 printf("%g\n", diff(3, square)); // 6.01 ߝߞ
  11. ࡏ ࡏ чӄӋҺӍӊӝ Һ + чӄӋҺӍӊӝ ӃҸҾҸӧӑӐӨ Һ ӍҾӄӌҸӏӌӝӕ ӈҸҺӝӖӈҸӕࡈ

    ӕӏҸӌӨӑӐӨ Һ ӎҿӏҿӋҿӌӌӝӕ ӑӄӎҸ charࡏ char x = 'H'; char y = 'i'; printf("%c%c\n", x, y); // Hi ߝߠ
  12. ࡏ ࡏ пӍҾӄӏӍҺӈӄ пӍҾӄӏӍҺӈҸ ࡴ ӐӍӍӑҺҿӑӐӑҺӄҿ ӋҿӂҾӒ ӐӄӋҺӍӊӍӋ ӄ ӗҿӊӍӖӄӐӊҿӌӌӝӋ

    ӈӍҾӍӋࡏ ë+vv ࡴ ӔӒӌҾҸӋҿӌӑҸӊӛӌҸӨ ߣࡷҹӄӑӌҸӨ ӈӍҾӄӏӍҺӈҸࡇ ࡄ ߟߞ ӒӎӏҸҺӊӨӧәӄӕ ӐӄӋҺӍӊҸࡈ ࡄ '\0'ࡈ ӈӍҾ ߜ ࡴ ӎӒӐӑӍӅ ӐӄӋҺӍӊࡈ ࡄ '\t'ࡈ ӈӍҾ ߥ ࡴ ӑҸҹӒӊӨӗӄӨࡈ ࡄ '\n'ࡈ ӈӍҾ ߝߜ ࡴ ӎҿӏҿҺӍҾ ӐӑӏӍӈӄࡈ ࡄ ࡊ ࡄ ߥߢ ӄӌӔӍӏӋҸӗӄӍӌӌӝӕ ӐӄӋҺӍӊӍҺࡏ ࡄ '0'..'9' ࡴ ӗӄӔӏӝࡈ ࡄ 'a'..'z'ࡈ 'A'..'Z' ࡴ ҹӒӈҺӝࡈ ࡄ ',', '?', ... ࡴ ӎӒӌӈӑӒҸӗӄӨࡏ ࡄ ࡊ ߝߡ
  13. ࡏ ࡏ чӄӋҺӍӊӝ ӈҸӈ ӖӄӐӊҸ char a = 'X'; printf("%c\n",

    a); // X printf("%d\n", a); // 88 char b = a + 1; printf("%c\n", b); // Y ߝߢ
  14. ࡏ ࡏ уҸӗӄӍӌҸӊӛӌӝҿ ӐӄӋҺӍӊӝ Я ӖӑӍ ҾҿӊҸӑӛࡈ ҿӐӊӄ ӕӍӖҿӑӐӨ ӄӐӎӍӊӛӃӍҺҸӑӛ

    ӈӄӏӄӊӊӄӗӒࡈ ӒӋӊӨӒӑӝ ӄ ӄҿӏӍһӊӄӔӝࡐ ߤࡷҹӄӑӌӝҿ ӈӄӏӄӊӊӄӖҿӐӈӄҿ ӈӍҾӄӏӍҺӈӄࡈ ҹҸӃӄӏӒӧәӄҿӐӨ ӌҸ ë+vvࡇ пфлࡷߤࡈ ĦǞȘƇȩʶɯࡷߝߞߡߝࡈ ࡊ уҸ ӎӏҸӈӑӄӈҿ ӈҸӈࡷӑӍ ӑҸӈࡇ ӄӘॏӧӜӏӚ ॊӝӕӜөॏӘӡӘӜӏӣӘӚ ߝߣ
  15. ࡏ ࡏ ўӌӄӈӍҾ ўӌӄӈӍҾ ࡴ ӋӌӍһӍҹҸӅӑӍҺҸӨ ӈӍҾӄӏӍҺӈҸࡈ ӎӍӈӏӝҺҸӧәҸӨ ӎӍӖӑӄ ҺӐҿ

    ӎӄӐӛӋҿӌӌӝҿ ӨӃӝӈӄࡏ хӍӃҺӍӊӨҿӑ ӈӍҾӄӏӍҺҸӑӛ ߝ ߝߝߞ ߜߢߠ ӐӄӋҺӍӊӍҺࡏ б ҺҿӏӐӄӄ ߥࡏߜࡈ ӄӧӌӛ ߞߜߝߢ һࡏࡈ ӄӐӎӍӊӛӃӒҿӑӐӨ ӊӄӘӛ ߝߞߤ ߞߟߣࡏ чӍҾҿӏӂӄӑ ҺӐҿ ӌҸӗӄӍӌҸӊӛӌӝҿ ӐӄӋҺӍӊӝࡈ ӋҸӑҿӋҸӑӄӖҿӐӈӄҿ ӐӄӋҺӍӊӝࡈ ӐӄӋҺӍӊӝ ҾӏҿҺӌӄӕ ӎӄӐӛӋҿӌӌӍӐӑҿӅ ӄ ӋӌӍһӍҿ ҾӏӒһӍҿࡈ ҺӈӊӧӖҸӨ ☺ ☹ ӄ ҾҸӂҿ ࡋ ߝߤ
  16. ࡏ ࡏ чӑӏӍӈӄ чӑӏӍӈҸ ࡴ ӢӑӍ ӌҸҹӍӏ ӐӄӋҺӍӊӍҺࡏ б ӨӃӝӈҿ

    + ӐӑӏӍӈӄ ӎӏҿҾӐӑҸҺӊӨӧӑӐӨ Һ ҺӄҾҿ ӋҸӐӐӄҺӍҺ ӐӄӋҺӍӊӍҺ ӑӄӎҸ charࡏ пӍӌӐӑҸӌӑӌӝҿ ӐӑӏӍӈӄ ӃҸҾҸӧӑӐӨ Һ ҾҺӍӅӌӝӕ ӈҸҺӝӖӈҸӕࡏ еӊӄӌҸ ӐӑӏӍӈӄ ӨҺӌӍ ӌҿ ӕӏҸӌӄӑӐӨࡇ ӎӍӐӊҿ ӎӍӐӊҿҾӌҿһӍ ӐӄӋҺӍӊҸ ӐӑӏӍӈӄ ӕӏҸӌӄӑӐӨ ӐӎҿӗӄҸӊӛӌӝӅ ӐӄӋҺӍӊ '\0'ࡏ char* str = "Hi"; printf("%s\n", str); // Hi printf("%d - %c\n", str[0], str[0]); // 72 - H printf("%d - %c\n", str[1], str[1]); // 105 - i printf("%d - %c\n", str[2], str[2]); // 0 - ߞߜ
  17. ࡏ ࡏ шӍӌӈӍӐӑӛ Ӑ ӄӌӄӗӄҸӊӄӃҸӗӄҿӅ ӐӑӏӍӈ ࡄ test ࡴ ӒӈҸӃҸӑҿӊӛ

    ӌҸ ӈӍӌӐӑҸӌӑӌӒӧ ӐӑӏӍӈӒࡏ char* test = "cat"; test[2] = 'r'; // run time error ࡄ test ࡴ ӋҸӐӐӄҺࡈ ӎӏӍӄӌӄӗӄҸӊӄӃӄӏӍҺҸӌӌӝӅ ӈӍӌӐӑҸӌӑӌӍӅ ӐӑӏӍӈӍӅࡏ char test[] = "cat"; test[2] = 'r'; printf("%s\n", test); // car ߞߝ