Java Programmers learn C + + arrays and dynamic arrays

Source: Internet
Author: User

Array:

#include <iostream>using namespace std;//templates function template <class t>void dump (T val) {cout << >>> > << __function__ << endl;//built-in macros that print the name of the current function cout << sizeof (val) << ":" << typeID (val). Name () << endl;cout << <<<< << __function__ << endl << Endl;  void dump1 (int a[]) {cout << "a=" << a << endl;//address of the first element cout << "&a=" << &a <<  endl;//pointer cout << "a+1=" << A + 1 << endl;//array The address of the second element (+4) cout << "&a+1=" << &a + 1 << endl;//The next variable that points to the pointer address (+4)}int main () {const int len = 6;int A[len] = {0, 2, 4, 6, 8};//Assign less one okay, no assignment will have a default value of 0int *pa = A;int *PA2 = a;//a is a pointer to the first element of the array cout << "a=" << a << endl << "&a[0]=" << & (a[ 0]) << endl;for (int i = 0; i < len;i++) {A[i] = i;cout << "a[" << I << "]=" << A[i] <& Lt "<<" * (a+i) = "<< * (A + i) << "" << "* (i+a) =" << * (i + a) << ""//by the above expression according to the binding law of addition << "i[a]=" << I[a] << "" cout << * (a++) << endl;//compilation error, A is the first address of the array (equivalent to a constant), no longer can be assigned to a value << "* (pa++) =" << * (pa++) << "" & lt;< "pa[" << I << "]=" << Pa2[i] << "<<" * (pa+i) = "<< * (PA2 + i) <<" "& lt;< "i[pa]=" << i[pa2 "<<" "//Integrated, operation array can be either by array name or by pointer variable << Endl; cout << "=================== array type and pointer type =======================" << endl;cout << sizeof (a) << ":" << typeID (a). Name () << Endl;//24:int[6]dump (a);//4:int*//within the scope of the array declaration, a is the array type; outside the scope (including function passing), A degenerate into a shaping pointer cout << "==================== array type take address ======================" << endl;cout << "a=" <<  A << endl;cout << "&a=" << &a << endl;//and the values above are the same as the first element's address cout << "a+1=" << a+1 << endl;//The address of the second element of the array (+4) cout << "&a+1=" << &a+1 << endl;//the address of a variable after the array (+4*6) cout << "==================== pointer type takes address ======================" << endl;dump1 (a); return 0;}

Dynamic arrays:

#include <iostream>using namespace std;/*c++ memory management  stack: The compiler automatically allocates and deletes the variable's area      storage such as local variables, function parameters, etc.  efficient but limited  space Heap: Storage allocated and deleted by application      does not release allocated memory causes memory leak  global/static storage: Store global variables and static variables  constant store: Store constants, do not allow modification */int main () {//dynamic array definition format: int *a = new int[array_size];//release format: delete []a;int len = 0;cin >> len;int *p = new Int[len];for (int i = 0; i < len;i++) {P[i] = i;cout << "p[i]=" << P[i] << Endl;} delete []p;return 0;}

Difference:








Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java Programmers learn C + + arrays and dynamic arrays

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.