Tuesday, December 13, 2005

 

C++ Tutorial

  1. C++ Tutorial
  2. C++ StrTok
  3. Gary's Note
  4. Microsoft MSDN developer
  5. Pointer

Pointer功能強大且好用,但在使用之前,先要initialized,不然它會指向任意處,會造成電腦記憶體不預期的問題。

pointer is initialized to point to a specific memory address before it is used. If this was not the case, it could be pointing to anything. This can lead to extremely unpleasant consequences to the computer. You should always initialize pointers before you use them.

#include 
using namespace std;
int main()
        { int x;
        int *p;
        p = &x;
        cin>> x;
        cin.ignore();
        cout<< *p <<"\n";
        cin.get(); }
int *ptr = new int;

It initializes ptr to point to a memory address of size int (because variables have different sizes, number of bytes, this is necessary). The memory that is pointed to becomes unavailable to other programs. This means that the careful coder should free this memory at the end of its usage. Allocate完區塊的memory,在程式執行完後,必須要釋 放memory(就是透過delete ptr;),然後指定此pointer為null pointer

After deleting a pointer, it is a good idea to reset it to point to 0. When 0 is assigned to a pointer, the pointer becomes a null pointer, in other words, it points to nothing.


Comments: Post a Comment



<< Home

This page is powered by Blogger. Isn't yours?