Talk:New and delete (C++)

Page contents not supported in other languages.
From Wikipedia, the free encyclopedia

Untitled[edit]

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

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"

84.231.99.112 05:39, 18 March 2006 (UTC)[reply]

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

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 :( )
89.0.94.220 19:02, 31 August 2007 (UTC)[reply]

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)[reply]

Heap[edit]

  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)[reply]
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)[reply]

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

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)[reply]

Syntax[edit]

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 77.223.199.45 (talk) 15:10, 15 April 2008 (UTC)[reply]

Placement new[edit]

What about placement new? 155.198.233.90 (talk) 10:55, 10 July 2008 (UTC)[reply]

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)[reply]

Notable?[edit]

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)[reply]

I agree, should include other languages ... such as javanishantjr (talk) 15:46, 19 November 2008 (UTC)[reply]
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)[reply]

Memory reallocation via placement new operator[edit]

This does not reallocate memory as claimed by the article previously

#include <new>
class Test { 
public:
   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. 115.64.19.205 (talk) 08:20, 30 September 2012 (UTC)[reply]