constructor the shared structure in-place

5825 ワード

new(mCblk) audio_track_cblk_t();   placement new
 1 #include <iostream>

 2 #include <stdio.h>

 3 #include <malloc.h>

 4 

 5 class Student

 6 {

 7     private:

 8         int age;

 9         int grade;

10     public:

11         Student(void)

12         {

13             age = 5;

14             grade = 10;

15         }

16 

17         ~Student(void)

18         {

19             printf("xx
"); 20 } 21 }; 22 23 int main(void) 24 { 25 Student *pStudent = (Student*)malloc(sizeof(Student)); 26 char *p = (char*)pStudent; 27 28 int i = 0; 29 for(i = 0; i < sizeof(Student); i++) 30 *p++ = 0; 31 32 p = (char*)pStudent; 33 34 new(pStudent) Student(); 35 36 for(i = 0; i < sizeof(Student); i++) 37 { 38 printf("%u
", (unsigned int)(*p) & off); 39 p++; 40 } 41 delete pStudent; 42 return 0; 43 } 44 45 x86 : 46 5 47 0 48 0 49 0 50 10 51 0 52 0 53 0