Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 5 of 5

Thread: Why this code does not crash ?

  1. #1
    Member
    Join Date
    Mar 2019
    Location
    Bordeaux / France
    Posts
    69

    Why this code does not crash ?

    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);
    }

  2. #2
    Senior Member
    Join Date
    Jul 2014
    Posts
    2,254
    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

  3. #3
    Member
    Join Date
    Mar 2019
    Location
    Bordeaux / France
    Posts
    69
    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...

  4. #4
    Member
    Join Date
    Mar 2019
    Location
    Bordeaux / France
    Posts
    69
    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);

  5. #5
    Member
    Join Date
    Mar 2019
    Location
    Bordeaux / France
    Posts
    69
    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...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •