SlideShare ist ein Scribd-Unternehmen logo
1 von 24
Downloaden Sie, um offline zu lesen
OOP in C
 Inherit


           elpam.tw@gmail.com
           elpam@Taiwan, Taipei, 2008
LICENSE
●   本投影片授權方式為:
    –   姓名標示─非商業性─相同方式分享 3.0  台灣版
    –   http://creativecommons.org/licenses/by­nc­sa/3.0/tw/




●   所有的範例程式皆為 Public Domain




                              elpam.tw@gmail.com
About This Slides
●   All example was build by 
    –   GCC­4.1.3
    –   GLIB­1.2.10
    –   GMAKE­3.81




                      elpam.tw@gmail.com
C  Language Review
●   Stack
    –   Caller Function's Address
    –   CPU's Register
    –   Local Value
●   Heap (run­time heap)
    –   managed by malloc




                         elpam.tw@gmail.com
Address Space
●   Linux Memory Model
    –   copy from Jserv's               STACK
        Hacking Hello World




                                        HEAP




                        elpam.tw@gmail.com
Function Stack          Caller's Address
                                             Return Address


void Func_B()                                  Local Value
{
    int a,b;
   /* do nothing*/
}                                                               Low
void Func_A()                                  int b
{                       Func_B's Stack         int a
    char a;                                   Func_A
    Func_B();
}
                                               char a
                        Func_A's Stack
                                                main
int main()
{
    Func_A();
}                        main's stack            ....

                        elpam.tw@gmail.com                      High
After Review
●   請了解 Callee Stack 裡頭會放置那些東西
●   請了解 Callee Stack Release 後那些變數會被清
    除掉
●   請確定已正確的了解 Stack 與 Heap 的差別




                elpam.tw@gmail.com
Structure




elpam.tw@gmail.com
Structure (I)
                                      /* 1­1.c */
 ●   Address + Offset                 struct A{
                                         int a;
                                         int b;
     –   pa = 0x804a008               };
     –   pa ­> a == pa + 0            int main()
                                      {
     –   pa ­> b == pa + 4             struct A* pa =malloc(sizeof(struct A));

                                        printf("%x,%x,%xn"
                                                ,pa, &(pa­>a), &(pa­>b) );

                                        free( pa );
 #> ./1­1                             }
804a008,804a008,804a00c
 #>


                             elpam.tw@gmail.com
●   所有的東西都是 Memory Address
●   ­>  所代表的意義是 offset ,而這一個數值的大
    小可以在 Compiler Time 被決定




               elpam.tw@gmail.com
Structure (II)
                                      /* 1­2.c */
                                      struct A{                       Size = 8 byte
 ●   pa == pb                            int a;   int b;
                                      };
      –     pa ­> c Compile Error     struct B{
                                         int a;   int b;
      –     pb ­> c Correct              int c;                       Size = 12 byte
                                      };
                                      int main()
                                      {
                                       struct A* pa =malloc(sizeof(B));
                                       struct B* pb = pa;
                                        pa ­> a = 1;     pa ­> b = 2;
 #> ./1­2                               pb ­> c = 3;
1,2,3
  #>                                    printf("pb(%d,%d,%d)n"
                                                       ,pb­>a,pb­>b,pb­>c );
                                      }


                              elpam.tw@gmail.com
●   Pointer 的 Type 與被 allocate 的記憶體大小是沒
    有關係的
●   請注意我們是如何使用 casting




                 elpam.tw@gmail.com
/* 1­3.c */
                  Structure (III)
struct A{
     int a;
●
}; Casting        int main()
                  {
                       struct A* pa = malloc( sizeof(struct A) );
struct B{              struct B* pb = pa;
    short b;           struct C* pc = pa;
    short c;
};                    pc ­> hi_b = 0x12;
                      pc ­> low_b = 0x34;
struct C{             pc ­> hi_c = 0x56;
    char low_b;       pc ­> low_c = 0x78;
    char hi_b;
    char low_c;       printf("pb­>b(0x%x)  pb­>c(0x%x)n",pb­>b,pb­>c );
    char hi_c;        printf("pa­>a(0x%x)n",pa­>a );
};
                      free( pa );
                  }


                        elpam.tw@gmail.com
