C, C ++ pointer plus 1 problem

xiaoxiao2021-03-06  73

First, use Bell's pen test questions: [Total] Description The following procedure. #include

<

stdio.h

>

Int main (void)

{

Unsigned int a [3] = {0x01020304, 0x05060708, 0x090a0b0c}; unsigned int * p = (unsigned int *) ((int)); 1 Printf ("% x / n", * p); Return 0 ;

} [Topic Analysis] The output result of this program should be 8010203 (ie 0x08010203). The memory storage structure is shown in (Fig. 1) (in a BIG-Endian system). In the 1st statement, first transform the first address of the array A into an integer, then add 1, then force the transformation to (unsigned int *), this time P value should be 0x1235, so taken from 0x1235 to 0x1238 New integers consisting of four bytes, so it is 0x08010203. If the formula is given is (a [0] >> 8 | a [1] << 24).

Figure 1 Memory Structure Schematic

[Supplementary description] What is the output of this statement? P = a; Printf ("% D% 08X% D% D% D", (p 1) -p, * (p 1), & a [1] - & a [0], (int) (p 1) - (int) p, (char *) (p 1) - (char *) p); the result is: 1 05060708 1 4 4 The first output You may understand as a matter of course, let's look at the results . The second output value is the value of the A [1] element of the array A, that is, the address pointing to (p 1) is 0x1238, not 0x1235. why? Because when the pointer is added, it is actually a width of his point to the data type, so that the pointer can point to the next element rather than one element. The assembly code is as follows: 0040106A MOV ECX, DWORD PTR [EBP-10H] 0040106D Add ECX, 4; Note Here, actual addition is 4, not 100401070 SUB ECX, DWORD PTR [EBP-10H] 00401073 SAR ECX, 200401076 PUSH ECX The third output should be 0x1238-0X1234 = 4, but why is 1? Because returning is the number of such elements, not the real address difference, that is, the difference is removed in the element width. The following is a stack process of the assembly code, parameter & a [1] - & a [0] of the VC. 00401043 Lea ECX, [EBP-8]; Address value 00401046 LEA EDX, [EBP-0CH]; add the address value of A [0] 00401049 SUB ECX, EDX; calculate the difference between the address value, and put Enter ECX (value 4) 0040104B SAR ECX, 2; shift two digits in ECX, which is divided by 4 (value 1) 0040104e Push ECX; parameter stack, for function printf () using fourth The output is 4 because the INT type is subtraction is the reduction in the actual number. You do not need to consider other factors. The assembly code is as follows: 00401063 MOV EDX, DWORD PTR [EBP-10H] 00401066 MOV EAX, DWORD PTR [EDX 4] 00401069 Push EAX; note! There is no right shift before the stack, saying that I think everyone should understand why it is 4, I only give an assembly code: 0040104F MOV EAX, DWORD PTR [EBP-10H] 00401052 Add Eax, 400401055 SUB EAX, DWORD PTR [EBP-10H] 00401058 Push Eax Now look back at the assembly code of the first output result.

When performing P 1, it is actually adding 4 (width of the INT type), but the result is still 1 when it is reduced, so the result is still 1. (p 1) -P0040106A MOV ECX, DWORD PTR [EBP-10H] 0040106D Add ECX, 400401070 SUB ECX, DWORD PTR [EBP-10H] 00401073 SAR ECX, 200401076 PUSH ECX [Post "posts, these previously impressed, but It is not very sure, I tried yesterday, I found out the law. In fact, these C99 should have, my classmates are helping me find, I found it to attach the link to here. When Two Pointers Are Subtracted, Both Shall Point To Elements of The Same Array Object, or One Past The last element of the array object

(The result is the difference between the two array elements)

转载请注明原文地址:https://www.9cbs.com/read-88076.html

New Post(0)