The abnormality in a piece of colleague appears, memory access error. Take out together. Using VC6.0, the system is Win2K.
The main adjustment function is func1, which is transmitted into the first address of an array. Access to the subscript method of the array, the subscript is the return value of a sub-function FUNC2. Unusual appears here. At the beginning, the address that is suspected is problematic, and it is right to track it. I doubt the return value of FUNC2, but see this function is very simple, just returns a structure of Word member, as if there should be no mistake.
FUNC2 has a parameter, a structural pointer LP, and the function body is Return LP-> VAL. Function declaration returns a Word type, the member VAL of the structure is also a Word type.
The return value of the FUNC2 is printed in the event of an accident. The results illustrate the reason for the error, the printed value is 1520000. Obviously, FUNC2 returns a DWORD value. Access the array offline, so exception.
Why is this so? Why is the function return type is word, but get a DWORD value during use?
I have made an attempt if the return value is assigned to a partial variable of a Word type, and the result is correct; if the type conversion (Word "FUNC2 (P) is also correct.
Look at the assembly code of these three situations (one wrong two pairs), you can find that correct case, there is a sentence before the return value of FUNC2 in FUNC1:
0040B7F2 and Eax, 0FFFH // high 16 bits.
Because when the FUNC2 is called, the transmission of the structural pointer parameters uses a 32-bit EAX. You can see in the assembly code of FUNC2. You need to return to the 16-bit AX. Therefore, 16 bits before the FUNC1 is used, and the use is still EAX, but the value is the correct value required.
The wrong situation, that is, the direct use of the conversion, there is no such clear high. There is no MOVZX zero expansion instruction to use MOV to EAX when it is assigned.
0040B7E6 MOV EDX, DWORD PTR [EBP-4] 0040B7E9 PUSH EDX0040B7EA CALL @ ilt 5 (_func) (0040100A) 0040B7EF Add ESP, 40040B7F2 and Eax, 0FFFH // error code is not. 0040B7F7 PUSH EAX0040B7F8 PUSH OFFSET STRING "A =% D / N" (00420F74) 0040B7FD Call Printf (00401090) 0040B802 Add ESP, 8
Doubt is the bug of the compiler. So I do an experiment. Create a new Project, simulate this FUNC1 and FUNC2. The result is correct. Even if it is unprocessed, there is also a high level in the assembly code, and the returned value will not be interference. More doubt.
Continue experiment. Doubt the actual structure has problems. Transplant the structure in the actual Project to the experimental project. It is also correct.
Doubt Project settings have problems. Transplant the structure and functions in the experiment into actual Project and is correct. depressed.
Change this member's definition position in the structure, is suspected to be aligned, although this should not be a programmer, but no difference. Change the type of this member (DWORD, BYTE), did not find a problem. At this point, technical poor.
I hope I can find the root of the problem.