image.png

    PPT:
    image.png
    image.png
    image.png
    image.png
    image.png
    image.png
    image.png
    image.png
    image.png
    image.png
    image.png
    image.png
    image.png

    CODE1:

    1. #include <iostream>
    2. #include <string>
    3. using namespace std;
    4. class Test
    5. {
    6. static const unsigned int COUNT = 4;
    7. static char c_buffer[];
    8. static char c_map[];
    9. int m_value;
    10. public:
    11. void* operator new(unsigned int size)
    12. {
    13. void* ret = NULL;
    14. for(int i = 0; i < COUNT; i++)
    15. {
    16. if( !c_map[i] )
    17. {
    18. c_map[i] = 1;
    19. ret = c_buffer + i * sizeof(Test);
    20. cout << "succeed to allocate memory: " << ret << endl;
    21. break;
    22. }
    23. }
    24. return ret;
    25. }
    26. void operator delete(void* p)
    27. {
    28. if( p != NULL )
    29. {
    30. char* mem = reinterpret_cast<char*>(p);
    31. int index = (mem - c_buffer) / sizeof(Test);
    32. int flag = (mem - c_buffer) % sizeof(Test);
    33. if( (flag == 0) && ( 0 <= index) && (index < COUNT))
    34. {
    35. c_map[index] = 0;
    36. cout << "succeed to free memory: " << p << endl;
    37. }
    38. }
    39. }
    40. };
    41. char Test:: c_buffer[sizeof(Test) * Test::COUNT] = {0};
    42. char Test:: c_map[Test::COUNT] ={0};
    43. int main()
    44. {
    45. Test* pt = new Test;
    46. delete pt;
    47. Test* pa[5] = {0};
    48. for( int i = 0; i < 5; i++)
    49. {
    50. pa[i] = new Test;
    51. cout << " pa[" << i << "] =" << pa[i] << endl;
    52. }
    53. for( int i = 0; i < 5; i++)
    54. {
    55. cout << " pa[" << i << "] =" << pa[i] << endl;
    56. delete pa[i];
    57. }
    58. return 0;
    59. }

    CODE2:

    1. #include <iostream>
    2. #include <string>
    3. #include <cstdlib>
    4. using namespace std;
    5. class Test
    6. {
    7. static unsigned int c_count;
    8. static char* c_buffer;
    9. static char* c_map;
    10. int m_value;
    11. public:
    12. static bool SetMemorySource(char* memory, unsigned int size)
    13. {
    14. bool ret = false;
    15. c_count = size / sizeof(Test);
    16. ret = (c_count && (c_map = reinterpret_cast<char*>(calloc(c_count,sizeof(char)))));
    17. if(ret)
    18. {
    19. c_buffer = memory;
    20. }
    21. else
    22. {
    23. free(c_map);
    24. c_map = NULL;
    25. c_buffer = NULL;
    26. c_count = 0;
    27. }
    28. return ret;
    29. }
    30. void* operator new(unsigned int size)
    31. {
    32. void* ret = NULL;
    33. if(c_count > 0)
    34. {
    35. for(int i = 0; i < c_count; i++)
    36. {
    37. if( !c_map[i] )
    38. {
    39. c_map[i] = 1;
    40. ret = c_buffer + i * sizeof(Test);
    41. cout << "succeed to allocate memory: " << ret << endl;
    42. break;
    43. }
    44. }
    45. }
    46. else
    47. {
    48. ret = malloc(size);
    49. }
    50. return ret;
    51. }
    52. void operator delete(void* p)
    53. {
    54. if( p != NULL )
    55. {
    56. if(c_count > 0)
    57. {
    58. char* mem = reinterpret_cast<char*>(p);
    59. int index = (mem - c_buffer) / sizeof(Test);
    60. int flag = (mem - c_buffer) % sizeof(Test);
    61. if( (flag == 0) && ( 0 <= index) && (index < c_count))
    62. {
    63. c_map[index] = 0;
    64. cout << "succeed to free memory: " << p << endl;
    65. }
    66. }
    67. else
    68. {
    69. free(p);
    70. }
    71. }
    72. }
    73. };
    74. unsigned int Test:: c_count = 0;
    75. char* Test:: c_buffer = NULL;
    76. char* Test:: c_map = NULL;
    77. int main()
    78. {
    79. char buffer[12] = {0};//只能申请3个
    80. Test:: SetMemorySource(buffer,sizeof(buffer));
    81. Test* pt = new Test;
    82. delete pt;
    83. Test* pa[5] = {0};
    84. for( int i = 0; i < 5; i++)
    85. {
    86. pa[i] = new Test;
    87. cout << " pa[" << i << "] =" << pa[i] << endl;
    88. }
    89. for( int i = 0; i < 5; i++)
    90. {
    91. cout << " pa[" << i << "] =" << pa[i] << endl;
    92. delete pa[i];
    93. }
    94. return 0;
    95. }

    CODE3:

    1. #include <iostream>
    2. #include <string>
    3. #include <cstdlib>
    4. using namespace std;
    5. class Test
    6. {
    7. int m_value;
    8. public:
    9. Test()
    10. {
    11. m_value = 0;
    12. }
    13. ~Test()
    14. {
    15. }
    16. void* operator new (unsigned int size)
    17. {
    18. cout << "operator new: " << size << endl;
    19. return malloc(size);
    20. }
    21. void operator delete (void* p)
    22. {
    23. cout << "operator delete: " << p << endl;
    24. free(p);
    25. }
    26. void* operator new[] (unsigned int size)
    27. {
    28. cout << "operator new[]: " << size << endl;
    29. return malloc(size);
    30. }
    31. void operator delete[] (void* p)
    32. {
    33. cout << "operator delete[]: " << p << endl;
    34. free(p);
    35. }
    36. };
    37. int main(int argc, char *argv[])
    38. {
    39. Test* pt = NULL;
    40. pt = new Test;
    41. delete pt;
    42. pt = new Test[5];
    43. delete[] pt;
    44. return 0;
    45. }