C + +: The nature and difference of different types of pointers

Source: Internet
Author: User

From: http://blog.csdn.net/richerg85/article/details/10076365 pointer type (the kind of a Pointer)

How does a pointer to Zooanimal (a class) differ from a pointer to an integral type or pointer to a template array?

Zoolanimal *px;
int *pi;
array< String > *pta;

In terms of memory requirements, there is no difference. Each of the three types has enough memory to place a machine address (usually a word). The difference between "pointing to different types of pointers" is not that the pointer represents a different method, nor does it differ in its content (representing an address), but rather in the different types of objects (object) that it addresses. That is, the pointer type teaches the compiler how to resolve memory content and its size in a particular address.

The Zooanimal class is defined as follows:

Class zooanimal{
Public
Zoolanimal ();
Virtual ~zoolanimal ();
//...
virtual void rotate ();

Protected
int Loc;
String name;
};

Zooanimal za ("Zoey");
Zooanimal *pza = &za;

Analyze the above code:

Figure 1: Object layout and pointer layout for non-derived classes

1, an integer pointer to address 1000, on the 32-bit machine, will cover the address space 1000~1003 (4 bytes)

2. If string is a traditional 8-bytes (containing a 4-bytes character pointer and an integer used to represent the length of the string), then a zooanimal pointer will span the address space 1000~1015 (4+8+4) (? This place is not too clear, such as int Loc accounts for 4 bytes,string of 8 bytes, and 4 bytes are vptr virtual table or address 1000 accounted for? I think it is vptr virtual table accounted for. )

So what address space will be covered by a pointer to address 1000 and type void*? We don't know that! This is why a pointer of type void* can only contain an address and cannot manipulate the object it refers to.

So, transformation (cast) or type conversion, in fact, is a compiler directive, in most cases it does not change the actual address contained in a pointer, it only affects the "size of the indicated memory and its content" interpretation method. (the next article will read the meaning of this sentence)

Learn the "explore the C + + object Model" in detail.

C + +: The nature and difference of different types of pointers

Related Article

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.