Structure (III) (cont')
●   Memory Casting
●   x86 is little­endian                    pc ­> hi_b = 0x12;
                                            pc ­> low_b = 0x34;
                                            pc ­> hi_c = 0x56;
                                            pc ­> low_c = 0x78;




                                        #>./1­3
                                      pb­>b(0x1234)  pb­>c(0x5678)
                                      pa­>a(0x56781234)
                                        #>




                       elpam.tw@gmail.com
Inherit




elpam.tw@gmail.com
C++ Review
                                      /* 1­6.cpp */
                                      class A{
●   Casting to Parent                    public:
                                            int a;
                                      };
                                      class B : public A{
                                         public:
                                            int b;
                                      };

                                      int main()
                                      {
                                           B b;
                                           b . b  = 2;
                                           A(b) . a = 1
                                      }




                        elpam.tw@gmail.com
Inherit
                                          int main()
                                          {
   ●   sizeof(A) =  8                      struct B* pb = malloc(sizeof(struct B));
                                            pb ­> parent . a = 0;
   ●   sizeof(B) = 12                       pb ­> parent . b = 1;
                                            pb ­> c = 2
                    /* 1­4.h */           }
                    struct A{
                       int a;
                       int b;             /* 1­4.c */
       Inherit A    };                    int main()
                    struct B{             {
                       struct A parent;    struct A* pa =malloc(sizeof(struct B));
                       int c;              struct B* pb = pa;
                    };                      pa ­> a = 0;
                                            pa ­> b = 1;
  #>./1­4                                   pb ­> c = 2;
pa(1,2) pb(3)                             }
  #>
                             elpam.tw@gmail.com
Inherit & Casting
                                         /* 1­5.c */
   ●   Casting to Parent                 int main()
                                         {
                                          struct B* pb =malloc(sizeof(struct B));
                                           pb ­> c = 3;
                                          struct A* pa = pb;
                   /* 1­5.h */             pa ­> a = 1;
                   struct A{               pa ­> a = 2;
                      int a;
                      int b;               printf("pa(%d,%d) pb(%d)n"
                   };                              ,pa­>a,pa­>b,pb­>c );
                   struct B{             }
                      struct A parent;
                      int c;  
                   };

  #>./1­5
pa(1,2) pb(3)
  #>
                            elpam.tw@gmail.com
Public Member Value in C++
                                      int main()
                                      {
class A{
                                        B* b = new B;
public:
   int a;
                                        b . a = 0;
   int b;
                                        b . b = 1;
};
                                        b . c = 2;
class B : public A {
                                      }
public:
   int c;  
};
                                      int main()
                                      {
                                        A* a = new b;

                                        a . a = 0;
                                        a . b = 1;
                                        a . c = 2;       /* error */
                                        B(a) . c = 2;  /* correct */
                                      }
                       elpam.tw@gmail.com
Object Inherit
●   C Language                           ●   C++


     struct A{                                     class A{
        int a;                                     public:
        int b;                                        int a;
     };                                               int b;
     struct B{                                     };
        struct A parent;                           class B : public A {
        int c;                                     public:
     };                                               int c;  
                                                   };




                              elpam.tw@gmail.com
Type Casting ( 形態轉換 )
●   C Language                         ●   C++
    –   Structure­to­Structure             –     dynamic_cast
         ●   Address­to­Address            –     static_cast
                                           –     reinterpret_cast
                                           –     const_cast




                            elpam.tw@gmail.com
Inherit in C
●   C ­> B ­> A
                                       /* error memory access */
                                       int main()
    struct A{                          {
       int v1;                          struct B* pb = malloc(sizeof(B));
       int v2;                          struct C* pc = pb; /* correct */
    };                                  pc ­> v1 = 0; /* run time error */
    struct B{
       struct A parent;                }
       int v1;  
    };
    struct C{
       struct B parent;                struct C c;
       int v1;                         struct B* pb = &c;  /* use B's member */
    };                                 struct A* pb = &c;  /* use A's member */

                            elpam.tw@gmail.com
Conclusion 



我們可以使用 memory casting
的技巧來描述繼承關係

*  因為缺乏語法 (Compiler) 的保護,所以我們必須
要小心的使用



            elpam.tw@gmail.com
Questions
●   建構子 (Constructor)  還有需要嗎 ?
●   但 C++ 的繼承還提供了 Virtual Function,  需要
    這樣的功能嗎 ?  該如何實作 ?
    –   下一個投影片將討論這兩個問題




                 elpam.tw@gmail.com

Weitere ähnliche Inhalte

Was ist angesagt?

C++17 introduction - Meetup @EtixLabs
C++17 introduction - Meetup @EtixLabsC++17 introduction - Meetup @EtixLabs
C++17 introduction - Meetup @EtixLabsStephane Gleizes
 
Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)Geeks Anonymes
 
