summaryrefslogtreecommitdiff
path: root/reference/CPLUSPLUS/SYNTAX/new.html
blob: bd077fd9023f26b95f3fe50c9b9c7c267a45797c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<title>NEW operator</title>
<!-- Changed by: Martin Leslie, 14-Mar-1996 -->
<body bgcolor="#dddddd">
<font color=brown>
<hr>
<center><h1>NEW operator.</h1></center>
<hr>
<p>
The <b>new</b> operator replaces the 
<a href="../../C/FUNCTIONS/malloc.html">malloc</a> 
function provided in C to reserve storage taken from the heap.
<p>
The <b>new</b> operator requires one operand that describes the 
type of data to be stored in the memory. Here are some examples.
<p>
<center>
<table bgcolor=ivory width="80%" border=1>
<tr>
<td>
<pre>

    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;
</pre>
</td>
</tr>
</table>
</center>
<p>

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

<p>
<center>
<table bgcolor=ivory width="80%" border=1>
<tr>
<td>
<pre>

    char *ptr
    ptr = new char[80];
    
    delete [] ptr;          // Free the storage.
</pre>
</td>
</tr>
</table>
</center>
<p>



Although <b>new</b> and 
<a href="../../C/FUNCTIONS/malloc.html">malloc</a>
can be intermixed in the same code, it is probably best
to continue using 
<a href="../../C/FUNCTIONS/malloc.html">malloc</a>
in existing code and <b>new</b> in new code. This is because
you can not use 
<a href="delete.html">delete</a> 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.
<p>
<hr>
<h2>Examples:</h2>
<img src="../../GRAPHICS/computer.gif">
<a href="../EXAMPLES/new.cc">Example program.</a>
<hr>
<h2>See Also:</h2>
<img src="../../GRAPHICS/whiteball.gif">
<a href="delete.html">delete</a> keyword.

</font>
<hr>
<font color=black>
<h2>C References</h2>
<p>
<img src="../../GRAPHICS/whiteball.gif">
<a href="../../C/FUNCTIONS/malloc.html">malloc</a> function.

<p>
<img src="../../GRAPHICS/whiteball.gif">
<a href="../../C/FUNCTIONS/free.html">free</a> function.
</font>

<hr>
<p>
<center>
<table border=2 width="80%" bgcolor="ivory">
<tr align=center>
<td width="25%">
<a href="../cref.html">Top</a>
</td><td width="25%">
<a href="../../C/master_index.html">Master Index</a>
</td><td width="25%">
<a href="keywords.html">C++ Keywords</a>
</td><td width="25%">
<a href="../../C/FUNCTIONS/funcref.htm">Functions</a>
</td>
</tr>
</table>
</center>
<p>
<hr>
<font color=brown>
<address>Martin Leslie 
17-Feb-96</address><p>
</font>