Why this code does not crash ?

Status
Not open for further replies.

Tactif CIE

Well-known member
Not related with Teensy but in my experiments with hard_fault_isr() I'm trying to produce weird, stupid crashes... I thought this silly code would not run, but yes it runs, and I really don't understand why

Any idea ?

Code:
#include <Arduino.h>

char array[8192] = {0xAA};

class Foo {
    int b = 1;

  public:
    void some() {
        Serial.println("Some");
    }
    void boid(int i) {
        Serial.printf("boid %i %ld\n", i, millis());
    }
};

class Bar {
  public:
    void none() {
        Serial.println("none");
    }
};

void setup() {
    Serial.begin(115200);
    while (millis() < 800)
        ;
}

void loop() {
    Foo *foo_ptr;
    Bar *bar_ptr;
    foo_ptr = (Foo *)&bar_ptr;
    foo_ptr->boid(1);
    foo_ptr = (Foo *)0L;
    foo_ptr->boid(2);
    foo_ptr = (Foo *)&array;
    foo_ptr->boid(3);
    delay(500);
}
 
even this does work (better: does not crash)
Code:
void loop() {
    Foo *foo_ptr;
    Bar *bar_ptr;
    foo_ptr = (Foo *)0L;
    foo_ptr->boid(1);
    delay(500);
}
on T3.6; A1.8.8; Td1.46b8
 
And this one :


Code:
void loop() {
    Foo *foo_ptr;
    Bar *bar_ptr = new Bar();
    bar_ptr->none();
    foo_ptr = (Foo *)bar_ptr;
    foo_ptr->boid(1);    
    delay(500);
}

WTF ? bar_ptr is pointing to a Bar instance, force casted to a Foo* pointer, running boid() and it does the job ? Come on...
 
OMG

Code:
void loop() {
    Foo *foo_ptr;
    Bar *bar_ptr = new Bar();
    bar_ptr->none();
    uint32_t iam_pure_junk[1024];
    iam_pure_junk[0] = (uint32_t)malloc(sizeof(Bar));
    memset((void *)iam_pure_junk[0], 0x99, 3);
    ((Foo *)iam_pure_junk[0])->boid(32);
    foo_ptr = (Foo *)bar_ptr;
    foo_ptr->boid(1);    
    delay(500);
}

This prints 32

Code:
((Foo *)iam_pure_junk[0])->boid(32);
 
And I get the same result with code converted to clang/macOS... It's beyond me... I don't understand nothing of what is going on...
 
Status
Not open for further replies.
Back
Top