3

I am not going to use this in practice. This is intended to understand the language spec.

I am trying to construct a C++ object in heap, using malloc and explicit initialization of fields. While for many interesting classes this is not possible without invoking UB, for some very simple classes, this seems legal to me. I thought to check if my understanding is correct (i.e. Does C++ somehow prevents the developer from bypassing constructor?). For example is the following legal?

class A {
  public:
  int a;
  int b;

  // constructor declaration and definition
  // destructor declaration and definition
  // other member functions.
};

int main() {
  A *a = (A *) malloc (sizeof(A));
  a->a = 1;
  a->b = 2;
  delete A;
}
esam
  • 580
  • 3
  • 13
  • 5
    Which version of C++? These rules underwent a major update in C++20 – NathanOliver Jul 13 '22 at 23:00
  • @RetiredNinja Given that question and its answers are several years old and predate C++20, I'm pretty sure this is not a dupe. – Andrew Henle Jul 13 '22 at 23:03
  • *Does C++ somehow prevents the developer from bypassing constructor?* **No**, you can do that, at your own peril. C++ is not a nanny language. It gives you enough rope to shoot yourself in the foot. If you're careful, there are situations where what you want to do is allowed. And if you do it in the situations where is it not allowed, all sorts of interesting things could happen (even appearing to work as expected). – Eljay Jul 13 '22 at 23:04
  • 2
    @Eljay Actually you can now. C++20 adds implicit lifetime types. – NathanOliver Jul 13 '22 at 23:05
  • @NathanOliver • Agreed, that is why I said **no**, because you are **not** *prevented*. But you do have to be careful/mindful and follow the rules. – Eljay Jul 13 '22 at 23:12
  • 6
    Beware: you are `delete`ing something you’ve `malloc`ed. that’s UB. You at least need to `free` it. – Ben Jul 13 '22 at 23:18
  • 1
    Doing `delete A` when `A` was initialised with a `malloc()` call definitely gives undefined behaviour. To avoid undefined behaviour, a pointer returned by `malloc()` should be released, either by `realloc()` [in circumstances where `realloc()` actually reallocates] or `free()`]. – Peter Jul 13 '22 at 23:19
  • 1
    @AndrewHenle This isn't tagged C++20, but I'd love to see an updated answer for it. – Retired Ninja Jul 13 '22 at 23:21
  • @Peter And I've actually seen that mistake in shipping code written by ... Microsoft. – Paul Sanders Jul 13 '22 at 23:31
  • @PaulSanders I can assure you that Microsoft isn't alone in having developers who make such mistakes in shipping code. – Peter Jul 13 '22 at 23:49
  • @Peter Oh sure, but this was a lulu. It broke 'Edit and Continue' (my favourite feature) _completely_. Ironically, it caused one of their 'safe' routines to misfire and silently swallow the error, so things just mysteriously didn't work right. – Paul Sanders Jul 13 '22 at 23:51
  • Quis custodiet ipsos custodes? – user4581301 Jul 14 '22 at 00:02
  • @user4581301 Ubi sunt dracones non est certus praesidio – Peter Jul 14 '22 at 01:24
  • Praesidio... that's in San Francisco, Right? Not sure what that has to do with dragons, though. – user4581301 Jul 14 '22 at 04:15

0 Answers0