logoalt Hacker News

dupedyesterday at 10:35 PM2 repliesview on HN

People use `new` and `delete` when explaining memory in C++ because those are the language primitives for allocating and releasing memory in C++.

That rule of thumb is only a useful rule if you don't care about how memory works and are comfortable with abstractions like RAII. That's fine for lots of real code but dismissing `new` and `delete` on principle is not interesting or productive for any discussion.


Replies

tialaramextoday at 12:49 AM

Also they're operators.

I understand C++ has a lot of operators which are variously reserved but not standardized ("asm") largely obsolete but still needed because of perverse C programmers ("goto") still reserved long after their usefulness subsided ("register") or for ideas that are now abandoned ("synchronized") not to mention all its primitive types ("double", "signed", "long", "short", "char8_t") and redundant non-textual operators given ASCII names like ("and_eq", "bitand", "xor")

But it also has dozens, like new and delete which look like features you'd want. So kinda makes sense to at least mention them in this context.

show 1 reply
jurschreudertoday at 5:08 AM

No the primitives are:

{

  // allocate

  auto my_obj = MyObj{}
} // released