Fun with Lambdas: C++14 Style (part 1)
Fun with Lambdas: C++14 Style (part 1)Fun with Lambdas: C++14 Style (part 1)
Fun with Lambdas: C++14 Style (part 1)Sumant Tambe
 
[C++ Korea] Effective Modern C++ Study, Item 11 - 13
[C++ Korea] Effective Modern C++ Study, Item 11 - 13[C++ Korea] Effective Modern C++ Study, Item 11 - 13
[C++ Korea] Effective Modern C++ Study, Item 11 - 13Chris Ohk
 
[C++ korea] effective modern c++ study item 3 understand decltype +이동우
[C++ korea] effective modern c++ study   item 3 understand decltype +이동우[C++ korea] effective modern c++ study   item 3 understand decltype +이동우
[C++ korea] effective modern c++ study item 3 understand decltype +이동우Seok-joon Yun
 
C++ 11 Features
C++ 11 FeaturesC++ 11 Features
C++ 11 FeaturesJan Rüegg
 
Paradigmas de Linguagens de Programacao - Aula #5
Paradigmas de Linguagens de Programacao - Aula #5Paradigmas de Linguagens de Programacao - Aula #5
Paradigmas de Linguagens de Programacao - Aula #5Ismar Silveira
 
Objective c beginner's guide
Objective c beginner's guideObjective c beginner's guide
Objective c beginner's guideTiago Faller
 
C++ Advanced
C++ AdvancedC++ Advanced
C++ AdvancedVivek Das
 
Data structure week 1
Data structure week 1Data structure week 1
Data structure week 1karmuhtam
 
Solid C++ by Example
Solid C++ by ExampleSolid C++ by Example
Solid C++ by ExampleOlve Maudal
 
李建忠、侯捷设计模式讲义
李建忠、侯捷设计模式讲义李建忠、侯捷设计模式讲义
李建忠、侯捷设计模式讲义yiditushe
 
C++11 concurrency
C++11 concurrencyC++11 concurrency
C++11 concurrencyxu liwei
 
Learning C++ - Functions in C++ 3
Learning C++ - Functions  in C++ 3Learning C++ - Functions  in C++ 3
Learning C++ - Functions in C++ 3Ali Aminian
 
Pure virtual function and abstract class
Pure virtual function and abstract classPure virtual function and abstract class
Pure virtual function and abstract classAmit Trivedi
 
(2) c sharp introduction_basics_part_i
(2) c sharp introduction_basics_part_i(2) c sharp introduction_basics_part_i
(2) c sharp introduction_basics_part_iNico Ludwig
 
Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4Ismar Silveira
 

Was ist angesagt? (20)

C++17 introduction - Meetup @EtixLabs
C++17 introduction - Meetup @EtixLabsC++17 introduction - Meetup @EtixLabs
C++17 introduction - Meetup @EtixLabs
 
Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)
 
Fun with Lambdas: C++14 Style (part 1)
Fun with Lambdas: C++14 Style (part 1)Fun with Lambdas: C++14 Style (part 1)
Fun with Lambdas: C++14 Style (part 1)
 
C++11
C++11C++11
C++11
 
[C++ Korea] Effective Modern C++ Study, Item 11 - 13
[C++ Korea] Effective Modern C++ Study, Item 11 - 13[C++ Korea] Effective Modern C++ Study, Item 11 - 13
[C++ Korea] Effective Modern C++ Study, Item 11 - 13
 
[C++ korea] effective modern c++ study item 3 understand decltype +이동우
[C++ korea] effective modern c++ study   item 3 understand decltype +이동우[C++ korea] effective modern c++ study   item 3 understand decltype +이동우
[C++ korea] effective modern c++ study item 3 understand decltype +이동우
 
C++ 11 Features
C++ 11 FeaturesC++ 11 Features
C++ 11 Features
 
Le langage rust
Le langage rustLe langage rust
Le langage rust
 
