[C++]C++メモ---Pointers,Vectors and Array Objects

6785 ワード

ポインタの説明:
ポインタの説明を開始する前に、&アドレス付けと*値取り記号の2つの記号を理解する必要があります.
int n = 10     //&n    n   
cout <<"The address of n is" << &n << endl;

int * pn = &n;   //  pn  n   ,*pn pn       。
cout <<"The value that pn points to is" << *pn<<endll

int * pt;
pt is a pointer that points to a "int"type value.
double * pd;
pd is a pointer that points to a "double"type value.
New and Delete a pointer
The pointers that we mentioned above all points to a memory location that were declared and initialised by other variables. 
Pointers can points to a memory location that was declared and initialised by itself using new.
Typename * pointerName = new Typename;

int * pn = new int;
*pn = 100;

pointers that are created by new must be deleted later after use. The delete only free the memory that pn points, it does not delete the pointer itself.
delete pn;

Create array dynamically
To create a dynamic array in C++, 
int * pn = new int[10]; //new Type-of-array No-of_elements
*pn = 1;  //The first element
pn[0] = 1;  //first element
pn[1] = 2;  //second
*(pn+1) =2;  //second

The new operator returns the address of the first element of the array. The brackets tells the program that to remove the whole array but not the pointer itself.
delete[] pn;

The name of an array is the address of the first element of that array, and alias, the address of the array.
int ar[3] = {1,2,3};

In short, you should observe these rules when you usenew and delete:
  • Don’t use deleteto free memory thatnewdidn’t allocate.
  • Don’t use deleteto free the same block of memory twice in succession.nUsedelete []if you usednew []to allocate an array.
  • Use delete(no brackets) if you usednewto allocate a single entity.
  • It’s safe to applydeleteto the null pointer (nothing happens). 
  • int * pn = new int[10]; // new int[10] creates a array of 10 int and return the address of the first element of the array.
    pn[1] == *(pn+1)  //The value of the second element of the array

    キーワードnewは、メモリ領域を宣言/取得するために使用できます.だからもし一つ
    struct Tapstry { int x, int y};
    Tapstry * tps = new Tapstry;//tps pointer points to the address of type Tapstry struct.
    tps->x = 1;
    (*tps).y = 2;
    The C-style string
    C style stringとは、文字列が文字配列で保存され、文字列の終わりに'0'で終わることを意味します.
    char chars[] = {'H','e','l','l','o','\0'};
    char chars[] = "Hello" //implicate add '\0' at the end of Hello
    char chars[] = "tests";
    char chars_b[] = {'H','e','l','l','o','\0'};
    cout<<chars<<chars_b<<endl;

    Result:
    testsHello

    実はポインタを使ってstringを使うことができます."HelloString" 
    	const char * aaa = "HelloString";  
    	char * css = new char[strlen(aaa) + 1];
    	strcpy(css, aaa);
    	cout << css << aaa << endl;
    	delete[] css;

    Result:
    HelloStringHelloString

    ポインタ配列とポインタを指すポインタ
    int a = 10;
    int b = 20;
    const int * pa[2] = {&a, &b};
    int ** pTop= pa;

    paは配列であり、彼の要素はポインタであり、paはポインタ配列とも呼ばれる.const intはpaの要素定数を表す. 
           pTop    >      pa                 int
             0x..    /       0x...         ->     a
                                0x...        ->      b
    paもポインタであり、その最初の要素、which is also a pointer、so pa is a pointer to a pointerを指す.so that 
    int ** pTop = pa;     
    stands.
    Vectors(C++ 98) and Array Objects(C++ 11)
    Vector is  a dynamic class that its size can change during run time. It uses 'new' and 'delete' automatically, so that it stores on the HEAP.
    Vector<Type> vector_variable_name(no_of_elems);
    

    no_of_elems can be a variable as the size of the vector is resizable.
    Array Object  is a fixed size and use stack as build-in array, but with more safety. To use Array object, we need to include header.
    array<TypeName, no_of_elems> array_obj_variable_name;

    no_of_elems can not be a variable and must be a const number because array object is a fixed-size.
    Array object can be assigned to another array object, well build-in array types can only be copied element-by-element.
    void ChapterFour::showPointers() {
    	double da[2] = {1.3, 2.4};
    	int vsize = 2;
    	vector<int> va(2);
    	va[0] = 10;
    	va[1] = 20;
    	int ia[2] = {1,2};
    	cout<< "value of build-in array da[1] "<< da[1] << " the address " << &da[1] <<endl;
    	cout <<" value of build-in array ia[1] " << ia[1] << " the address " << &ia[1] << endl;
    	cout <<" value of build-in array ia[-1] " << ia[-1] << " the address " << &ia[-1] << endl;
    	cout <<" value of vector va[1] " << va[1] << " the address " << &va[1] << endl;
    }

    Result:
    value of build-in array da[1] 2.4 the address 0x7fff3cda39e8
     value of build-in array ia[1] 2 the address 0x7fff3cda39f4
     value of build-in array ia[-1] 1073951539 the address 0x7fff3cda39ec
     value of vector va[-1] 20 the address 0xba5194

    The result shows that:
  • the build-in array are stored on stack, and their locations are very close to each other
  • the vector are stored on heap and its address is different.
  • build-in array index have -1 

  • 変数をメモリに格納する方法
  • 自動記憶:メソッドで宣言された変数は自動記憶であり、stackに保存され、Last in,first out.終了メソッドが戻ると、メモリは
  • に回収される.
  • 静的記憶、static variable.定義された場所からプログラム実行終了まで、グローバルに有効です.
  • 動的記憶、newとdelete.宣言された変数はheapに保存されます.