(Abstract) C ++ primer plus, Fourth Edition-using a dynamic array

Source: Internet
Author: User

After you create a dynamic array, how do you use it? First, think about the problem conceptually. The statement

Int * psome = new int [10]; // get a block of 10 ints

Creates a pointerPsomeThat points to the first element of a block of tenIntValues. Think of it as a finger pointing to that element. SupposeIntOccupies four bytes. then, by moving your finger four bytes in the correct direction, you can point to the second element. altogether, there are ten elements, which is the range over which you can move your finger. thus,NewStatement supplies you with all the information you need to identify every element in the block.

Now think about the problem practically. How do you access one of these elements? The first element is no problem. BecausePsomePoints to the first element of the array,* PsomeIs the value of the first element. that leaves nine more elements to access. the simplest way may surprise you if you haven't worked with C: Just use the pointer as if it were an array name. that is, you can usePsome [0]Instead* PsomeFor the first element,Psome [1]For the second element, and so on. it turns out to be very simple to use a pointer to access a dynamic array, even if it may not immediately be obvious why the method works. the reason you can do this is that C and C ++ handle arrays internally by using pointers anyway. this near equivalence of arrays and pointers is one of the beauties of C and C ++. we'll elaborate on this equivalence in a moment. first, listing 4.13 shows how you can useNewTo create a dynamic array and then use array notation to access the elements. It also points out a fundamental difference between a pointer and a true array name.

Listing 4.13. Arraynew. cpp
// arraynew.cpp _ using the new operator for arrays
#include <iostream>
using namespace std;
int main()
...{
    double * p3 = new double [3]; // space for 3 doubles
    p3[0] = 0.2;                  // treat p3 like an array name
    p3[1] = 0.5;
    p3[2] = 0.8;
    cout << "p3[1] is " << p3[1] << ". ";
    p3 = p3 + 1;                  // increment the pointer
    cout << "Now p3[0] is " << p3[0] << " and ";
    cout << "p3[1] is " << p3[1] << ". ";
    p3 = p3 - 1;                  // point back to beginning
    delete [] p3;                 // free the memory
    return 0;
}

Here is the output:

p3[1] is 0.5.
Now p3[0] is 0.5 and p3[1] is 0.8.

As you can see,Arraynew. cppUses the pointerP3As if it were the name of an array,P3 [0]As the first element, and so on. The fundamental difference between an array name and a pointer shows in the following line:

p3 = p3 + 1; // okay for pointers, wrong for array names

You can't change the value of an array name. But a pointer is a variable, hence you can change its value. Note the effect of adding 1P3. The expressionP3 [0]Now refers to the former second element of the array. Thus adding 1P3Causes it to point to the second element instead of the First. subtracting one takes the pointer back to its original value so that the program can provideDelete []With the correct address.

The actual addresses of consecutiveIntS typically differ by two or four bytes, so the fact that adding 1P3Gives the address of the next element suggests that there is something special about pointer arithmetic. There is.

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.