Paradigmas de Linguagens de Programacao - Aula #5
Paradigmas de Linguagens de Programacao - Aula #5Paradigmas de Linguagens de Programacao - Aula #5
Paradigmas de Linguagens de Programacao - Aula #5
 
Objective c beginner's guide
Objective c beginner's guideObjective c beginner's guide
Objective c beginner's guide
 
C++ Advanced
C++ AdvancedC++ Advanced
C++ Advanced
 
Data structure week 1
Data structure week 1Data structure week 1
Data structure week 1
 
Solid C++ by Example
Solid C++ by ExampleSolid C++ by Example
Solid C++ by Example
 
李建忠、侯捷设计模式讲义
李建忠、侯捷设计模式讲义李建忠、侯捷设计模式讲义
李建忠、侯捷设计模式讲义
 
C++11 concurrency
C++11 concurrencyC++11 concurrency
C++11 concurrency
 
Learning C++ - Functions in C++ 3
Learning C++ - Functions  in C++ 3Learning C++ - Functions  in C++ 3
Learning C++ - Functions in C++ 3
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Pure virtual function and abstract class
Pure virtual function and abstract classPure virtual function and abstract class
Pure virtual function and abstract class
 
(2) c sharp introduction_basics_part_i
(2) c sharp introduction_basics_part_i(2) c sharp introduction_basics_part_i
(2) c sharp introduction_basics_part_i
 
Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4
 

Ähnlich wie OOP in C - Inherit (Chinese Version)

CS50 Lecture3
CS50 Lecture3CS50 Lecture3
CS50 Lecture3昀 李
 
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6 1
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6  1ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6  1
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6 1Little Tukta Lita
 
2. data, operators, io
2. data, operators, io2. data, operators, io
2. data, operators, iohtaitk
 
Threads and Callbacks for Embedded Python
Threads and Callbacks for Embedded PythonThreads and Callbacks for Embedded Python
Threads and Callbacks for Embedded PythonYi-Lung Tsai
 
개발 과정 최적화 하기 내부툴로 더욱 강력한 개발하기 Stephen kennedy _(11시40분_103호)
개발 과정 최적화 하기 내부툴로 더욱 강력한 개발하기 Stephen kennedy _(11시40분_103호)개발 과정 최적화 하기 내부툴로 더욱 강력한 개발하기 Stephen kennedy _(11시40분_103호)
개발 과정 최적화 하기 내부툴로 더욱 강력한 개발하기 Stephen kennedy _(11시40분_103호)changehee lee
 
C++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorC++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorJussi Pohjolainen
 
Boost.Python: C++ and Python Integration
Boost.Python: C++ and Python IntegrationBoost.Python: C++ and Python Integration
Boost.Python: C++ and Python IntegrationGlobalLogic Ukraine
 
computer notes - Data Structures - 8
computer notes - Data Structures - 8computer notes - Data Structures - 8
computer notes - Data Structures - 8ecomputernotes
 
Objective-Cひとめぐり
Objective-CひとめぐりObjective-Cひとめぐり
Objective-CひとめぐりKenji Kinukawa
 
C++totural file
C++totural fileC++totural file
C++totural filehalaisumit
 

Ähnlich wie OOP in C - Inherit (Chinese Version) (20)

CS50 Lecture3
CS50 Lecture3CS50 Lecture3
CS50 Lecture3
 
Computer Network Assignment Help
Computer Network Assignment HelpComputer Network Assignment Help
Computer Network Assignment Help
 
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6 1
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6  1ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6  1
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6 1
 
2. data, operators, io
2. data, operators, io2. data, operators, io
2. data, operators, io
 
C++ idioms.pptx
C++ idioms.pptxC++ idioms.pptx
C++ idioms.pptx
 
Stacks.ppt
Stacks.pptStacks.ppt
Stacks.ppt
 
Stacks.ppt
Stacks.pptStacks.ppt
Stacks.ppt
 
C test
C testC test
C test
 
Threads and Callbacks for Embedded Python
Threads and Callbacks for Embedded PythonThreads and Callbacks for Embedded Python
Threads and Callbacks for Embedded Python
 
