Write Great Code, Volume 2 by Randall Hyde

Write Great Code, Volume 2 by Randall Hyde

Author:Randall Hyde [Randall Hyde]
Language: eng
Format: epub, mobi
Tags: COMPUTERS / Computer Science
ISBN: 9781593271138
Publisher: No Starch Press
Published: 2012-08-06T16:00:00+00:00


11.4.4 Comparing Pointers

Comparisons are another set of operations that make sense for pointers. Almost every language (that supports pointers) will let you compare two pointers to see if they are equal or not equal. A pointer comparison will tell you whether the pointers reference the same object in memory. Some languages (e.g., assembly and C/C++) will also let you compare two pointers to see if one pointer is less than or greater than another. Like subtraction of two pointers, comparing two pointers only makes sense if the pointers have the same base type and point into the same data structure. If one pointer is less than another, this tells you that the pointer references an object within the data structure that appears before the object whose address the second pointer contains. The converse is equally true for the greater-than comparison. Here is a short example in C that demonstrates pointer comparison:

#include <stdio.h> int iArray[256]; int *ltPtr; int *gtPtr; int main( int argc, char **argv ) { int lt; int gt; // Put the address of the "argc" element // of iArray into ltPtr. This is done // so that the optimizer doesn't completely // eliminate the following code (as would // happen if we just specified a constant // index): ltPtr = &iArray[argc]; // Put the address of the eighth array // element into gtPtr. gtPtr = &iArray[7]; // Assuming you don't type seven or more // command-line parameters when running // this program, the following two // assignments should set lt and gt to 1 (True). lt = ltPtr < gtPtr; gt = gtPtr > ltPtr; printf( "lt:%d, gt:%d\n", lt, gt ); return 0; }

At the (80x86) machine-language level, addresses are simply 32-bit quantities so the machine code can compare these pointers as though they were 32-bit integer values. Here’s the 80x86 assembly code that MSVC emits for this example:

; Line 23 ; ; Grab ARGC (passed to the program on the stack), use ; it as an index into iArray (four bytes per element, ; hence the "*4" in the scaled-indexed addressing mode), ; compute the address of this array element (using the ; LEA -- load effective address -- instruction), and ; store the resulting address into ltPtr: mov eax, DWORD PTR _argc$[esp-4] lea eax, DWORD PTR _iArray[eax*4] mov DWORD PTR _ltPtr, eax ; Line 28 ; ; Put the address of iArray[7] into gtPtr. Because ; the compiler computes the address of iArray[7] ; at compile time (base address of the static variable ; iArray + 7 * 4), this instruction sequence is much ; simpler than the above (which had a variable index ; into the array): mov DWORD PTR _gtPtr, OFFSET FLAT:_iArray+28 ; Line 36 ; ; Set ECX to 1 if the address of iArray[argc] (held ; in EAX) is less than the address of iArray[7]. ; ; Note that "sbb ecx, ecx" and "neg ecx" is a ; sneaky way of setting ECX to 0 or 1 depending ; on whether EAX is less than the address of ; iArray[7] after the comparison.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.