Usage of new in c ++

Source: Internet
Author: User

In c ++, the usage of new is flexible. 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; 2. new [] allocates n Memory Spaces of this type and uses the default constructor to initialize these variables; # include <iostream>
# Include <cstring>
Using 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,
// Use "Hello" for initialization. This is obviously incorrect.
Char * p = new char [6];
// P = "Hello ";
// 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
Strcpy (p, "Hello ");
Cout <* p <endl; // only outputs the first character of the string pointed to by p!
Cout <p <endl; // output the string pointed to by p!
Delete [] p;-q % k E w; Y
Return 0;} 7 U) m4 | * B8 e 'P: v
Output result:
H
Hello; 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:
Int * p1 = new int [10];
Returns an int x + l $ U; a + [0 U8 K
Int (* p2) [10] = new int [2] [10];
A two-dimensional array is added, and the leftmost one [2] is removed, with int [10] Left. Therefore, a pointer int (*) pointing to a one-dimensional array such as int [10] is returned (*) [10].
Int (* p3) [2] [10] = new int [5] [2] [10]; new a three-dimensional array, remove the leftmost one [5], there is also int [2] [10], so a pointer to the two-dimensional array int [2] [10] type int (*) [2] [10] is returned.
# Include <iostream>
# Include <typeinfo>
Using namespace std;-@. Q B $ f: [(h
Int main (){
Int * a = new int [34];
Int * B = new int [];
Int (* c) [2] = new; M/L1] 3 L) I] "G1 s" x/s & H
Int [34] [2];
Int (* d) [2] = new int [] [2];
Int (* 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];
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;
D [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;
F [0] [0] [0] = 1; // runtime error, no memory allocated. f only acts as a pointer and is used to point to the corresponding data
Cout <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;
Cout <typeid (d). name () <endl;. _ 9 h9 K3 g /?
Cout <typeid (e). name () <endl;
Cout <typeid (f). name () <endl;. j9' % R * S) l * a8 u "}; D *~
Delete [] a; delete [] B; delete [] c;
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:
Int * (} "q #]" u % v) P! \ 8 _ "Y! ]
Int *
Int (*) [2]
Int (*) [2]
Int (*) [2] [3] $ v/u (a: p &[
Int (*) [2] [3]


Note the differences between parentheses () and brackets []. Otherwise, errors may occur. I have suffered a loss on this issue. I hope you will pay more attention to it.

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.