개발 과정 최적화 하기 내부툴로 더욱 강력한 개발하기 Stephen kennedy _(11시40분_103호)
개발 과정 최적화 하기 내부툴로 더욱 강력한 개발하기 Stephen kennedy _(11시40분_103호)개발 과정 최적화 하기 내부툴로 더욱 강력한 개발하기 Stephen kennedy _(11시40분_103호)
개발 과정 최적화 하기 내부툴로 더욱 강력한 개발하기 Stephen kennedy _(11시40분_103호)
 
C++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorC++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operator
 
Computer Science Assignment Help
Computer Science Assignment HelpComputer Science Assignment Help
Computer Science Assignment Help
 
C++ Language
C++ LanguageC++ Language
C++ Language
 
Boost.Python: C++ and Python Integration
Boost.Python: C++ and Python IntegrationBoost.Python: C++ and Python Integration
Boost.Python: C++ and Python Integration
 
computer notes - Data Structures - 8
computer notes - Data Structures - 8computer notes - Data Structures - 8
computer notes - Data Structures - 8
 
C# 8 and Beyond
C# 8 and BeyondC# 8 and Beyond
C# 8 and Beyond
 
Objective-Cひとめぐり
Objective-CひとめぐりObjective-Cひとめぐり
Objective-Cひとめぐり
 
C Programming
C ProgrammingC Programming
C Programming
 
Revision1 C programming
Revision1 C programmingRevision1 C programming
Revision1 C programming
 
C++totural file
C++totural fileC++totural file
C++totural file
 

Kürzlich hochgeladen

Inspiring Through Words Power of Inspiration.pptx
Inspiring Through Words Power of Inspiration.pptxInspiring Through Words Power of Inspiration.pptx
Inspiring Through Words Power of Inspiration.pptxShubham Rawat
 
Module-2-Lesson-2-COMMUNICATION-AIDS-AND-STRATEGIES-USING-TOOLS-OF-TECHNOLOGY...
Module-2-Lesson-2-COMMUNICATION-AIDS-AND-STRATEGIES-USING-TOOLS-OF-TECHNOLOGY...Module-2-Lesson-2-COMMUNICATION-AIDS-AND-STRATEGIES-USING-TOOLS-OF-TECHNOLOGY...
Module-2-Lesson-2-COMMUNICATION-AIDS-AND-STRATEGIES-USING-TOOLS-OF-TECHNOLOGY...JeylaisaManabat1
 
Authentic No 1 Amil Baba In Pakistan Amil Baba In Faisalabad Amil Baba In Kar...
Authentic No 1 Amil Baba In Pakistan Amil Baba In Faisalabad Amil Baba In Kar...Authentic No 1 Amil Baba In Pakistan Amil Baba In Faisalabad Amil Baba In Kar...
Authentic No 1 Amil Baba In Pakistan Amil Baba In Faisalabad Amil Baba In Kar...Authentic No 1 Amil Baba In Pakistan
 
(南达科他州立大学毕业证学位证成绩单-永久存档)
(南达科他州立大学毕业证学位证成绩单-永久存档)(南达科他州立大学毕业证学位证成绩单-永久存档)
(南达科他州立大学毕业证学位证成绩单-永久存档)oannq
 
E J Waggoner against Kellogg's Pantheism 8.pptx
E J Waggoner against Kellogg's Pantheism 8.pptxE J Waggoner against Kellogg's Pantheism 8.pptx
E J Waggoner against Kellogg's Pantheism 8.pptxJackieSparrow3
 
南新罕布什尔大学毕业证学位证成绩单-学历认证
南新罕布什尔大学毕业证学位证成绩单-学历认证南新罕布什尔大学毕业证学位证成绩单-学历认证
南新罕布什尔大学毕业证学位证成绩单-学历认证kbdhl05e
 

Kürzlich hochgeladen (6)

Inspiring Through Words Power of Inspiration.pptx
Inspiring Through Words Power of Inspiration.pptxInspiring Through Words Power of Inspiration.pptx
Inspiring Through Words Power of Inspiration.pptx
 
Module-2-Lesson-2-COMMUNICATION-AIDS-AND-STRATEGIES-USING-TOOLS-OF-TECHNOLOGY...
Module-2-Lesson-2-COMMUNICATION-AIDS-AND-STRATEGIES-USING-TOOLS-OF-TECHNOLOGY...Module-2-Lesson-2-COMMUNICATION-AIDS-AND-STRATEGIES-USING-TOOLS-OF-TECHNOLOGY...
Module-2-Lesson-2-COMMUNICATION-AIDS-AND-STRATEGIES-USING-TOOLS-OF-TECHNOLOGY...
 
