关闭

Step-by-step learning C + +(chapter four summary____Array and Pointers)

219人阅读 评论(0) 收藏 举报

Chapter Four Summary

    Lower-level compound types arrays and pointers that are similar t o vectors and iterators.

1. Operations on Arrays

  • Subscript a vector : size_type as the type for the index
  • Subscript a array  : size_t as the type for the index

2. Checking Subscript Values

  • Buffer overflow :These bugs occur when a subscript is not checked and reference is made to an element outside the bounds of an array or other similar data structure.

3. Avoid Uninitialized Pointers

  • Uninitialized pointers are a common source of run-time errors

4. void * Pointer

        The type void* is a special pointer type that can hold an address of any object

5. Comparing Pointers and References

  •  Same point: both references and pointers are used to indirectly access another value
  •  Important Differences between reference and pointers

                       Firstly, a reference always to an object. (Alway refers to the same underlying object)

                         It is an error define a reference without initializing it.

              Secondly, the behavior of assignment

                         Assigning to a reference changes the object to which the reference is bound.

                         Pointer maybe does rebind the reference to another object.

6. Pointer arithmetic

  • Type * p ;            p+4; 

                when we add 4 to the pointer p ,we are computing a new pointer.

  • ptrdiff_t n = ip1 - ip2

              The result of subscript two pointers is a library type name ptrdiff_t (where as ptrdiff_t is a signed integral type).

7. Pointers Are Iterator for Arrays

8. Pointers and the const Qualifier

  • const Type *cptr                     //cptr may point to a Type that is const
  • Type *const cptr                     //cptr is a const
  • const Type *const cptr;           // cptr is const and points to a const object
  • const Pointer to a const Object

9. Pointers and Typedefs

  • typedef and pointers

                   typedef string *pstring;

           const pstring cstr; //equivalent to string *const cstr

  • typedef and macro(#define)

            typedef string *ptstring;

            #define string *pdstring;

           ptstring cstr,cstr2;// Objects is the same type of string *, it equivalent to string *cstr,*cstr2

           pdstring dstr,dstr2;// dstr is point to a string object , but dstr2 is a string object , it equivalent to string *dstr,dstr2

10. Never Forget About the NULL-Terminator

         char ca[] = {'a','b','c'}; //no null-terminated

      cout << strlen(ca) << endl; //disaster: ca isn't null-terminated

           The strlen function assumes that it can rely on finding a null charater at the end of its argument.

11. Whenever Possible , Use library string 

  • Now the library handles all memory management , and we need to longer worry if the size of either string changes

12. Dynamically Allocating Arrays

  • A variable of array type has three important limitations

                  ~~~Size is fixed

           ~~~Size must be known at compile time

           ~~~The array exists only until the end of the block in which it was defined

  • Dynamically arrays

                  ~~~The size of dynamically allocated array need not be fixed at compile time

           ~~~The size can be determined at run time

           ~~~A dynamically allocated array continues to exist until it is explicitly freed by the program

                   Because of this a pool of available memory is referred to as the program's free store or heap

  • Defining a Dynamically Array

                  int *ip = new int[10]; // array of 10 uninitialized ints

          string *ps = new string[10]; // array of 10 empty strings , it is initialized

          ~~~When we allocated an array of objects of a class type , then that type's default constructor is used to initialize each element.

          ~~~If the array holds elements of built-in type ,then the lements are uninitialized.

13.It is legal to dynamically allocate an empty array

              char arr[0]; // error: cannot define zero-length array

         char *cp = new char[0]; //ok:but cp can't be dereferened

14. Freeing Dynamic Memory

             When we allocate memory ,we must eventually free it. Otherwise ,memory is grabually used up and may be exhausted.

          ------------delete[] pointerToDynamicArrayName;

Note: If the empty bracket pair([]) is omitted , it is an error, bun an error that the compile is unlikely to catch; the program may fail at run time.


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:4426次
    • 积分:159
    • 等级:
    • 排名:千里之外
    • 原创:10篇
    • 转载:1篇
    • 译文:1篇
    • 评论:0条
    文章分类
    文章存档