Changeset 506d4f0 for tests/zombies/constructors.c
- Timestamp:
- Oct 1, 2020, 8:17:34 AM (2 years ago)
- Branches:
- arm-eh, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast-unique-expr, pthread-emulation, qualifiedEnum
- Children:
- 17b6fc9, ebec8ed
- Parents:
- e96e439
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
tests/zombies/constructors.c
re96e439 r506d4f0 1 int fred() { 1 #include <fstream.hfa> 2 #include <stdlib.hfa> 3 4 int main() { 2 5 // initialize basic structure 3 6 struct S { 4 7 int i, j, k; 5 8 }; 6 void ?{}( S *s ) { s->i = 1, s->k = 2; } // default constructor7 void ?{}( S *s, int i, int k ) { s->i = i, s->k = k; } // 2 parameter constructor8 void ?{}( S *s, S c ) { *s = c; }// copy constructor9 void ^?{}( S *s ) { s->i = 0, s->k = 0; } // default destructor10 void ^?{}( S *s, int i ) { s->i = i, s->k = i; } // 1 parameter destructor9 void ?{}( S & s ) { s.i = 1, s.k = 2; } // default constructor 10 void ?{}( S & s, int i, int k ) { s.i = i, s.k = k; } // 2 parameter constructor 11 void ?{}( S & s, S c ) { /* s @= c */ s.[i,j,k] = c.[i,j,k]; } // copy constructor 12 void ^?{}( S & s ) { s.i = 0, s.k = 0; } // default destructor 13 void ^?{}( S & s, int i ) { s.i = i, s.k = i; } // 1 parameter destructor 11 14 { 12 S s1; // default constructor13 S s2 = { 3, 7 }; // 2 parameter constructor14 S s3 @= { .k:3, .i:7 }; // 2 parameter C initialization15 ?{}( &s3, 2, 5 );// explicit 2 parameter constructor16 ^?{}( &s1 );// explicit call to default destructor15 S s1; // default constructor 16 S s2 = { 3, 7 }; // 2 parameter constructor 17 S s3 @= { .k:3, .i:7 }; // 2 parameter C initialization 18 ?{}( s3, 2, 5 ); // explicit 2 parameter constructor 19 ^?{}( s1 ); // explicit call to default destructor 17 20 } // implicit call to default destructor for s2, explicit call s1, no call for s3 18 S s4 @= {}; // no default construction19 ( &s4){ 2, 5 };// explicit 2 parameter constructor20 ^s4{ 3 }; // explicit call to 1 parameter destructor21 S s4 @= {}; // no default construction 22 (s4){ 2, 5 }; // explicit 2 parameter constructor 23 ^s4{ 3 }; // explicit call to 1 parameter destructor 21 24 22 25 // initialize pointer to a basic structure 23 26 24 void ?{}( S * *s ) { *s = malloc(); (*s)->i = 1, (*s)->k = 2; } // default constructor25 void ?{}( S * *s, int i, int k ) { *s = malloc(); (*s)->i = i, (*s)->k = k; } // 2 parameter constructor26 void ^?{}( S * *s ) { (*s)->i = 0, (*s)->k = 0; free( *s ); *s = 0; } // default destructor27 void ?{}( S *& s ) { s = malloc(); s->i = 1, (*s).k = 2; } // default constructor 28 void ?{}( S *& s, int i, int k ) { s = malloc(); (*s).i = i, (*s).k = k; } // 2 parameter constructor 29 void ^?{}( S *& s ) { (*s).i = 0, (*s).k = 0; free( s ); &s = 0p; } // default destructor 27 30 { 28 S *ps1; // default constructor 29 S *ps2 = { 3, 7 }; // 2 parameter constructor 30 S *ps3 @= 0; // C initialization 31 S *ps4 @= {}; // no default construction 31 S * ps1; // default constructor 32 S * ps2 = { 3, 7 }; // 2 parameter constructor 33 sout | ps1 | ps2; 34 35 S * ps3 @= 0p; // C initialization 36 S * ps4 @= { 3 }; // no default construction 37 sout | ps3 | ps4; 38 39 ?{}( ps3, 2, 5 ); // explicit 2 parameter constructor 40 (ps4){ 2, 5 }; // explicit 2 parameter constructor 41 sout | ps3 | ps4; 42 43 ^?{}( ps3 ); // explicit call to default destructor 44 ^ps4{}; // explicit call to default destructor 45 sout | ps3 | ps4; 32 46 } // implicit call to default destructor for ps2 and ps1, checks ordering of explicit destructor calls 33 34 ?{}( &ps3, 2, 5 ); // explicit 2 parameter constructor35 (&ps4){ 2, 5 }; // explicit 2 parameter constructor36 37 ^?{}( &ps3 ); // explicit call to default destructor38 ^ps4{}; // explicit call to default destructor39 47 40 48 // initialize complex structure … … 44 52 }; 45 53 46 void ?{}( T *t ) {} // default constructor => implicitly call constructor for field s 47 void ?{}( T *t, int i, int k ) { (&t->s){ i, k }; } // 2 parameter constructor => explicitly call constructor for field s 48 void ?{}( T *t, S c ) { (&t->s){ c }; } // 1 parameter constructor => explicitly call copy constructor for field s 49 void ^?{}( T *s, int i ) {} // destructor => implicitly call destructor for field s 54 void ?{}( T & t ) {} // default constructor => implicitly call constructor for field s 55 void ?{}( T & t, int i, int k ) { (t.s){ i, k }; } // 2 parameter constructor => explicitly call constructor for field s 56 void ?{}( T & t, S c ) { (t.s){ c }; }// 1 parameter constructor => explicitly call copy constructor for field s 57 void ^?{}( T & s ) {} // destructor => implicitly call destructor for field s 58 void ^?{}( T & s, int i ) {}// destructor => implicitly call destructor for field s 50 59 { 51 S s; // default constructor 52 T t1; // default constructor 53 T t2 = { s }; // 1 parameter constructor 54 ^?{}( &t1 ); // explicit call to default destructor => implicit call to t1.s's destructor 60 S s; // default constructor 61 T t1; // default constructor 62 T t2 = { s }; // 1 parameter constructor 63 ^?{}( t1, 3 ); // explicit call to default destructor => implicit call to t1.s's destructor 64 T t3; // default constructor 65 T t4 @= { { 1, 3 } }; // C initialization 66 (t4){ 2, 5 }; // explicit 2 parameter constructor 55 67 } // implicit call to default destructor for t2 and implicit call for s; 56 T t3; // default constructor57 T t4 @= { { 1, 3 } }; // C initialization58 (&t4){ 2, 5 }; // explicit 2 parameter constructor59 68 60 69 T *pt = malloc(){ 3, 4 }; // common usage
Note: See TracChangeset
for help on using the changeset viewer.