|
| 1 | + |
| 2 | +// show arduino_new benefits |
| 3 | +// released to public domain |
| 4 | +// result is below |
| 5 | + |
| 6 | +class SomeClass { |
| 7 | + public: |
| 8 | + SomeClass(const String& s1 = emptyString, const String& s2 = emptyString) { |
| 9 | + Serial.printf("SomeClass@%p(%s)(%s)\n", this, s1.c_str(), s2.c_str()); |
| 10 | + } |
| 11 | + |
| 12 | + ~SomeClass() { |
| 13 | + Serial.printf("~ SomeClass @%p\n", this); |
| 14 | + } |
| 15 | +}; |
| 16 | + |
| 17 | +class oom { |
| 18 | + private: |
| 19 | + char large [65000]; |
| 20 | + public: |
| 21 | + oom() { |
| 22 | + Serial.printf("this constructor should not be called\n"); |
| 23 | + } |
| 24 | +}; |
| 25 | + |
| 26 | +void setup() { |
| 27 | + |
| 28 | + Serial.begin(115200); |
| 29 | + Serial.printf("\n\narduino_new benefits\n\n"); |
| 30 | + delay(5000); // avoid too frequent bootloop |
| 31 | + |
| 32 | + // arduino_new / arduino_newarray api |
| 33 | + |
| 34 | + Serial.printf("\n----- arduino_new:\n"); |
| 35 | + auto an = arduino_new(SomeClass); |
| 36 | + delete an; |
| 37 | + |
| 38 | + Serial.printf("\n----- arduino_new with oom:\n"); |
| 39 | + auto anoom = arduino_new(oom); |
| 40 | + Serial.printf("nullptr: %p\n", anoom); |
| 41 | + delete anoom; |
| 42 | + |
| 43 | + Serial.printf("\n----- arduino_new with constructor parameters:\n"); |
| 44 | + auto ancp = arduino_new(SomeClass, "param1", "param2"); |
| 45 | + delete ancp; |
| 46 | + |
| 47 | + Serial.printf("\n----- arduino_newarray[2]\n"); |
| 48 | + auto ana2 = arduino_newarray(SomeClass, 2); |
| 49 | + Serial.printf("@:%p s=%zd s(2)=%zd\n", ana2, sizeof(SomeClass), sizeof(SomeClass[2])); |
| 50 | + Serial.printf("0: %p\n", &ana2[0]); |
| 51 | + Serial.printf("1: %p\n", &ana2[1]); |
| 52 | + delete [] ana2; |
| 53 | + |
| 54 | + Serial.printf("\n----- arduino_newarray[2] (with constructor parameters)\n"); |
| 55 | + auto ana2cp = arduino_newarray(SomeClass, 2, "param1"); |
| 56 | + Serial.printf("@:%p s=%zd s(2)=%zd\n", ana2cp, sizeof(SomeClass), sizeof(SomeClass[2])); |
| 57 | + Serial.printf("0: %p\n", &ana2cp[0]); |
| 58 | + Serial.printf("1: %p\n", &ana2cp[1]); |
| 59 | + delete [] ana2cp; |
| 60 | + |
| 61 | + Serial.printf("\n----- arduino_newarray[100000]\n"); |
| 62 | + auto anaX = arduino_newarray(SomeClass, 100000); |
| 63 | + Serial.printf("@:%p\n", anaX); |
| 64 | + |
| 65 | + // standard c++ api for new and new[] |
| 66 | + |
| 67 | + Serial.printf("\n----- new\n"); |
| 68 | + auto sn = new SomeClass; |
| 69 | + delete sn; |
| 70 | + |
| 71 | + Serial.printf("\n----- new with oom: (abort() with option 'Exceptions: Disabled (new can abort)'\n"); |
| 72 | + auto snoom = new oom; |
| 73 | + Serial.printf("nullptr: %p\n", snoom); |
| 74 | + delete snoom; |
| 75 | + |
| 76 | + Serial.printf("\n----- new[2]\n"); |
| 77 | + auto sna2 = new SomeClass[2]; |
| 78 | + Serial.printf("@:%p s=%zd s(2)=%zd\n", sna2, sizeof(SomeClass), sizeof(SomeClass[2])); |
| 79 | + Serial.printf("0: %p\n", &sna2[0]); |
| 80 | + Serial.printf("1: %p\n", &sna2[1]); |
| 81 | + delete [] sna2; |
| 82 | + |
| 83 | + Serial.printf("\n----- new[10000] (badly fails with 'Exceptions: Legacy' or '...Disabled'\n"); |
| 84 | + auto snaX = new SomeClass[100000]; |
| 85 | + Serial.printf("@:%p\n", snaX); |
| 86 | +} |
| 87 | + |
| 88 | +void loop() { |
| 89 | +} |
| 90 | + |
| 91 | +////////////////////////////// |
| 92 | +/* |
| 93 | +
|
| 94 | + Result with: |
| 95 | + Exceptions: Legacy(new can return nullptr) |
| 96 | +
|
| 97 | + ////////////////////////////// |
| 98 | +
|
| 99 | + arduino_new benefits |
| 100 | +
|
| 101 | + ----- arduino_new: |
| 102 | + SomeClass@0x3fff1864()() |
| 103 | + ~ SomeClass @0x3fff1864 |
| 104 | +
|
| 105 | + ----- arduino_new with oom: |
| 106 | + nullptr: 0 |
| 107 | +
|
| 108 | + ----- arduino_new with constructor parameters: |
| 109 | + SomeClass@0x3fff1864(param1)(param2) |
| 110 | + ~ SomeClass @0x3fff1864 |
| 111 | +
|
| 112 | + ----- arduino_newarray[2] |
| 113 | + SomeClass@0x3fff1868()() |
| 114 | + SomeClass@0x3fff1869()() |
| 115 | + @: 0x3fff1868 s = 1 s(2) = 2 |
| 116 | + 0: 0x3fff1868 |
| 117 | + 1: 0x3fff1869 |
| 118 | + ~ SomeClass @0x3fff1869 |
| 119 | + ~ SomeClass @0x3fff1868 |
| 120 | +
|
| 121 | + ----- arduino_newarray[2](with constructor parameters) |
| 122 | + SomeClass@0x3fff1868(param1)() |
| 123 | + SomeClass@0x3fff1869(param1)() |
| 124 | + @: 0x3fff1868 s = 1 s(2) = 2 |
| 125 | + 0: 0x3fff1868 |
| 126 | + 1: 0x3fff1869 |
| 127 | + ~ SomeClass @0x3fff1869 |
| 128 | + ~ SomeClass @0x3fff1868 |
| 129 | +
|
| 130 | + ----- arduino_newarray[100000] |
| 131 | + @: 0 |
| 132 | +
|
| 133 | + ----- new |
| 134 | + SomeClass@0x3fff1864()() |
| 135 | + ~ SomeClass @0x3fff1864 |
| 136 | +
|
| 137 | + ----- new with oom: (abort() with option 'Exceptions: Disabled (new can abort)' |
| 138 | + this constructor should not be called |
| 139 | + nullptr: 0 |
| 140 | +
|
| 141 | + ----- new[2] |
| 142 | + SomeClass@0x3fff1868()() |
| 143 | + SomeClass@0x3fff1869()() |
| 144 | + @:0x3fff1868 s = 1 s(2) = 2 |
| 145 | + 0: 0x3fff1868 |
| 146 | + 1: 0x3fff1869 |
| 147 | + ~ SomeClass @0x3fff1869 |
| 148 | + ~ SomeClass @0x3fff1868 |
| 149 | +
|
| 150 | + ----- new[10000](badly fails with 'Exceptions: Legacy' or '...Disabled' |
| 151 | +
|
| 152 | + Exception(29): |
| 153 | + epc1 = 0x402013de epc2 = 0x00000000 epc3 = 0x00000000 excvaddr = 0x00000000 depc = 0x00000000 |
| 154 | +
|
| 155 | + >>> stack >>> |
| 156 | + ... |
| 157 | +
|
| 158 | +*/ |
| 159 | +///////////////////////////// |
0 commit comments