View C ++ from the perspective of assembly (pointer 1)

Source: Internet
Author: User

[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]

 

Pointers are a type of data that we often encounter in C/C ++. The usage of pointers improves the readability of the Code. However, improper usage may cause a lot of trouble. Pointer, that is, to point to a data type address. There are many types, which can be the built-in types of programming languages, such as int, long, short, Char, float, double, and Int. They also point to a custom data type, it can make Union, struct, or class. Even the data type pointing to itself is a pointer, such as int *, char *, and short **. Of course, the pointer can also be directed to a piece of memory, indicates the starting address with a certain length, for example, INT (* pdata) [4]. Finally, the pointer can also be a function pointer, pointing directly to the first byte of the function running.

(1) Common Data Type pointer

The Common Data Type pointer is relatively simple. It indicates that the area to which it points is the space of the common data type. Let's take a look at the following sample code:

43:       int  m = 10;004012F8   mov         dword ptr [ebp-4],0Ah44:       char* p = (char*) &m;004012FF   lea         eax,[ebp-4]00401302   mov         dword ptr [ebp-8],eax45:       float* f = (float*) &m;00401305   lea         ecx,[ebp-4]00401308   mov         dword ptr [ebp-0Ch],ecx46:       short* s = (short*) &m;0040130B   lea         edx,[ebp-4]0040130E   mov         dword ptr [ebp-10h],edx47:       *p = 2;00401311   mov         eax,dword ptr [ebp-8]00401314   mov         byte ptr [eax],248:       *f = 2.4f;00401317   mov         ecx,dword ptr [ebp-0Ch]0040131A   mov         dword ptr [ecx],4019999Ah49:       *s = 10;00401320   mov         edx,dword ptr [ebp-10h]00401323   mov         word ptr [edx],offset process+46h (00401326)

The above Code contains four data types and three pointers. We can sort them out one by one. M, P, f, and s are temporary variables in the function, because the pointer is also a data type, and the data stored by it is no longer a char, short, or Int type, it is an address. So when we copy P, f, and S, we copy M addresses one by one to them. So although the pointer types are different, the values of P, f, and s are the same. The following is related to the pointer type when assigning values to the space pointed to by the pointer. Generally, if the pointer is of the char type, the computation is limited to the byte pointed to by the pointer. if the pointer is of the int type, the computation range is four consecutive bytes pointed to by the pointer; of course, if the pointer is a data structure or class type, the memory area of the pointer operation will be larger. Therefore, when we return the code, we find that * P = 2 only operates a byte. When * f = 2.4f, we operate four bytes, that is, DWORD, * s = 10, the value assigned is a word. Here 0x401326 is actually 0x00 data.
0a. After the variable in the function is involved, is the M value 10? Can you think about it?(Actually 0x4019000a)

(2) function type pointer

The following is an interesting piece of code. You can view the function address.

void add(){printf("hello!\n");}void process(int* q){int* address = (int*)add;__asm{call address}}

This Code uses embedded assembly, but there is no difficulty in understanding it. Interested users can directly copy it to VC for compilation. Of course, they also need to add the header file and main function. Through code, we can find that address is actually an address, and call address is actually the same as call add.

(3) pointer

Pointer refers to the data type that the Pointer Points. However, if the pointer is an address, the pointer to the address data is also an address.

46:       int* pp = &p;004012FF   lea         eax,[ebp-4]00401302   mov         dword ptr [ebp-8],eax47:       int** ppp = &pp;00401305   lea         ecx,[ebp-8]00401308   mov         dword ptr [ebp-0Ch],ecx48:       int*** pppp = &ppp;0040130B   lea         edx,[ebp-0Ch]0040130E   mov         dword ptr [ebp-10h],edx49:       assert(sizeof(p) == 4);50:       assert(sizeof(pp) == 4);51:       assert(sizeof(ppp) == 4);52:       assert(sizeof(ppp) == 4);

A pointer is a data type that stores an address. A pointer is also an address data type that stores an address. And so on, what about the pointer of the pointer ...... The above Code clearly illustrates this point. Although PP, PPP, and PPPP have different meanings, the data they store is very simple, that is, the stack address. PPPP-> PPP-> PP-> P. If you understand this relationship, you will not think the pointer is complicated. If the problem persists, you can print the pointer content and pointer address separately, and you will find their differences. The following is the printed result:

p = 0xa, &p = 0x12ff20pp = 0x12ff20, &pp = 0x12ff1Cppp = 0x12ff1C, &ppp = 0x12ff18pppp = 0x12ff18, &pppp = 0x12ff14

 

(To be continued)

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.