Keyword new in constructor crashes

Below are two versions, both crashes. Why?
I am using platformIO with Arduino, have tested on two different ES32 to rule out hardware problems.

class testNewClass2 {
  public:
    testNewClass2() : p1(new char(20)) {    }
    char *p1;
};

class testNewClass3 {
  public:
    testNewClass3() {
      p1 = new char(10);
    }
    char *p1;
};

With what platformio.ini and full src/main.cpp is that? Used package versions?

I made a new complete project that worked.
As expected I guess.
However, the example is part of a test project for ArduinoHA.h where the problem showed up first time.
I will go back and start over to from scratch.

Just of curiosity; have you any idea how/why such a simple example can fail (without having the complete code)?

Anyway, thank you replying.

I wouldn’t rely on guessing based on incomplete code. If you can reproduce it, and get a backtrace, then that can be decoded and understood.

Did you mean parentheses or square brackets?

You’re initializing a single character to a value, not an array of a given length, but I’m not sure what you’re trying to do.

I think levin is on the right track. you should give new a type, not a constant of some type. I am a bit surprised this crashes. I‘d expect your examples to produce compiler errors.