Talk:New and delete (C++)

From Wikipedia, the free encyclopedia
  (Redirected from Talk:New (C++))
Jump to: navigation, search


I did not know there is new operator in C also. Could anyone confirm it? --Leo 05:35, 18 March 2006 (UTC)

int main() {
        int *p=new int;

test.c:2: error: `new' undeclared (first use in this function)
test.c:2: error: (Each undeclared identifier is reported only once
test.c:2: error: for each function it appears in.)
test.c:2: error: syntax error before "int" 05:39, 18 March 2006 (UTC)

I deleted C implementation. --Leo 15:54, 8 April 2006 (UTC)

I think it would be good to add examples for multi-dimensional arrays.
Also, a reference to STL vector seems appropriate.
(but I'm afraid to add it myself :( ) 19:02, 31 August 2007 (UTC)

Error Handling[edit]

It would be good if this article covered how new handles error such as 'out of memory' errors, and how it can throw and exception and call a different function. Stephenbez 09:16, 26 September 2007 (UTC)


  1. When memory is allocated dynamically using new or array new, is this memory a contiguous piece of memory?
Yes. (and now without the oversimplification: yes, as far as the application knows)
  1. One more question regarding delete[]... From the article: Note that the compiler will generate neither a warning nor an error for using the wrong delete; it cannot know in general whether a pointer is to a single element or an array of elements....... then, how does the runtime know how many elements there are to deallocate memory from?
delete[] for the most part exists for non-POD (plain-old-data - things without destructors), and on all platforms I'm aware of, delete[] and delete both work correctly on POD objects. It knows the same way malloc knows (by storing a huge tree of available and unavailable blocks and their length) in this case.
In the case of non-POD, new[] can store the number of objects in an integer immediately before the pointer that it (new) returns. So it mallocs the number of bytes that you really need for your allocation, plus the size of an integer, uses the first sizeof(int) bytes of that pointer to record the number of elements new[]ed, and then returns a pointer to the first chunk of memory after that int. As a result, you have to use delete[] instead of delete in order to know that you need to subtract sizeof(int) from the pointer, and to know how many destructors to call. njaard (talk) 05:44, 22 April 2008 (UTC)
I should make an adjustment to this explanation. C++ was originally implemented with C in mind, which means that new should be implemented in terms of malloc. Nowadays, it's possible some (I'm not sure which, if any) can use malloc's data structures to get the number of objects that need to be destructed, meaning that delete[] is more or less unnecessary; I wish I had more information for you. njaard (talk) 06:40, 7 May 2008 (UTC)

Thanks, --Abdull (talk) 16:44, 6 February 2008 (UTC)

But does the C++ standard specify why a statement of the form T *v = new T; (used with delete v;) doesn't just compile to the same code as T *v = new T[1]; (used with delete[] v;)? --Damian Yerrick (talk | stalk) 01:39, 13 November 2008 (UTC)


contemporary C++ doesn't support entries like: int[] p_array = new int [5]

so there is a confusing information in the "Syntax" chapter.

int * p_array = new int [5]

should be used instead

qdoj, Poland, Gliwice —Preceding unsigned comment added by (talk) 15:10, 15 April 2008 (UTC)

Placement new[edit]

What about placement new? (talk) 10:55, 10 July 2008 (UTC)

I agree. There should be a description of the placement new. It was part of the original ANSI standard, it's implemented on all compilers, and many users may be looking for help with its syntax. BTW, the syntax is "new (void *) ClassName(...)" where that extra void pointer is where you want the new object. Scott Bowden (talk) 15:29, 20 November 2008 (UTC)


How is a C++ operator notable? I think the article should encompass the new operator in various programming languages, especially object oriented ones. Otherwise, I don't believe it is notable, and the article certainly doesn't assert its notability. — FatalError 05:33, 1 August 2008 (UTC)

I agree, should include other languages ... such as javanishantjr (talk) 15:46, 19 November 2008 (UTC)
Here's what to do: Make new (operator) describing the other languages, and then merge this article into that one. --Damian Yerrick (talk | stalk) 04:17, 17 December 2008 (UTC)

Memory reallocation via placement new operator[edit]

This does not reallocate memory as claimed by the article previously

#include <new>
class Test { 
   int a,b,c; 
   Test() { a=b=c=10; } 
int main() { 
   Test *t; 
   t = new Test; 
   t = new(t) Test[5]; 
   return 0; 

What the placement new operator (new() Class[]) does is construct objects in already allocated memory. As the memory allocated was only large enough to store one object constructing 5 will cause undefined behaviour by writing to unallocated memory. (talk) 08:20, 30 September 2012 (UTC)