Now suppose that the linker has determined that

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ("x = %d\n", x); return 0; } 1 2 3 4 5 6 7 /* bar4.c */ int x; void f() { x = 15212; } 360 CHAPTER 7. LINKING The application of Rules 2 and 3 can introduce some insidious run-time bugs that are incomprehensible to the unwary programmer, especially if the duplicate symbol definitions have different types. Consider the following example, where x is defined as an int in one module and a double in another: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 /* foo5.c */ #include <stdio.h> void f(void); int x = 15213; int y = 15212; int main() { f(); printf("x = 0x%x y = 0x%x \n", x, y); return 0; } 1 2 3 4 5 6 7 /* bar5.c */ double x; void f() { x = -0.0; } On an IA32/Linux machine, doubles are 8 bytes and ints are 4 bytes. Thus, the assignment x = -0.0 in line 5 of bar5.c will overwrite the memory locations for x and y (lines 5 and 6 in foo5.c) with the double-precision floating-point representation of negative one! linux> gcc -o foobar5 foo5.c bar5.c linux> ./foobar5 x = 0x0 y = 0x80000000 This is a subtle and nasty bug,...
View Full Document

This note was uploaded on 09/02/2010 for the course ELECTRICAL 360 taught by Professor Schultz during the Spring '10 term at BYU.

Ask a homework question - tutors are online