Talk:New and delete (C++)

message: I did not know there is new operator in C also. Could anyone confirm it? --Leo 05:35, 18 March 2006 (UTC)
replies: text: I deleted C implementation. --Leo 15:54, 8 April 2006 (UTC)
thread_title: Untitled

message: 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)
thread_title: Error Handling

message: #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)
#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)
replies: text: 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)
thread_title:  Heap 

message: 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)
thread_title:  Syntax 

message: What about placement new? 155.198.233.90 (talk) 10:55, 10 July 2008 (UTC)
thread_title: Placement new

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

text: 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)
thread_title:  Notable? 

message: 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)
thread_title:  Memory reallocation via placement new operator