Authentic No 1 Amil Baba In Pakistan Amil Baba In Faisalabad Amil Baba In Kar...
Authentic No 1 Amil Baba In Pakistan Amil Baba In Faisalabad Amil Baba In Kar...Authentic No 1 Amil Baba In Pakistan Amil Baba In Faisalabad Amil Baba In Kar...
Authentic No 1 Amil Baba In Pakistan Amil Baba In Faisalabad Amil Baba In Kar...
 
(南达科他州立大学毕业证学位证成绩单-永久存档)
(南达科他州立大学毕业证学位证成绩单-永久存档)(南达科他州立大学毕业证学位证成绩单-永久存档)
(南达科他州立大学毕业证学位证成绩单-永久存档)
 
E J Waggoner against Kellogg's Pantheism 8.pptx
E J Waggoner against Kellogg's Pantheism 8.pptxE J Waggoner against Kellogg's Pantheism 8.pptx
E J Waggoner against Kellogg's Pantheism 8.pptx
 
南新罕布什尔大学毕业证学位证成绩单-学历认证
南新罕布什尔大学毕业证学位证成绩单-学历认证南新罕布什尔大学毕业证学位证成绩单-学历认证
南新罕布什尔大学毕业证学位证成绩单-学历认证
 

OOP in C - Inherit (Chinese Version)

  • 1. OOP in C Inherit elpam.tw@gmail.com elpam@Taiwan, Taipei, 2008
  • 2. LICENSE ● 本投影片授權方式為: – 姓名標示─非商業性─相同方式分享 3.0  台灣版 – http://creativecommons.org/licenses/by­nc­sa/3.0/tw/ ● 所有的範例程式皆為 Public Domain elpam.tw@gmail.com
  • 3. About This Slides ● All example was build by  – GCC­4.1.3 – GLIB­1.2.10 – GMAKE­3.81 elpam.tw@gmail.com
  • 4. C  Language Review ● Stack – Caller Function's Address – CPU's Register – Local Value ● Heap (run­time heap) – managed by malloc elpam.tw@gmail.com
  • 5. Address Space ● Linux Memory Model – copy from Jserv's  STACK Hacking Hello World HEAP elpam.tw@gmail.com
  • 6. Function Stack Caller's Address Return Address void Func_B() Local Value {     int a,b;    /* do nothing*/ } Low void Func_A() int b { Func_B's Stack int a     char a; Func_A     Func_B(); } char a Func_A's Stack main int main() {     Func_A(); } main's stack .... elpam.tw@gmail.com High
  • 7. After Review ● 請了解 Callee Stack 裡頭會放置那些東西 ● 請了解 Callee Stack Release 後那些變數會被清 除掉 ● 請確定已正確的了解 Stack 與 Heap 的差別 elpam.tw@gmail.com
  • 9. Structure (I) /* 1­1.c */ ● Address + Offset struct A{    int a;    int b; – pa = 0x804a008 }; – pa ­> a == pa + 0 int main() { – pa ­> b == pa + 4  struct A* pa =malloc(sizeof(struct A));   printf("%x,%x,%xn" ,pa, &(pa­>a), &(pa­>b) );   free( pa );  #> ./1­1 } 804a008,804a008,804a00c  #> elpam.tw@gmail.com
  • 10. 所有的東西都是 Memory Address ● ­>  所代表的意義是 offset ,而這一個數值的大 小可以在 Compiler Time 被決定 elpam.tw@gmail.com
  • 11. Structure (II) /* 1­2.c */ struct A{ Size = 8 byte ● pa == pb     int a;   int b; }; – pa ­> c Compile Error struct B{    int a;   int b; – pb ­> c Correct    int c;   Size = 12 byte }; int main() {  struct A* pa =malloc(sizeof(B));  struct B* pb = pa;   pa ­> a = 1;     pa ­> b = 2;  #> ./1­2   pb ­> c = 3; 1,2,3   #>   printf("pb(%d,%d,%d)n" ,pb­>a,pb­>b,pb­>c ); } elpam.tw@gmail.com
  • 12. Pointer 的 Type 與被 allocate 的記憶體大小是沒 有關係的 ● 請注意我們是如何使用 casting elpam.tw@gmail.com
  • 13. /* 1­3.c */ Structure (III) struct A{ int a; ● }; Casting int main() { struct A* pa = malloc( sizeof(struct A) ); struct B{ struct B* pb = pa; short b; struct C* pc = pa; short c; }; pc ­> hi_b = 0x12; pc ­> low_b = 0x34; struct C{ pc ­> hi_c = 0x56; char low_b; pc ­> low_c = 0x78; char hi_b; char low_c; printf("pb­>b(0x%x)  pb­>c(0x%x)n",pb­>b,pb­>c ); char hi_c; printf("pa­>a(0x%x)n",pa­>a ); }; free( pa ); } elpam.tw@gmail.com
  • 14. Structure (III) (cont') ● Memory Casting ● x86 is little­endian pc ­> hi_b = 0x12; pc ­> low_b = 0x34; pc ­> hi_c = 0x56; pc ­> low_c = 0x78;   #>./1­3 pb­>b(0x1234)  pb­>c(0x5678) pa­>a(0x56781234)   #> elpam.tw@gmail.com
  • 16. C++ Review /* 1­6.cpp */ class A{ ● Casting to Parent    public:       int a; }; class B : public A{    public:       int b; }; int main() { B b; b . b  = 2; A(b) . a = 1 } elpam.tw@gmail.com
  • 17. Inherit int main() { ● sizeof(A) =  8  struct B* pb = malloc(sizeof(struct B));   pb ­> parent . a = 0; ● sizeof(B) = 12   pb ­> parent . b = 1;   pb ­> c = 2 /* 1­4.h */ } struct A{    int a;    int b; /* 1­4.c */ Inherit A }; int main() struct B{ {    struct A parent;  struct A* pa =malloc(sizeof(struct B));    int c;    struct B* pb = pa; };   pa ­> a = 0;   pa ­> b = 1;   #>./1­4   pb ­> c = 2; pa(1,2) pb(3) }   #> elpam.tw@gmail.com
  • 18. Inherit & Casting /* 1­5.c */ ● Casting to Parent int main() {  struct B* pb =malloc(sizeof(struct B));   pb ­> c = 3;  struct A* pa = pb; /* 1­5.h */   pa ­> a = 1; struct A{   pa ­> a = 2;    int a;    int b;   printf("pa(%d,%d) pb(%d)n" }; ,pa­>a,pa­>b,pb­>c ); struct B{ }    struct A parent;    int c;   };   #>./1­5 pa(1,2) pb(3)   #> elpam.tw@gmail.com
  • 19. Public Member Value in C++ int main() { class A{   B* b = new B; public:    int a;   b . a = 0;    int b;   b . b = 1; };   b . c = 2; class B : public A { } public:    int c;   }; int main() {   A* a = new b;   a . a = 0;   a . b = 1;   a . c = 2;       /* error */   B(a) . c = 2;  /* correct */ } elpam.tw@gmail.com
  • 20. Object Inherit ● C Language ● C++ struct A{ class A{    int a; public:    int b;    int a; };    int b; struct B{ };    struct A parent; class B : public A {    int c;   public: };    int c;   }; elpam.tw@gmail.com
  • 21. Type Casting ( 形態轉換 ) ● C Language ● C++ – Structure­to­Structure – dynamic_cast ● Address­to­Address – static_cast – reinterpret_cast – const_cast elpam.tw@gmail.com
  • 22. Inherit in C ● C ­> B ­> A /* error memory access */ int main() struct A{ {    int v1;  struct B* pb = malloc(sizeof(B));    int v2;  struct C* pc = pb; /* correct */ };  pc ­> v1 = 0; /* run time error */ struct B{    struct A parent; }    int v1;   }; struct C{    struct B parent; struct C c;    int v1;   struct B* pb = &c;  /* use B's member */ }; struct A* pb = &c;  /* use A's member */ elpam.tw@gmail.com
  • 23. Conclusion  我們可以使用 memory casting 的技巧來描述繼承關係 *  因為缺乏語法 (Compiler) 的保護,所以我們必須 要小心的使用 elpam.tw@gmail.com
  • 24. Questions ● 建構子 (Constructor)  還有需要嗎 ? ● 但 C++ 的繼承還提供了 Virtual Function,  需要 這樣的功能嗎 ?  該如何實作 ? – 下一個投影片將討論這兩個問題 elpam.tw@gmail.com