From 7e0f021a9aec35fd8e6725e87e3313b101d26f5e Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Sun, 27 Jan 2008 11:37:44 +0100 Subject: Initial import (2.0.2-6) --- reference/CPLUSPLUS/SYNTAX/new.html | 125 ++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 reference/CPLUSPLUS/SYNTAX/new.html (limited to 'reference/CPLUSPLUS/SYNTAX/new.html') diff --git a/reference/CPLUSPLUS/SYNTAX/new.html b/reference/CPLUSPLUS/SYNTAX/new.html new file mode 100644 index 0000000..bd077fd --- /dev/null +++ b/reference/CPLUSPLUS/SYNTAX/new.html @@ -0,0 +1,125 @@ +NEW operator + + + +
+

NEW operator.

+
+

+The new operator replaces the +malloc +function provided in C to reserve storage taken from the heap. +

+The new operator requires one operand that describes the +type of data to be stored in the memory. Here are some examples. +

+

+ + + + +
+
+
+    int *ptr1;                  // Declare a pointer to int.
+    ptr1 = new int;             // Reserve storage and point to it.
+    
+    float *ptr2 = new float;    // Do it all in one statement.
+    
+    delete ptr1;                // Free the storage.
+    delete ptr2;
+    
+    struct House                // Declare a complex structure.
+    {
+        int Floors;
+        int Windows;
+    };
+    
+    House *ptr3 = new House;    // Reserve storage and point to it.
+
+    delete ptr3;
+
+
+
+

+ +Blocks or arrays of storage can also be reserved as show in the +next example. + +

+

+ + + + +
+
+
+    char *ptr
+    ptr = new char[80];
+    
+    delete [] ptr;          // Free the storage.
+
+
+
+

+ + + +Although new and +malloc +can be intermixed in the same code, it is probably best +to continue using +malloc +in existing code and new in new code. This is because +you can not use +delete with storage thats been +reserved with malloc and visa versa. If you keep to one +method or the other you are less likely to make a mistake. +

+


+

Examples:

+ +Example program. +
+

See Also:

+ +delete keyword. + +
+
+ +

C References

+

+ +malloc function. + +

+ +free function. + + +


+

+

+ + + + +
+Top + +Master Index + +C++ Keywords + +Functions +
+
+

+


+ +
Martin Leslie +17-Feb-96

+ + -- cgit v1.2.3-54-g00ecf