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

ゼロからわかるポインター

Aruneko
January 26, 2018

 ゼロからわかるポインター

SAMIT 18.01で発表したC言語のポインタに関する資料です

Aruneko

January 26, 2018
Tweet

More Decks by Aruneko

Other Decks in Programming

Transcript

  1. θϩ͔ΒΘ͔Δ
    ϙΠϯλʔ
    ARUNEKO @ SAMIT 18.01

    View full-size slide

  2. ͸͡Ίʹ
    ॾ஫ҙ
    ▸ جຊతͳ಺༰͔ΒԠ༻·ͰऔΓѻ͍·͢
    ▸ Θ͔Βͳ͍Օॴ͸ద࣭ٓ໰͍ͯͩ͘͠͞
    ▸ ͪ͜Β΋Ͳ͜·ͰΘ͔͔࣭ͬͨ໰͠ͳ͕ΒਐΊ·͢
    ▸ ؾָʹ΍Γ·͠ΐ͏
    ▸ खݩʹCίϯύΠϥΛ༻ҙ͍ͯͩ͘͠͞
    ▸ ͳ͍ਓ͸ϒϥ΢βͰʮWandboxʯΛάά͓͍͍ͬͯͯͩ͘͞

    View full-size slide

  3. ͸͡Ίʹ
    ͜ͷεϥΠυͰ఻͍͑ͨ͜ͱ
    ▸ ϙΠϯλ͸༷ʑͳཁૉ͕བྷΈ߹͍ͬͯͯɺཧղΛ೉͍ͤͯ͘͠͞·͢
    ▸ ͦΕΛҰͭҰͭ΄Ͳ͍͍͖ͯ·͢
    ▸ ͱΓ͋͑ͣश͖ͬͯͨ͜ͱ͸๨Εͯθϩ͔Β࢝Ί·͠ΐ͏ʂ

    View full-size slide

  4. ϊΠϚϯܕ

    ίϯϐϡʔλ

    View full-size slide

  5. ϊΠϚϯܕίϯϐϡʔλ
    ·ͣ͸Ξϯέʔτ
    ▸ ϊΠϚϯܕίϯϐϡʔλͬͯ஌ͬͯ·͔͢ʁ

    View full-size slide

  6. ϊΠϚϯܕίϯϐϡʔλ
    ϊΠϚϯܕίϯϐϡʔλͷͬ͘͟Γͨ͠঺հ
    ▸ νϡʔϦϯάϚγϯΛՄೳͳൣғͰ࣮ݱͨ͠΋ͷ
    ▸ νϡʔϦϯάϚγϯ͸ҎԼͷ3ཁૉ͔ΒͰ͖͍ͯΔ
    ▸ 1. ه߸Λॻ͖ࠐΊΔແݶͷ௕͕͋͞Δςʔϓ
    ▸ 2. ςʔϓΛಡΈॻ͖͢Δϔου
    ▸ 3. ಡΈॻ͖ͨ͠ΓςʔϓΛಈ͔ͨ͠Γ͢ΔػߏΛ੍ޚ͢Δ༗ݶΦʔτϚτϯ
    ςʔϓ
    ϔου
    ༗ݶΦʔτϚτϯ

    View full-size slide

  7. ϊΠϚϯܕίϯϐϡʔλ
    νϡʔϦϯάϚγϯͱͷରԠ
    ▸ ςʔϓ → هԱ૷ஔ(CPUΩϟογϡ΍RAMͳͲ)
    ▸ ϔουͱ༗ݶΦʔτϚτϯ → CPU
    ▸ (ͦΕΒΛ઀ଓ͢Δόε)

    View full-size slide

  8. ϊΠϚϯܕίϯϐϡʔλ
    ςʔϓ(هԱ૷ஔ)্ͷҐஔΛಛఆ͢Δʹ͸ʁ
    ▸ σʔλΛςʔϓʹॻ͖ࠐΜͩΓɺಡΈࠐΜͩΓ͍ͨ͠
    ▸ Ͳ͏΍ͬͯςʔϓ্ͷҐஔΛಛఆ͢Δʁ
    ▸ ΞυϨεΛৼΔ
    ▸ ͜ͷΞυϨεΛCݴޠ্Ͱѻ͏ํ๏͕"ϙΠϯλ"
    10൪஍ 20൪஍
    ... n൪஍
    ...

    View full-size slide

  9. ϊΠϚϯܕίϯϐϡʔλ
    ͜͜·Ͱͷ·ͱΊ
    ▸ ϊΠϚϯܕίϯϐϡʔλ͸νϡʔϦϯάϚγϯͷ࣮૷ͷͻͱͭ
    ▸ νϡʔϦϯάϚγϯʹ͸ςʔϓͱϔομ͕͋Δ
    ▸ ςʔϓ(ϝϞϦ)ͷҐஔΛಛఆ͢ΔͨΊʹ͸ΞυϨε͕ඞཁ
    ▸ CݴޠͰΞυϨεΛѻ͏ͨΊͷखஈ͕ϙΠϯλ

    View full-size slide

  10. ϏοτͱόΠτ

    View full-size slide

  11. ϏοτͱόΠτ
    Ϗοτ
    ▸ ίϯϐϡʔλ಺෦Ͱ͸2ਐ਺ͰܭࢉΛߦ͍ͬͯΔ
    ▸ 2ਐ਺ͷ1έλΛbitͱݺͿ
    ▸ ྫ͑͹ʮ10100010ʯ͸8bits
    1 0 1 0 0 0 1 0
    1ͭͰ1bit
    શ෦Ͱ8bit

    View full-size slide

  12. ϏοτͱόΠτ
    όΠτ
    ▸ ಛʹ໌ࣔ͠ͳ͍৔߹1byte=8bits
    ▸ ͜ͷεϥΠυͰ΋͜ͷఆٛʹै͍·͢
    ▸ ੲ͸1byte͕5bits, 6bits, 7bits, 9bits, 12bitsͳͲͷఆٛ΋͋ͬͨ
    1 0 1 0 0 0 1 0
    શ෦Ͱ1byte

    View full-size slide

  13. ϏοτͱόΠτ
    όΠτͱϝϞϦۭؒ
    ▸ ϝϞϦ্ۭؒͷΞυϨε͸1byte͝ͱʹΞΫηεͰ͖ΔΑ͏ʹઃܭ͞Ε͍ͯΔ
    ▸ ීஈ͸ΞυϨεΛಡΈ΍͍͢Α͏ʹ16ਐ਺Ͱදه
    ▸ جຊతʹϚγϯͷbit਺෼͚ͩϝϞϦۭؒΛ༻ҙͰ͖Δ
    1 0 1 0 0 0 1 0
    00൪஍ 01൪஍ ... 1A൪஍ 1B൪஍

    View full-size slide

  14. ϏοτͱόΠτ
    ͜͜·Ͱͷ·ͱΊ
    ▸ bit͕Θ͔ͬͨ
    ▸ ͍͍ͩͨͷ৔߹1byte=8bits
    ▸ ϝϞϦ্ۭؒͰ͸
    ▸ 1byte͝ͱʹΞυϨε͕ৼΒΕ͍ͯΔ
    ▸ ීஈ͸ಡΈ΍͍͢Α͏ʹ16ਐ਺Ͱදه

    View full-size slide

  15. ม਺ͱܕ
    ΠϯλϏϡʔ
    ▸ Cݴޠʹ͓͚Δม਺ͱ͸ԿͰ͠ΐ͏͔ʁ
    ▸ ಉ͘͡ܕͱ͸ԿͰ͠ΐ͏͔ʁ

    View full-size slide

  16. ม਺ͱܕ
    ม਺ͱ͸
    ▸ ϝϞϦ্ͷҐஔ(ΞυϨε)Λࣔ͢ϥϕϧ
    ▸ ม਺୯ମͩͱʮσʔλͷઌ಄ΞυϨεʯ͔͠Θ͔Βͳ͍
    ▸ ʮܕʯΛิ׬৘ใʹ࢖͏
    00൪஍ 01൪஍ ... 1A൪஍ 1B൪஍
    ม਺: a ม਺: b

    View full-size slide

  17. ม਺ͱܕ
    ܕͱ͸
    ▸ σʔλͷ௕͞ͱղऍํ๏Λࣔͨ͠ϥϕϧ
    ▸ intͱ͔charͱ͔voidͱ͔
    ▸ int͸࠷௿2bytesҎ্ͷූ߸෇͖੔਺Λද͢ܕ
    ▸ ᐆດͳఆٛͷ͍ͤͰॲཧܥʹΑͬͯintͷαΠζ͸·ͪ·ͪͳͷͰ஫ҙ
    ▸ char͸1byteͰจࣈ(ASCII)Λද͢ܕ
    ▸ void͸௕͞ͱղऍํ๏Λ࣋ͨͳ͍͜ͱΛࣔ͢ܕ
    ▸ σʔλͷऴ୺͕Θ͔Βͳ͘ͳΔͨΊvoidܕม਺͸એݴͰ͖ͳ͍

    View full-size slide

  18. ม਺ͱܕ
    ม਺ΛಡΉ
    ▸ ʮint a = 1ʯͷ৔߹
    ▸ ϝϞϦ্ʹ͋Δʮ(0x31ݸ)1ʯͱ͍͏σʔλͷઌ಄ʹʮaʯͱ͍͏ϥϕϧΛషΔ
    ▸ ͦͷσʔλ͸ɺintͷαΠζ෼͚ͩޙΖʹଓ͍͍ͯΔ
    ▸ ͦͷσʔλ͸ɺූ߸෇͖੔਺஋ͱͯ͠ղऍ͢Δ
    ▸ ʮchar b = '0'ʯͷ৔߹
    ▸ ϝϞϦ্ͷʮ00110000ʯͱ͍͏σʔλͷઌ಄ʹϥϕϧʮbʯΛషΔ
    ▸ ͦͷσʔλ͸ɺcharͷαΠζ(1byte)෼͚ͩޙΖʹଓ͍͍ͯΔ
    ▸ ͦͷσʔλ͸ɺASCIIίʔυʹैͬͯղऍ͢Δ

    View full-size slide

  19. ม਺ͱܕ
    ԋशɿղऍͷҧ͍Λମײ͢Δ
    ▸ ࣍ͷ2ਐ਺஋ΛintܕͱcharܕͦΕͧΕͰղऍͯ͠ΈΑ͏
    ▸ 01000101

    View full-size slide

  20. ม਺ͱܕ
    ԋशɿܕͷαΠζΛௐ΂ͯΈΑ͏
    ▸ short, int, long, float, double, charͷαΠζΛௐ΂ͯΈΑ͏
    ▸ sizeofԋࢉࢠͰ΍ͬͯΈΔ

    View full-size slide

  21. ม਺ͱܕ
    ม਺ͷαΠζΛௐ΂ͯΈΔ
    #include
    int main(void) {
    printf("%lu\n", sizeof(short));
    printf("%lu\n", sizeof(int));
    printf("%lu\n", sizeof(long));
    printf("%lu\n", sizeof(float));
    printf("%lu\n", sizeof(double));
    printf("%lu\n", sizeof(char));
    return 0;
    }

    View full-size slide

  22. ม਺ͱܕ
    ม਺ͷઌ಄ΞυϨεΛ֬ೝͯ͠ΈΔ
    ▸ ࣍ͷϓϩάϥϜΛ࣮ߦͯ͠ΈΑ͏
    ▸ ͍Ζ͍ΖͳαΠζͷม਺Ͱ࣮ݧ
    ▸ ม਺ͷઌ಄ΞυϨε͸&ͰऔಘͰ͖Δ
    #include
    int main(void) {
    int a = 5;
    char b = '0';
    double c = 5.3;
    printf("%p\n", &a);
    printf("%p\n", &b);
    printf("%p\n", &c);
    return 0;
    }

    View full-size slide

  23. ม਺ͱܕ
    ม਺ͷઌ಄ΞυϨεΛ֬ೝͯ͠ΈΔ
    ▸ ࣍ͷ఺ʹ஫໨ͯ͠ΈΑ͏
    ▸ Կճ͔࣮ߦͯ͠ΈͯɺΞυϨε͕Ͳ͏ͳΔ͔ݟͯΈΔ
    ▸ ͦΕͧΕͷม਺͸ԿόΠτͣͭ཭Ε͍ͯΔͩΖ͏͔
    ▸ ม਺ͷએݴॱͱΞυϨεͷେখؔ܎͸Ͳ͏ͳ͍ͬͯΔͩΖ͏͔

    View full-size slide

  24. ม਺ͱܕ
    ͜͜·Ͱͷ·ͱΊ
    ▸ ม਺͸ϝϞϦ্ͷྖҬͷઌ಄Λࣔ͢ϥϕϧ
    ▸ ܕ͸σʔλͷ௕͞ͱղऍํ๏Λද͢ϥϕϧ
    ▸ sizeofԋࢉࢠͰܕͷαΠζΛ֬ೝͰ͖Δ
    ▸ &Ͱม਺ͷઌ಄ΞυϨεΛऔಘͰ͖Δ
    ▸ ม਺ͷΞυϨε͸ຖճมΘΔ
    ▸ ม਺͸͖ͬͪΓ٧·ͬͯྖҬ͕֬อ͞ΕΔΘ͚Ͱ͸ͳ͍
    ▸ ม਺ͷྖҬ͸એݴॱͱٯʹ֬อ͞Ε͍ͯΔ

    View full-size slide

  25. ഑ྻ
    ഑ྻͷ͓͞Β͍
    ▸ ಛఆͷܕͷม਺Λฒ΂͓͚ͯ࣋ͬͯΔ
    ▸ ҎԼͷ഑ྻએݴʹ͓͚ΔσʔλͷαΠζͱղऍํ๏͸ԿͩΖ͏ʁ
    ▸ 1ݸ෼ͷαΠζʹ͍ͭͯ΋͍ͭͰʹௐ΂ͯΈΑ͏
    #include
    int main(void) {
    int a[5] = {0, 1, 2, 3, 4};
    printf("%lu, %lu\n", sizeof(a), sizeof(a[0]));
    printf("%p\n", a);
    return 0;
    }

    View full-size slide

  26. ഑ྻ
    ഑ྻܕͷαΠζͱղऍํ๏
    ▸ ഑ྻܕͷαΠζ͸ʮܕͷαΠζ×ཁૉ਺ ʯ
    ▸ ഑ྻͷऴ୺Λ໌ࣔ͢ΔͨΊ
    ▸ ऴ୺͕Θ͔Βͳ͍ม਺͸એݴͰ͖ͳ͍ʂ
    ▸ ഑ྻܕ͸ʮϝϞϦ্ͷΞυϨεʯͱͯ͠ղऍ͞ΕΔ
    ▸ ϝϞϦ্ͷΞυϨεΛૢ࡞͢Δํ๏͸ޙ΄Ͳ঺հ͠·͢

    View full-size slide

  27. ഑ྻ
    ཁૉ͝ͱͷΞυϨε
    ▸ ཁૉ͝ͱͷΞυϨε͕ͪΌΜͱ٧·͍ͬͯΔ͔֬ೝͯ͠ΈΔ
    #include
    int main(void) {
    int a[3] = {0, 1, 2};
    printf("%p, %p, %p\n", &a[0], &a[1], &a[2]);
    return 0;
    }

    View full-size slide

  28. ഑ྻ
    ͜͜·Ͱͷ·ͱΊ
    ▸ ഑ྻܕͷαΠζ͸ܕͷαΠζ×ཁૉ਺
    ▸ ഑ྻܕͷղऍํ๏͸ΞυϨε
    ▸ ഑ྻ͸ී௨ͷม਺ͱҧ͖ͬͯͪΜͱ٧·ͬͯྖҬ͕֬อ͞ΕΔ

    View full-size slide

  29. ϙΠϯλܕม਺

    View full-size slide

  30. ϙΠϯλܕม਺
    ϙΠϯλܕม਺ʁ
    ▸ ූ߸෇͖੔਺͸intܕ
    ▸ ഒਫ਼౓ුಈখ਺఺਺͸doubleܕ
    ▸ ASCIIͷ1จࣈ͸charܕ
    ▸ Ͱ͸ϝϞϦ্ͷΞυϨεΛ֨ೲ͢Δܕ͸ʁ
    ▸ ͦΕ͕ͦ͜ϙΠϯλܕͰ͢

    View full-size slide

  31. ϙΠϯλܕม਺
    ϙΠϯλܕม਺ͷએݴ
    ▸ ม਺એݴ࣌ʹɺม਺໊ͷલʹʮ*ʯΛ͚ͭΔ͜ͱͰϙΠϯλܕʹͳΔ
    ▸ ʮchar *aʯ͸ʮcharϙΠϯλܕʯ
    ▸ ࣍ͷએݴ͸ͦΕͧΕԿܕͰ͠ΐ͏͔ʁ
    ▸ int *a, *b, c;
    ▸ Θ͔ΓͮΒ͘ͳΔͨΊએݴ͸෼཭ͨ͠ํ͕ݟ΍͍͔͢΋

    View full-size slide

  32. ϙΠϯλܕม਺
    ϙΠϯλܕม਺ͷҙຯ
    ▸ ϙΠϯλܕม਺ʹ͸ϝϞϦ্ͷྖҬͷઌ಄ΞυϨε͕֨ೲ͞Ε͍ͯΔ
    ▸ xxxϙΠϯλܕม਺ͷxxx͸ɺͦͷΞυϨεΛԿܕͰղऍ͢Δ͔Λࣔ͢

    View full-size slide

  33. ϙΠϯλܕม਺
    ϙΠϯλܕม਺ͷ࣋ͭ஋
    ▸ ϙΠϯλܕม਺ʹΞυϨε͕֨ೲͰ͖Δ͜ͱΛ֬ೝ͢Δ
    #include
    int main(void) {
    int i = 0;
    int *p = &i;
    printf("%p\n", p);
    return 0;
    }

    View full-size slide

  34. ϙΠϯλܕม਺
    ϙΠϯλܕม਺ʹ֨ೲ͞Ε͍ͯΔΞυϨεͷத਎ΛಡΉ
    ▸ ϙΠϯλܕม਺ʹ֨ೲ͞Ε͍ͯΔΞυϨεͷத਎ΛಡΉʹ͸*Λ࢖͏
    #include
    int main(void) {
    int i = 0;
    int *p = &i;
    printf("%d\n", *p);
    return 0;
    }
    0 : int
    &i : int *
    ͔͜͜Β্ʹ4bytes෼ ->
    i
    p

    View full-size slide

  35. ϙΠϯλܕม਺
    ஫ҙ
    ▸ Cͷม਺͸ʮ࢖ΘΕํʯʹͦΖ͑ͯએݴ͢Δ
    ▸ int x͸ʮxΛ࢖͏ͱintܕʯ
    ▸ int *x͸ʮ*xΛ࢖͏ͱintܕʯ
    ▸ ʮxΛ࢖͏ͱintܕ஋͕ೖ͍ͬͯΔม਺ͷઌ಄ΞυϨεʯ

    View full-size slide

  36. ϙΠϯλܕม਺
    ࣮ݧ
    ▸ ܕͷղऍΛ͋͑ͯؒҧ͑ͯΈΔ
    ▸ ͳͥ͜Μͳ݁Ռʹͳ͔ͬͨߟ͑ͯΈΑ͏
    #include
    int main(void) {
    int i = 25185;
    char* p = &i;
    printf("%d\n", *p);
    printf("%c\n", *p);
    return 0;
    }
    25185 : int
    char

    View full-size slide

  37. ϙΠϯλܕม਺
    ͜͜·Ͱͷ·ͱΊ
    ▸ ม਺ͷΞυϨεΛ֨ೲ͢ΔͨΊʹɺϙΠϯλܕม਺͕͋Δ
    ▸ ϙΠϯλܕม਺͸ʮܕ *ม਺໊ʯͰએݴ͢Δ
    ▸ ϙΠϯλܕม਺͸ҎԼͷ2ཁૉΛ࣋ͭ
    ▸ ͋ΔϝϞϦ্ͷྖҬͷઌ಄ΞυϨε
    ▸ ͦͷྖҬΛԿܕͰղऍ͢Δ͔
    ▸ ϙΠϯλܕม਺ʹ֨ೲ͞Ε͍ͯΔΞυϨεͷத਎ΛಡΉʹ͸*Λ࢖͏

    View full-size slide

  38. ػցޠ͔Β

    ϙΠϯλΛ೷͘

    View full-size slide

  39. ػցޠ͔ΒϙΠϯλΛ೷͘
    ػցޠͱ͸
    ▸ CݴޠͷϓϩάϥϜ͸ίϯύΠϧ͞ΕΔͱػցޠʹ຋༁͞ΕΔ
    ▸ ػցޠ͸CPUͷछྨɺϏοτ਺ɺOSͳͲʹΑͬͯશ͘ҟͳΔ
    ▸ ࠓճ͸x86_64ͷELF(Executable and Linkable Format)Ͱ΍Γ·͢
    ▸ LinuxͳͲͰར༻͞Ε͍ͯΔ࣮ߦϑΝΠϧϑΥʔϚοτ
    ▸ ϙΠϯλͱ͸ͳΜͧ΍ͱ͍͏ͷ͕͍͍ͩͨػցޠͰઆ໌Ͱ͖·͢

    View full-size slide

  40. ػցޠ͔ΒϙΠϯλΛ೷͘
    αϯϓϧίʔυ
    ▸ ͜ͷίʔυͰ࣮ݧͯ͠ΈΑ͏
    #include
    int succ(int x) {
    int ans = x + 1;
    return ans;
    }
    int main(void) {
    int x = 5;
    int y = succ(x);
    printf("%d\n", y);
    return 0;
    }

    View full-size slide

  41. ػցޠ͔ΒϙΠϯλΛ೷͘
    ػցޠͷݟͨ໨
    ▸ ྫ͑͹succ͸͜Μͳײ͡
    ▸ ࠨ͔ΒɺϓϩάϥϜதͷ૬ରΞυϨεɺੜͷػցޠɺΦϖίʔυɺΦϖϥϯυ
    000000000000064a :
    64a: 55 push rbp
    64b: 48 89 e5 mov rbp,rsp
    64e: 89 7d ec mov DWORD PTR [rbp-0x14],edi
    651: 8b 45 ec mov eax,DWORD PTR [rbp-0x14]
    654: 83 c0 01 add eax,0x1
    657: 89 45 fc mov DWORD PTR [rbp-0x4],eax
    65a: 8b 45 fc mov eax,DWORD PTR [rbp-0x4]
    65d: 5d pop rbp
    65e: c3 ret

    View full-size slide

  42. ػցޠ͔ΒϙΠϯλΛ೷͘
    MAINؔ਺
    0000000000000659 :
    659: 55 push rbp
    65a: 48 89 e5 mov rbp,rsp
    65d: 48 83 ec 10 sub rsp,0x10
    661: c7 45 f8 05 00 00 00 mov DWORD PTR [rbp-0x8],0x5
    668: 8b 45 f8 mov eax,DWORD PTR [rbp-0x8]
    66b: 89 c7 mov edi,eax
    66d: e8 d8 ff ff ff call 64a
    672: 89 45 fc mov DWORD PTR [rbp-0x4],eax
    675: 8b 45 fc mov eax,DWORD PTR [rbp-0x4]
    678: 89 c6 mov esi,eax
    67a: 48 8d 3d a3 00 00 00 lea rdi,[rip+0xa3]
    681: b8 00 00 00 00 mov eax,0x0
    686: e8 95 fe ff ff call 520
    68b: b8 00 00 00 00 mov eax,0x0
    690: c9 leave
    691: c3 ret
    ؔ਺ʹೖΔॲཧ
    int x = 5;
    int y = succ(x);
    printf(“%d\n”, y);
    return 0;

    View full-size slide

  43. ػցޠ͔ΒϙΠϯλΛ೷͘
    ؔ਺ʹೖΔॲཧ
    ▸ ΋ͱ΋ͱͷϕʔεϙΠϯλ(rbp)ΛελοΫʹୀආͤ͞Δ
    ▸ ελοΫϙΠϯλ(rsp)Λrbpʹୀආͤ͞Δ
    ▸ ελοΫϙΠϯλΛ0x10(10ਐ਺Ͱ16)bytes෼Լ͛Δ
    ▸ ϩʔΧϧม਺ΛೖΕ͓ͯ͘ྖҬͷ֬อ
    659: 55 push rbp
    65a: 48 89 e5 mov rbp,rsp
    65d: 48 83 ec 10 sub rsp,0x10
    rbp →
    rsp →

    View full-size slide

  44. ػցޠ͔ΒϙΠϯλΛ೷͘
    ม਺΁ͷ୅ೖ
    ▸ 0x5ͱ͍͏஋ΛࢦఆՕॴʹίϐʔ
    ▸ ࢦఆՕॴ͸ʮrbp͔Β0x8ΛҾ͍ͨΞυϨεͷத਎ʯ
    ▸ ͔ͦ͜ΒDWORD(intܕ)෼ॻ͖ࠐΉ
    ▸ DWORD = WORD * 2 = BYTE * 2 * 2 = 4bytes
    ▸ ༨ͬͨྖҬ͸0ͰຒΊΔ
    661: c7 45 f8 05 00 00 00 mov DWORD PTR [rbp-0x8],0x5
    rbp →
    rsp →
    rbp - 0x8 →
    0x5
    DWORD

    View full-size slide

  45. ػցޠ͔ΒϙΠϯλΛ೷͘
    ؔ਺ݺͼग़͠
    ▸ rbp-0x8͔ΒDWORD෼த਎ΛಡΜͰeaxʹίϐʔ
    ▸ ୈ1Ҿ਺͸ediʹ٧ΊΔ
    ▸ call໋ྩͰݺͼग़͢
    ▸ ฦΓ஋͸eaxϨδελʹೖ͍ͬͯΔ
    668: 8b 45 f8 mov eax,DWORD PTR [rbp-0x8]
    66b: 89 c7 mov edi,eax
    66d: e8 d8 ff ff ff call 64a
    672: 89 45 fc mov DWORD PTR [rbp-0x4],eax
    rbp →
    rsp →
    rbp - 0x8 →
    rbp - 0x8 →
    rbp - 0x8 →
    rbp - 0x8 →
    0x5
    DWORD 0x6
    rbp - 0x4 →

    View full-size slide

  46. ػցޠ͔ΒϙΠϯλΛ೷͘
    ؔ਺࣮ߦ
    ▸ ΋͏ҰճrbpͷୀආͱrspͰͷ্ॻ͖Λ࣮ࢪ
    ▸ ͋ͱ͸ܭࢉͯ݁͠ՌΛeaxʹ٧ΊͯrbpΛ෮ݩ
    000000000000064a :
    64a: 55 push rbp
    64b: 48 89 e5 mov rbp,rsp
    64e: 89 7d ec mov DWORD PTR [rbp-0x14],edi
    651: 8b 45 ec mov eax,DWORD PTR [rbp-0x14]
    654: 83 c0 01 add eax,0x1
    657: 89 45 fc mov DWORD PTR [rbp-0x4],eax
    65a: 8b 45 fc mov eax,DWORD PTR [rbp-0x4]
    65d: 5d pop rbp
    65e: c3 ret
    mainͷrbp →
    succͷrbp →

    View full-size slide

  47. ػցޠ͔ΒϙΠϯλΛ೷͘
    දࣔॲཧ
    ▸ ม਺yΛeaxʹ٧Ίͯ
    ▸ ϑΥʔϚοτจࣈྻͷΞυϨεΛrdiʹ٧Ίͯ
    ▸ ΞυϨεͷઌʹ͸ʮ25 64 0a 00ʯ͕ೖ͍ͬͯΔ
    ▸ eaxΛϦηοτͯ͠
    ▸ printfΛ࣮ߦ
    675: 8b 45 fc mov eax,DWORD PTR [rbp-0x4]
    678: 89 c6 mov esi,eax
    67a: 48 8d 3d a3 00 00 00 lea rdi,[rip+0xa3]
    681: b8 00 00 00 00 mov eax,0x0
    686: e8 95 fe ff ff call 520

    View full-size slide

  48. ػցޠ͔ΒϙΠϯλΛ೷͘
    ͜͜·Ͱͷ·ͱΊ
    ▸ ػցޠʹ͸͍ΖΜͳछྨ͕͋Δ
    ▸ ػցޠͷதͰ͸ΞυϨε͕΍ΓͱΓ͞Ε͍ͯΔ
    ▸ ͦͷΞυϨεΛCݴޠͰѻ͏ΠϯλʔϑΣʔε͕ϙΠϯλ
    ▸ ܕ৘ใ͸όΠτ਺ʹஔ͖׵͑ΒΕ͍ͯͨ

    View full-size slide

  49. ഑ྻͱϙΠϯλ

    View full-size slide

  50. ഑ྻͱϙΠϯλ
    ഑ྻͱϙΠϯλͷؔ܎
    ▸ ഑ྻܕม਺ʹ͸௚઀஋͕ೖ͍ͬͯΔΘ͚Ͱ͸ͳ͍
    ▸ ഑ྻ͕ೖ͍ͬͯΔྖҬͷઌ಄ΞυϨεͷΈ͕֨ೲ͞Ε͍ͯΔ
    ▸ ഑ྻܕม਺͸࣍ͷ3ཁૉΛ͍࣋ͬͯΔ
    ▸ ഑ྻ͕ೖ͍ͬͯΔྖҬͷઌ಄ΞυϨε
    ▸ ͦͷ഑ྻ1ͭ෼ͷܕ
    ▸ ഑ྻશମͷαΠζ
    ▸ ΞυϨε͕֨ೲ͞Ε͍ͯΔ఺͸ϙΠϯλܕม਺ͱҰகͯ͠Δ

    View full-size slide

  51. ഑ྻͱϙΠϯλ
    ഑ྻͱϙΠϯλͷؔ܎
    ▸ ֨ೲ͞Ε͍ͯΔΞυϨεΛએݴ͞ΕͨܕͰಡΉʹ͸*Λ࢖͏
    ▸ ഑ྻܕม਺΋ΞυϨεΛ֨ೲ͍ͯ͠ΔͷͰಉ͡ࣄΛͯ͠ΈΔ
    #include
    int main(void) {
    int nums[5] = {0, 1, 2, 3, 4};
    printf("%d\n", *nums);
    return 0;
    }

    View full-size slide

  52. ഑ྻͱϙΠϯλ
    ഑ྻͱϙΠϯλͷؔ܎
    ▸ ഑ྻͷઌ಄ͷ஋ΛऔΓग़͢͜ͱ͕Ͱ͖ͨ
    ▸ Ͱ͸2൪໨Ҏ߱ͷ஋ΛऔΓग़͢ͱ͖͸ʁ

    View full-size slide

  53. ഑ྻͱϙΠϯλ
    ϙΠϯλͷΠϯΫϦϝϯτͱσΫϦϝϯτ
    ▸ ϙΠϯλ͸ઌ಄ΞυϨεͱܕ৘ใΛ͍࣋ͬͯΔͨΊ

    ܕ1ݸͷόΠτ਺෼ਐΊͨΓ໭ͨ͠ΓͰ͖Δ
    ▸ ਐΊΔͷΛΠϯΫϦϝϯτɺ໭͢ͷΛσΫϦϝϯτ
    ▸ nݸਐΜͩઌͷΞυϨε͸ʮ+ nʯͰද͢
    ↑ n = 0
    ↑ n = 1
    ↑ n = 2
    ↑ n = 3
    ↑ n = 5 ↑ n = 4

    View full-size slide

  54. ഑ྻͱϙΠϯλ
    ϙΠϯλͷΠϯΫϦϝϯτͱσΫϦϝϯτ
    ▸ ίʔυͰ͔֬ΊͯΈΔ
    ▸ ͨͩ͠ɺ഑ྻΛ͸Έग़͢Α͏ͳΞΫηεΛͨ͠৔߹Ͳ͏ͳΔ͔͸ෆ໌
    #include
    int main(void) {
    int nums[5] = {0, 1, 2, 3, 4};
    printf("%p\n", nums + 0);
    printf("%p\n", nums + 1);
    printf("%p\n", nums + 2);
    return 0;
    }

    View full-size slide

  55. ഑ྻͱϙΠϯλ
    ϙΠϯλͷΠϯΫϦϝϯτͱσΫϦϝϯτ
    ▸ ΠϯΫϦϝϯτͨ͠΋ͷ΋ΞυϨε
    ▸ ͱ͍͏͜ͱ͸ʮ*ʯͰத਎ʹΞΫηεͰ͖Δ
    ▸ nums[0]ͱ͍͏දه͸*(nums + 0)ͱಉٛ
    #include
    int main(void) {
    int nums[5] = {0, 1, 2, 3, 4};
    printf("%d\n", *(nums + 0));
    printf("%d\n", *(nums + 1));
    printf("%d\n", *(nums + 2));
    return 0;
    }

    View full-size slide

  56. ഑ྻͱϙΠϯλ
    ͜͜·Ͱͷ·ͱΊ
    ▸ ഑ྻܕม਺ʹ͸ઌ಄ΞυϨε͕֨ೲ͞Ε͍ͯΔɻ
    ▸ ഑ྻܕม਺͸1ͭ෼ͷܕͱશମͷαΠζ΋อ͍࣋ͯ͠Δ
    ▸ ϙΠϯλ͸ΠϯΫϦϝϯτͨ͠ΓσΫϦϝϯτͨ͠ΓͰ͖Δ
    ▸ nums[0]ͱ͍͏දه͸*(nums + 0)ͱಉٛ

    View full-size slide

  57. ؔ਺ͷҾ਺ͱͯ͠ͷ

    ϙΠϯλ

    View full-size slide

  58. ؔ਺ͷҾ਺ͱͯ͠ͷϙΠϯλ
    ී௨ͷม਺ͷ৔߹
    ▸ ࢖͍͍ͨܕΛهड़ͯ͠౉͚ͩ͢
    #include
    void put_address(int num) {
    printf("%p\n", &num);
    }
    int main(void) {
    int x = 0;
    printf("%p\n", &x);
    put_address(x);
    return 0;
    }

    View full-size slide

  59. ؔ਺ͷҾ਺ͱͯ͠ͷϙΠϯλ
    ͜͜Ͱ໰୊
    ▸ ͖ͬ͞දࣔ͞Εͨ2ͭͷΞυϨεʹ͍ͭͯɺͲͷΑ͏ͳ͜ͱ͕ߟ͑ΒΕΔʁ

    View full-size slide

  60. ؔ਺ͷҾ਺ͱͯ͠ͷϙΠϯλ
    ؔ਺ͷҾ਺
    ▸ Ҿ਺ʹ౉͢લͷม਺ͱɺؔ਺಺Ͱݺͼग़͞ΕͨҾ਺ͷΞυϨε͸ҟͳ͍ͬͯΔ
    ▸ ͦΕͧΕผͷϝϞϦ্ۭؒʹల։͞Ε͍ͯΔ
    ▸ ؔ਺ΛݺͿͱ͖ʹɺͦͷؔ਺͕ಡΈࠐΊΔྖҬʹҾ਺͸શ෦ίϐʔ͞ΕΔ
    ▸ intܕ̍ݸ෼Ͱ͋Ε͹ίϐʔʹ࣌ؒ͸͔͔Βͳ͍
    ▸ ͔͠͠ڊେͳσʔλͩͬͨ৔߹ɺඇޮ཰త
    ▸ ͦ͜ͰϝϞϦ্ۭؒͷΞυϨε͚ͩΛ౉ͯ͋͛͠Ε͹ྑ͍ͱ͍͏ൃ૝

    View full-size slide

  61. ؔ਺ͷҾ਺ͱͯ͠ͷϙΠϯλ
    ϙΠϯλܕม਺ΛҾ਺ʹऔΔؔ਺
    ▸ ΞυϨε͕ཉ͍͚ͩ͠ͳͷͰɺૉ௚ʹ౉ͯ͋͛͠Δ
    ▸ ؔ਺ͷதͰ͸ΞυϨεΛѻ͍ͬͯΔ͜ͱʹ஫ҙ
    ▸ ͋ͱ͸࢖ΘΕΔํʹͦΖ͑ΔݪଇͰOK!
    ▸ ίϐʔ͞ΕΔͷ͸ΞυϨε͚ͩ
    #include
    void swap(int *x, int *y) {
    int tmp = *x;
    *x = *y;
    *y = tmp;
    }
    int main(void) {
    int a = 1;
    int b = 2;
    printf("%d, %d\n", a, b);
    swap(&a, &b);
    printf("%d, %d\n", a, b);
    return 0;
    }

    View full-size slide

  62. ؔ਺ͷҾ਺ͱͯ͠ͷϙΠϯλ
    ؔ਺ͷҾ਺ʹ1࣍ݩ഑ྻΛऔΔʹ͸
    ▸ ౉͢΋ͷ͸ΞυϨε
    ▸ ΞυϨεΛҾ਺ʹऔΕ͹OK
    ▸ αΠζΛҾ਺ʹऔΔཧ༝͸ʁ
    #include
    void print_array(int *array, int size) {
    for(int i = 0; i < size; ++i) {
    printf("%d\n", array[i]);
    }
    }
    int main(void) {
    int nums[] = {0, 1, 2, 3, 4};
    int size = sizeof(nums) / sizeof(int);
    print_array(nums, size);
    return 0;
    }

    View full-size slide

  63. ؔ਺ͷҾ਺ͱͯ͠ͷϙΠϯλ
    αΠζΛҾ਺ʹऔΔཧ༝
    ▸ ഑ྻܕม਺͸ɺ഑ྻશମͷαΠζ৘ใΛ͍࣋ͬͯΔ
    ▸ Ұํɺؔ਺ͷҾ਺ʹ͸୯ͳΔϙΠϯλܕม਺͔͠࢖͑ͳ͍
    ▸ ϙΠϯλܕͰ͋Δͱ͍͏৘ใ͔࣋ͨ͠ͳ͍
    ▸ ഑ྻશମͷαΠζ৘ใ͕ܽམͯ͠͠·͏
    ▸ ࣄલʹશମͷαΠζ͔Βཁૉ਺ΛٻΊͯɺҾ਺ͱͯ͠౉͢ඞཁ͕͋Δ

    View full-size slide

  64. ؔ਺ͷҾ਺ͱͯ͠ͷϙΠϯλ
    ؔ਺ͷҾ਺ʹ2࣍ݩ഑ྻΛऔΔʹ͸
    ▸ 2࣍ݩ഑ྻͷʮ1ݸ෼ͷαΠζʯ͸தʹೖ͍ͬͯΔʮ1࣍ݩ഑ྻશମʯͷαΠζ
    ▸ ͭ·Γதʹೖ͍ͬͯΔ1࣍ݩ഑ྻͷαΠζ͸ݻఆ͠ͳ͚Ε͹ͳΒͳ͍
    ▸ ैདྷͷCݴޠͰ͸೚ҙ௕ͷ1࣍ݩ഑ྻΛؚΉ഑ྻΛҾ਺ʹ͸औΕͳ͔ͬͨ

    View full-size slide

  65. ؔ਺ͷҾ਺ͱͯ͠ͷϙΠϯλ
    ؔ਺ͷҾ਺ʹ2࣍ݩ഑ྻΛऔΔʹ͸
    ▸ C99͔ΒҾ਺Λྲྀ༻Ͱ͖ΔΑ͏ʹ
    ▸ n࣍ݩ഑ྻʹ΋Ԡ༻Մೳ
    #include
    void print_array(int s1, int s2, int array[][s2]) {
    for(int i = 0; i < s1; ++i) {
    for (int j = 0; j < s2; ++j) {
    printf("%d\n", array[i][j]);
    }
    }
    }
    int main(void) {
    int nums[][3] = {{0, 1, 2}, {3, 4, 5}};
    int s1 = sizeof(nums) / (sizeof(int) * 3);
    print_array(s1, 3, nums);
    return 0;
    }

    View full-size slide

  66. ؔ਺ͷҾ਺ͱͯ͠ͷϙΠϯλ
    ͜͜·Ͱͷ·ͱΊ
    ▸ Ҿ਺ʹ౉ͨ͠஋͸ίϐʔ͞ΕΔ
    ▸ ॏ͍ͨ΋ͷΛ౉͢ͱ͖͸ΞυϨεΛ౉ͯ͋͛͠Α͏
    ▸ ΞυϨεΛड͚औΓ͍ͨؔ਺Ͱ͸ϙΠϯλܕม਺ΛҾ਺ʹऔΔ
    ▸ 1࣍ݩ഑ྻΛ౉͢ͱ͖͸ϙΠϯλܕม਺Ͱड͚औΔ
    ▸ αΠζΛ౉͢ͷ΋͓๨Εͳ͘
    ▸ 2࣍ݩҎ্͸Ұॹʹड͚औͬͨαΠζΛྲྀ༻͢Δ

    View full-size slide

  67. ϙΠϯλܕ഑ྻ
    VS

    ഑ྻͷϙΠϯλ

    View full-size slide

  68. ϙΠϯλܕ഑ྻ VS ഑ྻͷϙΠϯλ
    ϙΠϯλܕ഑ྻ
    ▸ ϙΠϯλܕΛฒ΂ͨ഑ྻΛ࡞Γ͍ͨͱ͖͸ҎԼͷΑ͏ʹએݴ͢Ε͹OK
    ▸ int *x[5]
    ▸ ʮ࢖ΘΕํʯͰએݴ͢ΔݪଇΛࢥ͍ग़͢
    ▸ *x[1]Ͱxͷ1൪໨ͷΞυϨεͷ

    த਎͕ಡΊΔ
    #include
    int main(void) {
    int a = 0;
    int b = 1;
    int *x[2] = {&a, &b};
    printf("%d, %d\n", *x[0], *x[1]);
    return 0;
    }

    View full-size slide

  69. ϙΠϯλܕ഑ྻ VS ഑ྻͷϙΠϯλ
    ഑ྻͷϙΠϯλ
    ▸ ഑ྻͷઌ಄ΞυϨεͱܕ৘ใΛ࣋ͬͨϙΠϯλܕม਺ͷ͜ͱ
    ▸ ϙΠϯλܕએݴ͸഑ྻએݴΑΓऑ͍
    ▸ ׅހΛ͚ͭͯ༏ઌ౓Λ্͛Δ
    ▸ p͸ϙΠϯλͰɺܕ৘ใ͸ཁૉ਺3ͷchar഑ྻ
    ▸ ʮ࢖ΘΕํʯͷݪଇͰ͢Αʂ
    #include
    int main(void) {
    char str[3] = "ab";
    char (*p)[3] = &str;
    printf("%c\n", (*p)[1]);
    return 0;
    }

    View full-size slide

  70. ϙΠϯλܕ഑ྻ VS ഑ྻͷϙΠϯλ
    ͜͜·Ͱͷ·ͱΊ
    ▸ ϙΠϯλܕ഑ྻ͸ϙΠϯλܕΛ഑ྻʹͨ͠΋ͷ
    ▸ ഑ྻͷϙΠϯλ͸ɺܕ৘ใ͕௕͞nͷ഑ྻͰ͋ΔϝϞϦ্ͷྖҬͷઌ಄ΞυϨε
    Λ֨ೲ͢ΔͨΊͷϙΠϯλܕม਺
    ▸ ϙΠϯλܕએݴ͸഑ྻܕએݴΑΓऑ͍
    ▸ ͍ͣΕ΋ʮ࢖ΘΕํʯͷݪଇΛࢥ͍ग़ͦ͏

    View full-size slide

  71. ϝϞϦͷಈత֬อ

    View full-size slide

  72. ϝϞϦͷಈత֬อ
    ελοΫͱώʔϓ
    ▸ ϓϩάϥϜͷม਺͸ϝϞϦ্ͷελοΫͱݺ͹ΕΔྖҬʹ֨ೲ͞ΕΔ
    ▸ ελοΫͷ࡞ΒΕํ͸શͯίϯύΠϧ࣌ʹܾఆ͞ΕΔ
    ▸ ࣮ߦ࣌ʹ͸มߋͰ͖ͳ͍
    ▸ ࣮ߦ࣌ʹ৽ͨʹϝϞϦྖҬΛ֬อ͍ͨ͠ͱ͖͸ɺώʔϓʹ࡞੒͢Δ
    ▸ ώʔϓʹ΋ݶΓ͕͋ΔͷͰɺ৯͍ਚ͘͢ͱ֬อͰ͖ͳ͘ͳΔ
    ▸ ࢖͍ऴΘͬͨྖҬ͸ద੾ʹղ์͢Δඞཁ͕͋Δ

    View full-size slide

  73. ϝϞϦͷಈత֬อ
    ώʔϓͷ֬อํ๏
    ▸ stdlib.hʹmallocؔ਺͕͋ΔͷͰ͜ΕΛར༻͢Δ
    ▸ Memory allocateͷུͰ͢
    ▸ ֬อ͍ͨ͠όΠτ਺ΛҾ਺ʹ٧ΊΔ
    ▸ ฦΓ஋͸voidϙΠϯλܕͳͷͰཁ஫ҙ
    ▸ ܕ৘ใΛ͍࣋ͬͯͳ͍ͨΊɺ͜ͷ··Ͱ͸ར༻Ͱ͖ͳ͍
    ▸ ద੾ͳܕʹΩϟετ͢Δඞཁ͕͋Δ
    ▸ ࢖͍ऴΘͬͨྖҬ͸freeؔ਺Ͱղ์͠Α͏

    View full-size slide

  74. ϝϞϦͷಈత֬อ
    ώʔϓΛ֬อͯ͠ΈΔ
    ▸ scanfͰαΠζΛ΋Β͖ͬͯͯ
    ▸ mallocͯ͠
    ▸ ΤϥʔͳΒҟৗऴྃͯ͠
    ▸ ֬อͨ͠ΞυϨεΛදࣔͯ͠
    ▸ ๨Εͣʹfree͢Δ
    #include
    #include
    int main(void) {
    int n;
    scanf("%d", &n);
    char *str = (char *)malloc(sizeof(char) * n);
    if (str == NULL) {
    puts("memory allocate error\n");
    exit(1);
    }
    printf("%p\n", str);
    free(str);
    return 0;
    }

    View full-size slide

  75. ϝϞϦͷಈత֬อ
    ஫ҙ
    ▸ ֬อͨ͠ྖҬʹ͸Ҏલ࢖ΘΕ͍ͯͨσʔλ͕࢒͍ͬͯͨΓ͢Δ
    ▸ ॳظԽΛ๨Εͣʹ
    ▸ ΞυϨεͳͷͰ഑ྻͬΆ͘ར༻Մೳ
    ▸ ͨͩ֬͠อͨ͠ྖҬҎ্ʹ͸Έग़͞ͳ͍Α͏ʹ

    View full-size slide

  76. ϝϞϦͷಈత֬อ
    Մม௕഑ྻ
    ▸ C99Ͱಋೖ͞ΕɺC11ͰΦϓγϣϯʹ

    ֨Լ͛͞Εͨػೳ
    ▸ malloc͠ͳͯ͘΋scanfͳͲͰ

    औ͖ͬͯͨαΠζͰ഑ྻΛ֬อͰ͖Δ
    ▸ ֬อͰ͖Δͷ͸ϩʔΧϧม਺ͷ

    ഑ྻ͚ͩͳͷͰ஫ҙ
    ▸ ͏·͘࢖ͬͯखൈ͖͠Α͏ʂ
    #include
    #include
    int main(void) {
    int n;
    scanf("%d", &n);
    char str[n];
    for (int i = 0; i < n - 1; ++i) {
    str[i] = 'a' + i;
    }
    str[n-1] = '\0';
    printf("%s\n", str);
    return 0;
    }

    View full-size slide

  77. ϝϞϦͷಈత֬อ
    ͜͜·Ͱͷ·ͱΊ
    ▸ ϓϩάϥϜ࣮ߦதʹϝϞϦྖҬ͕ཉ͍͠ͱ͖͸mallocΛ࢖͏
    ▸ ΩϟετͱfreeΛ๨Εͣʹ
    ▸ ഑ྻ͕ཉ͍͚ͩ͠ͳΒՄม௕഑ྻΛ࢖͓͏
    ▸ ஫ҙ: ίϯύΠϧͰ͖ͳ͍ͱ͖͸ʮ-std=c11ʯΦϓγϣϯΛ͚ͭΔ

    View full-size slide

  78. ؔ਺ͷฦΓ஋ͱͯ͠ͷ

    ϙΠϯλ

    View full-size slide

  79. ؔ਺ͷฦΓ஋ͱͯ͠ͷϙΠϯλ
    ؔ਺ͷฦΓ஋ʹϙΠϯλΛ࢖͏ʹ͸
    ▸ ී௨ʹฦΓ஋ͷܕΛϙΠϯλܕʹ͢Ε͹OK
    #include
    char *repeat(char c, int n) {
    char *str = (char *)malloc(sizeof(char) * (n + 1));
    for (int i = 0; i < n; ++i) {
    str[i] = c;
    }
    str[n] = '\0';
    return str;
    }

    View full-size slide

  80. ؔ਺ͷฦΓ஋ͱͯ͠ͷϙΠϯλ
    ஫ҙ఺
    ▸ ؔ਺಺ͷϩʔΧϧม਺ͷΞυϨεΛฦͯ͠͸͍͚·ͤΜ
    ▸ ؔ਺͕ݺ͹ΕΔ౓ʹϩʔΧϧม਺ྖҬ͕֬อ͞Εɺऴྃ࣌ʹղ์͞ΕΔͨΊ
    ▸ ػցޠͰݴ͏ͱ͜ΖͷϕʔεϙΠϯλΛݩʹ໭͢ߦҝ
    ▸ ղ์͞Εͨ͋ͱͲ͏࢖ΘΕΔ͔͸ෆ໌
    ▸ ඞͣώʔϓྖҬ͋Δ͍͸ݺͼग़͠ݩؔ਺ͷϩʔΧϧม਺ΞυϨεΛฦ͢͜ͱ

    View full-size slide

  81. ؔ਺ͷฦΓ஋ͱͯ͠ͷϙΠϯλ
    ͜͜·Ͱͷ·ͱΊ
    ▸ ؔ਺ͷฦ஋ʹϙΠϯλΛ࢖͍͍ͨͱ͖͸ฦΓ஋ΛϙΠϯλܕʹ͢Ε͹OK
    ▸ ͨͩ͠ɺϩʔΧϧม਺ͷΞυϨε͸ฦͯ͠͸͍͚ͳ͍
    ▸ ฦ͢͜ͱ͸Ͱ͖Δ͕ɺར༻͢ΔͱͲ͏ͳΔ͔Θ͔Βͳ͍
    ▸ ϕʔεϙΠϯλ෮ݩͷͨΊ
    ▸ ΍Ζ͏ͱ͢ΔͱҰԠGCC͕ܯࠂͯ͘͠ΕΔ

    View full-size slide

  82. ؔ਺ϙΠϯλ

    View full-size slide

  83. ؔ਺ϙΠϯλ
    ؔ਺ͷਖ਼ମ
    ▸ ؔ਺ͷਖ਼ମͬͯԿͰ͠ΐ͏͔ʁ
    ▸ say_helloؔ਺͸Ͳ͜ʹ͋Δʁ
    #include
    void say_hello(void) {
    puts("hello");
    }
    int main(void) {
    say_hello();
    return 0;
    }

    View full-size slide

  84. ؔ਺ϙΠϯλ
    ؔ਺ͷਖ਼ମ
    ▸ ؔ਺͸ͻͱ͔ͨ·Γͷॲཧ͕ϝϞϦ্ʹల։͞Εͨ΋ͷ
    ▸ ͱ͍͏͜ͱ͸ؔ਺ʹ΋ઌ಄ΞυϨε͕ଘࡏ͢Δ
    ▸ ΞυϨε͕͋ΔͳΒɺܕ৘ใΛ෇͚Ε͹ม਺ʹ୅ೖͰ͖ΔͷͰ͸ʁ

    View full-size slide

  85. ؔ਺ϙΠϯλ
    ؔ਺ϙΠϯλܕ
    ▸ એݴํ๏͸࣍ͷ௨Γ
    ▸ ฦΓ஋ͷܕ (*ม਺໊)(Ҿ਺ͷܕ);
    ▸ ΍͸Γʮ࢖ΘΕํʯͷ๏ଇ
    ▸ ؔ਺એݴ΋ϙΠϯλܕએݴΑΓڧ͍ͷͰ

    Χοί͕ඞཁ
    ▸ ୅ೖ͍ͨ͠ͱ͖͸ؔ਺໊͚ͩΛ࢖༻
    #include
    int to_int(char c) {
    return (int)c;
    }
    int main(void) {
    int (*func)(char c) = to_int;
    printf("%p\n", func);
    printf("%d\n", (*func)('a'));
    return 0;
    }

    View full-size slide

  86. ؔ਺ϙΠϯλ
    ܕΛߟ͑ͯΈΔ
    ▸ ࣍ͷؔ਺Λ୅ೖͰ͖ΔܕΛߟ͑ͯΈΑ͏ʂ
    void putStr(char* str) {
    puts(str);
    }
    int fact(int n) {
    if (n == 2) {
    return 2;
    } else {
    return n * fact(n - 1);
    }
    }
    char first(char* str) {
    return str[0];
    }
    double sum(double a, double b) {
    return a + b;
    }

    View full-size slide

  87. ؔ਺ϙΠϯλ
    ౴͑߹Θͤ
    ▸ ࠨ্ʣvoid (*hoge)(char *)
    ▸ ࠨԼʣint (*hoge)(int)
    ▸ ӈ্ʣchar (*hoge)(char *)
    ▸ ӈԼʣdouble (*hoge)(double, double)

    View full-size slide

  88. ؔ਺ϙΠϯλ
    ͜͜·Ͱͷ·ͱΊ
    ▸ ؔ਺΋ϝϞϦ্ʹల։͞Ε͍ͯΔͨΊઌ಄ΞυϨε͕͋Δ
    ▸ ܕΛ޻෉͢Ε͹ͦΕΛ୅ೖ͢Δ͜ͱ͕Ͱ͖Δ
    ▸ ฦΓ஋ͷܕ (*ม਺໊)(Ҿ਺ͷܕ);
    ▸ ಈ࡞͍ͤͨؔ͞਺ΛϓϩάϥϜதͰ੾Γସ͍͑ͨͱ͖ʹར༻͠Α͏
    ▸ ͨͩ͠ฦΓ஋ͱҾ਺ͷܕ͕Ұக͠ͳ͚Ε͹ͳΒͳ੍͍໿͕...

    View full-size slide

  89. ઢܗϦετ
    ઢܗϦετͱ͸
    ▸ ઌ಄ʹ௥Ճ͢Δͷ͕؆୯(ఆ਺࣌ؒ)ͳՄม௕ͷϦετ
    ▸ CͰ͸ߏ଄ମΛ࢖ͬͯࣗྗͰ࣮૷͠·͢
    ▸ C++΍Javaʹ͸ඪ४Ͱ༻ҙ͞Ε͍ͯΔͷͰΊͬͪΌศར
    ▸ ߏ଄ମɺಈత֬อɺϙΠϯλͳͲ༷ʑͳཁૉ͕ొ৔͠·͢

    View full-size slide

  90. ઢܗϦετ
    Ϧετ༻ߏ଄ମΛ࡞Δ
    ▸ structΛ࢖ͬͯϦετͷ1ϊʔυΛ࡞੒
    ▸ ࣗ෼ࣗ਎Λࢀর͢ΔͨΊɺtypedefʹΑΔ໋໊͸એݴதͰ͸ར༻ෆՄ
    typedef struct node {
    int value;
    struct node *next;
    } Node;

    View full-size slide

  91. ઢܗϦετ
    ϊʔυΛ࡞Δ
    ▸ mallocͰ࡞੒ͯ͠ɺத਎ΛॳظԽ͓͖ͯ͠·͢
    ▸ ҰԠNULLνΣοΫ΋͓͖ͯ͠·͢
    Node *create_node(int x) {
    Node *new_node = (Node *)malloc(sizeof(Node));
    if (new_node == NULL) {
    return NULL;
    }
    new_node->value = x;
    new_node->next = NULL;
    return new_node;
    }

    View full-size slide

  92. ઢܗϦετ
    ϊʔυΛͭͳ͛Δ
    ▸ ֤ϊʔυΛnextͷϙΠϯλΛॻ͖׵͑Δ͜ͱͰ઀ଓ͠·͢
    void concat_node(Node *node, Node *list) {
    node->next = list;
    }

    View full-size slide

  93. ઢܗϦετ
    N൪໨ͷϊʔυΛऔಘ͢Δ
    ▸ forจͰnextΛͨͲΔ͜ͱͰn൪໨ͷ஋Λऔಘ͠·͢
    int take_n(Node *list, int n) {
    Node *tmp = list;
    for (int i = 0; i < n; ++i) {
    tmp = tmp->next;
    }
    return tmp->value;
    }

    View full-size slide

  94. ઢܗϦετ
    N൪໨ͷϊʔυΛ࡟আ͢Δ
    ▸ ࡟আର৅ͷ1ͭલ·ͰͨͲΔ
    ▸ ࡟আର৅ͱͦͷ࣍ͷϊʔυͷΞυϨεΛୀආͤ͞Δ
    ▸ ࡟আର৅ͷΞυϨεΛfree͢Δ void delete_n(Node *list, int n) {
    Node *tmp = list;
    for (int i = 0; i < n - 1; ++i) {
    tmp = tmp->next;
    }
    Node *target = tmp->next;
    tmp->next = tmp->next->next;
    free(target);
    }

    View full-size slide

  95. ઢܗϦετ
    શཁૉΛදࣔ͢Δ
    ▸ nextΛͨͲͬͯදࣔ͢Δ
    ▸ ͨͩ͠࠷ޙͷཁૉ͸ݸผʹදࣔ
    void print_list(Node *list) {
    Node *tmp = list;
    while (tmp->next != NULL) {
    printf("%d\n", tmp->value);
    tmp = tmp->next;
    }
    printf("%d\n", tmp->value);
    }

    View full-size slide

  96. ઢܗϦετ
    ςετͯ͠ΈΔ
    ▸ 1. ϊʔυΛ3ͭ࡞੒͠ɺ݁߹͢Δ
    ▸ 2. શཁૉΛදࣔ
    ▸ 3. 1൪໨ͷཁૉ͚ͩΛදࣔ
    ▸ 4. 1൪໨ͷཁૉΛ࡟আ
    ▸ 5. શཁૉΛදࣔ
    int main(void) {
    Node *node0 = create_node(0);
    Node *node1 = create_node(1);
    concat_node(node1, node0);
    Node *node2 = create_node(2);
    concat_node(node2, node1);
    print_list(node2);
    puts("");
    printf("%d\n", take_n(node2, 1));
    puts("");
    delete_n(node2, 1);
    print_list(node2);
    return 0;
    }

    View full-size slide

  97. ઢܗϦετ
    ͜͜·Ͱͷ·ͱΊ
    ▸ ߏ଄ମΛ࢖ͬͯઢܗϦετΛ࣮૷Ͱ͖ͨ
    ▸ ϊʔυΛ௥Ճͨ͠Γ࡟আͨ͠ΓͰ͖ͨ
    ▸ ৭ʑͱ͓ർΕ༷Ͱͨ͠

    View full-size slide

  98. ͓ΘΓʹ
    ࣭໰λΠϜ
    ▸ ͳΜ్͔தͰΘ͔Βͳ͔ͬͨͱ͜Ζͱ͔
    ▸ ՝୊ʹ͍ͭͯͱ͔
    ▸ Ϩϙʔτͷॻ͖ํͱ͔

    View full-size slide