Talk:New (C++)

From Wikipedia, the free encyclopedia

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"

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

[edit] Error Handling

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)

[edit] Heap

  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)

[edit] Syntax

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)