Questions tagged [new-operator]

new is a language construct that dynamically allocates memory from free store and initialises the memory using the constructor.

Use this tag in conjunction with a language specific tag.

The new operator in C++

The C++ standard library provides several overloads of operator new and it can also be overloaded for your own classes.

Use this tag for questions about dynamic allocation in C++ and questions about overloading operator new. The tag may also be relevant for questions about constructing objects in existing memory.

Memory allocated with new must be de-allocated using delete C++ operator.

Wiki for New in C++

The new operator in other languages

The new operator is used in several other languages as well, including C#, Java, Javascript, and Perl. Use this tag for questions about memory allocation and instantiating objects in those languages.

Some highlighted questions about

  1. Why should C++ programmers minimize use of 'new'?
  2. Do the parentheses after the type name make a difference with new?
  3. Passing arguments to C# generic new() of templated type
  4. Using "Object.create" instead of "new"
  5. Why does the use of 'new' cause memory leaks?
  6. C++ new int[0] -- will it allocate memory?
  7. Why would one replace default new and delete operators?
2953 questions
1920
votes
17 answers

What is the 'new' keyword in JavaScript?

The new keyword in JavaScript can be quite confusing when it is first encountered, as people tend to think that JavaScript is not an object-oriented programming language. What is it? What problems does it solve? When is it appropriate and when…
Alon Gubkin
  • 56,458
  • 54
  • 195
  • 288
1108
votes
8 answers

Do the parentheses after the type name make a difference with new?

If 'Test' is an ordinary class, is there any difference between: Test* test = new Test; and Test* test = new Test();
David Read
  • 11,031
  • 3
  • 17
  • 5
982
votes
19 answers

Why should C++ programmers minimize use of 'new'?

I stumbled upon Stack Overflow question Memory leak with std::string when using std::list, and one of the comments says this: Stop using new so much. I can't see any reason you used new anywhere you did. You can create objects by value…
bitgarden
  • 10,461
  • 3
  • 18
  • 25
589
votes
20 answers

In what cases do I use malloc and/or new?

I see in C++ there are multiple ways to allocate and free data and I understand that when you call malloc you should call free and when you use the new operator you should pair with delete and it is a mistake to mix the two (e.g. Calling free() on…
Ralph Burgess
512
votes
25 answers

What uses are there for "placement new"?

Has anyone here ever used C++'s "placement new"? If so, what for? It looks to me like it would only be useful on memory-mapped hardware.
Head Geek
  • 38,128
  • 22
  • 77
  • 87
477
votes
16 answers

Passing arguments to C# generic new() of templated type

I'm trying to create a new object of type T via its constructor when adding to the list. I'm getting a compile error: The error message is: 'T': cannot provide arguments when creating an instance of a variable But my classes do have a constructor…
LB.
  • 13,730
  • 24
  • 67
  • 102
417
votes
10 answers

Create an empty object in JavaScript with {} or new Object()?

There are two different ways to create an empty object in JavaScript: var objectA = {} var objectB = new Object() Is there any difference in how the script engine handles them? Is there any reason to use one over the other? Similarly it is also…
Jonas Pegerfalk
  • 9,016
  • 9
  • 29
  • 29
391
votes
15 answers

Using "Object.create" instead of "new"

Javascript 1.9.3 / ECMAScript 5 introduces Object.create, which Douglas Crockford amongst others has been advocating for a long time. How do I replace new in the code below with Object.create? var UserA = function(nameParam) { this.id =…
Graham King
  • 5,710
  • 3
  • 25
  • 23
361
votes
21 answers

How to open in default browser in C#

I am designing a small C# application and there is a web browser in it. I currently have all of my defaults on my computer say google chrome is my default browser, yet when I click a link in my application to open in a new window, it opens internet…
Sean
  • 8,401
  • 15
  • 40
  • 48
350
votes
12 answers

When should I use the new keyword in C++?

I've been using C++ for a short while, and I've been wondering about the new keyword. Simply, should I be using it, or not? With the new keyword... MyClass* myClass = new MyClass(); myClass->MyField = "Hello world!"; Without the new…
Nick Bolton
  • 38,276
  • 70
  • 174
  • 242
278
votes
10 answers

Is "delete this" allowed in C++?

Is it allowed to delete this; if the delete-statement is the last statement that will be executed on that instance of the class? Of course I'm sure that the object represented by the this-pointer is newly-created. I'm thinking about something like…
276
votes
6 answers

C++ new int[0] -- will it allocate memory?

A simple test app: cout << new int[0] << endl; outputs: 0x876c0b8 So it looks like it works. What does the standard say about this? Is it always legal to "allocate" empty block of memory?
anon
275
votes
6 answers

Can we omit parentheses when creating an object using the "new" operator?

I have seen objects being created this way: const obj = new Foo; But I thought that the parentheses are not optional when creating an object: const obj = new Foo(); Is the former way of creating objects valid and defined in the ECMAScript…
Behrang
  • 46,888
  • 25
  • 118
  • 160
273
votes
14 answers

Difference between new and override

Wondering what the difference is between the following: Case 1: Base Class public void DoIt(); Case 1: Inherited class public new void DoIt(); Case 2: Base Class public virtual void DoIt(); Case 2: Inherited class public override void…
Shiraz Bhaiji
  • 64,065
  • 34
  • 143
  • 252
233
votes
9 answers

Java FileOutputStream Create File if not exists

Is there a way to use FileOutputStream in a way that if a file (String filename) does not exist, then it will create it? FileOutputStream oFile = new FileOutputStream("score.txt", false);
Stefan Dunn
  • 5,363
  • 7
  • 48
  • 84
1
2 3
99 100