您的位置:首页 > 编程语言 > C语言/C++

C++复习Pointers

2017-09-28 14:14 106 查看

C++ details(Pointers)

A pointer variable is a variable that stores the address where another object resides. It is the fundamental mechanism used in many data structures. For instance, to store a list of items, we could use a contiguous array, but insertion into the middle of
the contiguous array requires relocation of many items. Rather than store the collection in an array, it is common to store each item in a separate, noncontiguous piece of memory, which is allocated as the program runs. Along with each object is a link to
the next object. This link is a pointer varable, because it stores a memory location of another object. this is the classic linked list.

int main()
{
    IntCell *m;
    m = new IntCell{0};
    m->write(5);
    cout<<"cell contents: "<<m->read()<<endl;
    delete m;
    return 0;
}

The *indicates that m is a pointer variable; It is allowed to point at an IntCell object. The value of m is the addressof the object that it points at. m is uninitialized at this point. In C++ , no such check is performed to verify that m is assigned a value
prior to being used . The use of uninitialized pointers typically crashes programs, because they result in acceess of memory locations that do not exist. In general , it is a good idea to provide an initial value, either by combining lines 3 and 5, or by initializing
m to the nullptr pointer.

In c++ new returns a pointer to the newly created object. In c++ there are several ways to create an object using its zero parameter constructor.:

m=new intcell();

m=new intcell{};

m=new intcell;

in some languages , when an object is no longer referenced, it is subject to automatic garbage collection, the programmer does not have to worry about it. c++does not have garbage collection. When an object that is allocated by new is nolonger referenced,
the delete operation must be applied to the object(through a pointer). Otherwise, the memory that it consumes is consumes is lost(until the program terminates). This is known as memory leak. Memory leaks are, unfortunately, common occurrences in many C++ programs.
fortunately, many sources of memory leaks can be aotumatically removed with care. One important rule is to not use new when an automatic variable can be used instead. In the original program, the intcell is not allocated by new but instead is allocated as
l local variable. inthat case, the memory for the intcell is automatically reclaimed when the function in which it is declared retruns.

Assignment and comparison of pointer varables in c++ is based on the value of the pointer, meaning the memory address that it stores. Thus two pointer variables are equal if they point at the object. if they point at different objects, the pointer variables
are not equal , even if the objects being pointed at are themselves equal. if lhs and rhs are pointer variables, the lhs=rhs makes lhs point at the same object that rhs points at.

Accessing members of an object through a pointer

if a pointer varable points at a class type, then a member of the object being pointed at can be accessed via the ->opterator. 

One important operator is the address of  operator &. this operator returns the memory location where an object resides and is useful for implementing an alias test;

我自己写的小网站www.caozhicong.com
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: