Manual calling of a one-dimensional array and a two-dimensional array in C ++

Source: Internet
Author: User

In the course of project development or C ++ learning at ordinary times, everyone will open an array, but those who are not proficient in pointers will still be unclear about the principle of the array, frequent memory call Bugs. As we all know, in C ++, the essence of arrays is pointers (which should be taught by teachers ). But what exactly do you know about array pointer operations? 1. the array pointer is const * T and cannot be directly modified. The following [cpp] int intArray [10]; intArray ++; is incorrect. But there is an exception, that is, when an array is used as a function parameter. (1) When the array name is used as a function parameter, In the function body, it loses its own meaning and is just a pointer. (2) Unfortunately, while losing its meaning, it also loses its constant feature and can perform auto-increment, auto-subtraction, and other operations and can be modified. In this case, [cpp] double * x = new double [10]; x ++; no error is reported, and the pointer is shifted normally. If you manually open an array, you can use the [] subscript to perform operations. That is to say, if you use new, you are more free. Of course, the error probability is greater. If you remember correctly, java seems to declare the array like this. [Cpp] # include <iostream> using namespace std; int a [10] = {1, 2, 4, 5, 6, 7, 8, 9, 10}; int main () {int * c = new int [10]; c [0] = 1; c [1] = 2; c [2] = 3; the space in cout <c ++ <endl; cout <* (c ++) <endl;} new is not released, and the delete operation should be released. For a two-dimensional array, [cpp] int ** x; // Why are two asterisks? Because the memory address is stored in the first dimension of the computer, the memory address points to another one-dimensional array, A two-dimensional array is essentially equivalent to multiplying two one-dimensional arrays. X = new int * [10]; // The first dimension is essentially a pointer for (int I = 0; I <10; I ++) x [I] = new int [10]; // the release of a two-dimensional array must correspond to [cpp] for (int I = 0; I <10; I ++) delete [] x [I]; // delete the row pointer delete [] x; x = 0; if the memory is too large, do not release it, if the memory is insufficient, the system will issue a bad_alloc exception and catch it. I learned so much today .. Finally, it is common sense that sizeof () is an operator and is definitely not a function.

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.