Last active
October 29, 2020 19:55
-
-
Save meowmeowxw/d58c925fe84aeb95d075e8e511de8656 to your computer and use it in GitHub Desktop.
Fill the heap of arduino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Counter { | |
public: | |
int count; | |
Counter(int value){ | |
count = value; | |
} | |
~Counter(){ | |
// destructor | |
} | |
void inc(){ | |
count++; | |
} | |
int getValue(){ | |
return count; | |
} | |
}; | |
void setup() { | |
Serial.begin(9600); | |
} | |
Counter *x; | |
void loop() { | |
int y; | |
x = new Counter(50); | |
if (x == 0) { | |
// No more space on the heap | |
Serial.println(*(int *)0); // -> 50 | |
x->count += 2; | |
Serial.println(*(int *)0); // -> 102 , probably the precedent operation overwrite the value of the register R0/R1 | |
delay(5000); | |
} else { | |
Serial.print((unsigned long)x, HEX); | |
Serial.print(String(" ")); | |
Serial.print((unsigned long)&y, HEX); | |
Serial.print(String(" ")); | |
Serial.println(*(int *)0); | |
delay(40); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment