0

Why did we use "new" operator here and what is the role of "new" operator in C++?

int *p = new int; //Here in this line
*p = 5;

cout << *p << endl;

delete p;
  • 7
    There is no reason to use new in the code. Sounds like you could use a [good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – NathanOliver Oct 20 '20 at 14:57
  • @FrançoisAndrieux: OP's phrasing is correct, this is the **`new` operator**. Which as you say is distinct from the **deallocation function which is named `operator new()`** – Ben Voigt Oct 20 '20 at 14:57
  • 3
    This code looks like an example of how to allocate an object (in this case, an `int`) from the global store (typically the *heap*), assign it a value, print it out, and then delete the object (returning the allocated memory back to the global store). – Eljay Oct 20 '20 at 14:58
  • @Eljay free store* – Asteroids With Wings Oct 20 '20 at 14:59
  • ***Why did we use “new” operator here? (Please explain briefly)*** I would say it's for teaching purposes but in modern code you should avoid `new` there really is not a good reason to use `new` for this code. – drescherjm Oct 20 '20 at 14:59
  • To dereference a pointer (for example, using `*p`) it is necessary for that pointer to point at (at least one) valid object. Initialising `p` using `new int` is one way of achieving that. – Peter Oct 20 '20 at 15:00
  • @BenVoigt Can you please refer me to a definition for "new operator"? I can only find "operator new" and "new expression" and I assumed they meant "operator new". – François Andrieux Oct 20 '20 at 15:00
  • @FrançoisAndrieux You are correct. It's a [_new-expression_](http://eel.is/c++draft/expr.new) consisting, in part, of a keyword. There's no operator here. There is _one_ example of "new operator" in the language wording (http://eel.is/c++draft/expr.new#4) but that looks like an editorial mistake to me. – Asteroids With Wings Oct 20 '20 at 15:02
  • It looks like "new operator" [is a widely used term](https://stackoverflow.com/questions/1885849/difference-between-new-operator-and-operator-new) but it seems to me like it is not an actual term used by the language. – François Andrieux Oct 20 '20 at 15:04
  • @BenVoigt [`new` is not an operator](http://eel.is/c++draft/lex.operators). – Asteroids With Wings Oct 20 '20 at 15:04
  • @AsteroidsWithWings: It most certainly is an operator. See https://eel.is/c++draft/over.oper.general#nt:operator which defines "operator". – Ben Voigt Oct 20 '20 at 15:04
  • @BenVoigt No, that's the grammar production for constructing `operator new`. I have linked you already to the standard's definition of what is and is not an operator. I concede the terminology used in that grammar production is a little misleading (as not everything under the terminal _operator_ is actually an operator), but it's still fairly clear. – Asteroids With Wings Oct 20 '20 at 15:04
  • @AsteroidsWithWings: It's the grammar production for operators. See the footnote https://eel.is/c++draft/over.oper.general#note-1 which calls them "operators", twice. – Ben Voigt Oct 20 '20 at 15:06
  • @AsteroidsWithWings: You incorrectly linked the preprocessor operators. – Ben Voigt Oct 20 '20 at 15:07
  • 1
    @François - It's a colloquialism for the keyword in the new expression, used by the standard itself too https://timsong-cpp.github.io/cppwp/n4861/expr.new#4.sentence-3 - if memory serves, it used to be in the normative text back in antiquity, but only the note remains now. – StoryTeller - Unslander Monica Oct 20 '20 at 15:07
  • @Ben Those are _not_ just preprocessing operators. This is how the grammar is defined. Read the start of /1 again. I have nothing to add over what I've already said. You are, simply, mistaken. ‍♂️ – Asteroids With Wings Oct 20 '20 at 15:09
  • @AsteroidsWithWings: No, those are how tokens are defined. The standard explicitly says that not every operator is a single token, and also lists a new of them that tokenize as keywords. That does not mean they are not operators. – Ben Voigt Oct 20 '20 at 15:11
  • I've removed my comment. Although "new operator" is not technically correct, it seems the term is well established. Edit : I'd advise the use of the technically correct "new expression" in the future. – François Andrieux Oct 20 '20 at 15:12
  • 1
    @FrançoisAndrieux: It IS technically correct. `new` is an operator, along with `new[]`, `co_await`, `noexcept`, `sizeof`, and a whole host of operators that do not appear on the lexer's operator-or-punctuator list. – Ben Voigt Oct 20 '20 at 15:15
  • Lets move this out of the comments : https://stackoverflow.com/questions/64448328/is-new-in-new-int-considered-an-operator – François Andrieux Oct 20 '20 at 15:24
  • @AsteroidsWithWings • thank you for the correction. (Alas, past the window to edit it.) – Eljay Oct 20 '20 at 15:28
  • Thanks! now I am a PRO in this – Naitik Srivastava Nov 05 '20 at 13:33
  • Thanks! now I am a PRO in this – Naitik Srivastava Nov 05 '20 at 13:34

2 Answers2

5

In this particular example, there is no reason for dynamic allocation (which is what new provides).

It looks like a toy example to show how you would dynamically allocate, set, print, then delete an int.

In reality, you wouldn't do this unless you had a more complex type to create, or you really needed the int to be shared between scopes for some reason (though, even then, smart pointers are nowadays preferred).

Refer to your book for more information on dynamic allocation and when it is (and isn't) useful.

Asteroids With Wings
  • 16,164
  • 2
  • 17
  • 33
-1

You can do that if you want to create this variable in heap.