Why C + + member function pointers are 16-byte wide

Source: Internet
Author: User

When referring to pointers, we generally think that it is a 8-byte object that can be represented by the void * pointer on the x86_64 architecture. For example, Wikipedia has an article on x86_64 that reads:

Pushes and pops on the stack is always in 8-byte strides, and pointers is 8 bytes wide.

From the CPU's point of view, the pointer is just a memory address, and all memory addresses in x86_64 are represented by 64 bits, so the 8-byte hypothesis is set. This conclusion can be achieved simply by printing different types of pointer sizes.

#include <iostream>

int main () {

Std::cout "

"sizeof (int*) = =" sizeof (int*) "\ n"

"sizeof (double*) = =" sizeof (double*) "\ n"

"sizeof (void (*) ()) = =" sizeof (void (*) ()) "STD::ENDL;

}

Compiling and running the program, the results clearly state that all pointers are 8 bytes:

$ uname-i

x86_64

$ g++-wall./example.cc

$./a.out

sizeof (int*) = = 8

sizeof (double*) = = 8

sizeof (void (*) ()) = = 8

But there is one exception in C + +-pointers to member functions.

More interestingly, the size of the member function pointer is exactly twice times the size of the other pointers. This can be verified by the following simple program, which prints "16":

#include <iostream>

struct Foo {

void Bar () const {}

};

int main () {

Std::cout "sizeof (&foo::bar)" STD::ENDL;

}

is Wikipedia wrong? Of course not. For all hardware, all pointers still have a width of 8 bytes. What exactly is the member function pointer? It is actually an attribute of the C + + language and is a virtual address that cannot correspond to the hardware (physical) address one by one. Because it is implemented by the C + + compiler at runtime (converting member function pointers to actual virtual memory addresses, along with other related work), this feature can incur a slight run-time overhead resulting in a performance penalty. The C + + specification does not care about a specific language implementation, so it does not make too much of a pointer to that class. Fortunately, the Itanium C + + ABI specification (Itanium C + + Application Binary interface specification is dedicated to standardizing the implementation of the C + + runtime) In addition to virtual table (virtual tables), RTTI (runtime type recognition), and exceptions (exception) Implementation of the §2.3, the member function pointers are also explained in the following section:

Each pointer to a member function is divided into the following two parts:

Ptr:

If the pointer points to a non-virtual member function, the field is a simple function pointer. Manhattan Casino If the pointer is to a virtual function member, the value of the field is the offset value of the virtual function member in its virtual table plus 1, expressed in C + + with the ptrdiff_t type. A value of 0 indicates a NULL pointer, regardless of the adjustment field value below.

Adj

When a member function is called, the position of the this pointer must be adjusted (Translator Note: This is related to the C + + object memory model, ensuring that each member function correctly accesses the various function members referenced in its function body, which is further explained below), expressed in C + + with the ptrdiff_t type.

A member function pointer is 16 bits, because in addition to the 8-bit byte to store the address of the function, a field of address size (8 bytes) is required to store information about how the this pointer position is adjusted (common sense: whenever a non-static member function is called, the this pointer is implicitly passed to the function by the compiler So that the various members of the calling object are properly accessed through the pointer within the body of the function. The ABI specification above does not explain why and when it is necessary to adjust the position of the this pointer.

Recommended Reading http://www.yupoo.com/photos/summo2/93898660/
Http://www.cnblogs.com/huangdj/p/4142633.html

Why C + + member function pointers are 16-byte wide

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.