C ++ new usage

Source: Internet
Author: User
In C ++, the usage of New is very flexible in C ++. Here is a simple summary: "') X5 T # D/S # V) v9 n w! J
1. New () allocates a memory space of this type and initializes this variable with the value in parentheses;. u * t, V #~ % U
2. New [] allocates n Memory Spaces of this type and uses the default constructor to initialize these variables. 8 A6 r I) '6g % m L
# Include <iostream>
. | 1 U "Q6 Z (y '? /V3 R A1 ~ # Include <cstring>
) W $ t/m, X8 N6 V9 qusing namespace STD;) @ 8 B 'J-I, F W $ q $ Z
Int main () {7 o8 Z "H1/4 J8 A8 R
// Char * P = new char ("hello"); 6 @ 0 @ 4 A8 C %/6 r
// Error allocates a char (1 byte) space,
$ S K1 S (K9 H. Y; M & // use "hello" for initialization, which is obviously incorrect
7 C7 B3 q )~ (M. F (? 6 {char * P = new char [6];
-C8 p $ A 'l1 x0 K0 E // P = "hello ";
% ^; ^ & B7 F! K3 B! {9 y // The character string cannot be directly assigned to the pointer P because: 'c3 {6 _) L7 P % Q2 U "s
// The pointer P points to the first character of the string and can only use the following * I3 M * Z2 D; A +/"L7 M # T
// Strcpy
/M, F4 o % x9 ') s! Ystrcpy (P, "hello ");
! L1 H7 J1 t F3 U ([! Ecout <* P <Endl; // only outputs the first character of the string pointed to by P!
3 B6} q0 a %/4 Z "'" s * bcout <p <Endl; // output the string pointed to by P!
* B3 R 'B' _; D, idelete [] P;-Q % K E W; y
Return 0;} 7 U) M4 | * B8 e 'P: V
Output result:
6 B-X2 U/N $ U0 X-L6 ^ h
# K * t 'n5 E & C: lhello; P0 X % Z! [5 V8 J; l! ~; ^ & D //
3. When the new operator is used to define a multi-dimensional array variable or array object, it generates a pointer to the first element of the array. The returned type retains all dimensions except the leftmost dimension. For example:
6 m3 I * I4 a %} 0 F! I1 U/J int * P1 = new int [10];
0 G5 X-D % F5 ^ * M returns an int pointer int * + L $ U; A + [0 u8 K
INT (* P2) [10] = new int [2] [10];
, {2 T: M $ R! P; ': M6 y (hnew has a two-dimensional array, removing the leftmost one [2] and leaving int [10], therefore, a pointer int (*) [10] pointing to a one-dimensional array such as int [10] is returned.
) F {'j1 ~ 4 E6 S/J, N % [int (* P3) [2] [10] = new int [5] [2] [10]; A new three-dimensional array, remove the leftmost one [5] and INT [2] [10]. Therefore, a pointer int (*) pointing to the two-dimensional array int [2] [10] is returned (*) [2] [10].
6 M0 x +/%] 6 y * F & I # include <iostream>
6 K0 | 9 t @ 5 C $ L3 D8 '# include <typeinfo>
& V) M: ^ 6 J: Y5] (x 'uusing namespace STD;-@. q B $ F: [(h
Int main (){
# @ % A + K2 K; U; L * @/qint * A = new int [34];
(U * O7 Z! Q7 lint * B = new int [];
-U % a3 ~ 2 F + W: I & xint (* C) [2] = new; M/L1] 3 L) I] "g1 s" x/S & H
Int [34] [2];
; F, J "? F2 M8 V -~ 6 xint (* D) [2] = new int [] [2];
4G; s: R6 T-^ $ V: hint (* E) [2] [3] = new int [34] [2] [3];) S, _ 6 B & F4 J/C-z/y/L-u, G "P
INT (* f) [2] [3] = new int [] [2] [3];
7? 4 i8 U. K6 S (D-|, _: D5 k m $ H a [0] = 1; + P6 N0 a "v" W: J3 E
B [0] = 1; // runtime error, no memory allocation, B only acts as a pointer, used to point to the corresponding data/P9 N _ + _-R ./
C [0] [0] = 1;
) T-y $ U1 @ 9 o "U0 E: X) T. rd [0] [0] = 1; // runtime error, no allocated memory, d only acts as a pointer, used to point to the corresponding data-I0 ^: D (C; n-! V "J
E [0] [0] [0] = 1;
/T-J5 I (k # Y. t5 X6 [f [0] [0] [0] = 1; // runtime error, no allocated memory, F only acts as a pointer, used to point to the corresponding data
, |: O (} 2/$ x * F6 | % J. fcout <typeid (a). Name () <Endl ;! U. D "d # Q (I % R
Cout <typeid (B). Name () <Endl;: B8 l % Z-W4 V6 e $ Y3 y! [& D
Cout <typeid (c). Name () <Endl;
8 [5 A2 I + B $ P/X! ~ : B/bcout <typeid (d). Name () <Endl;. _ 9 H9 K3 g /?
Cout <typeid (e). Name () <Endl;
+ J + '& Q5 V (K cout <typeid (f ). name () <Endl ;. j9 '% R * s) L * A8 U "}; D *~
Delete [] A; Delete [] B; Delete [] C;
2 L7 B "/9 ^; U1 W 'C) Q7 |:] Delete [] D; Delete [] E; Delete [] F; 5] 5 H0 s "F3 G-r-D. b8 R/J
} & Y6 R1 l0 J & I % R6 F & C1
Output result:
0} 9 R0 I % S $} int * (} "Q #]" U % v) P! /8 _ "y! ]
Int *
3 Z4 T1 D * K % H4 D (S % A. P % N % RINT (*) [2]
3 Q (o) Z! K'w3 T5 X8 o'] int (*) [2]
1 R2? $ D9 ~; J # R5 k'x: T, Bint (*) [2] [3] $ V/U (A: P &[
INT (*) [2] [3]
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.