H private global variables static void memstartbrk

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: a block. Allocated blocks are shaded. Free blocks are unshaded. Heap addresses increase from left to right. 526 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 CHAPTER 10. VIRTUAL MEMORY #include "csapp.h" #define MAXN 15213 int array[MAXN]; int main() { int i, n; scanf("%d", &n); if (n > MAXN) app_error("Input file too big"); for (i = 0; i < n; i++) scanf("%d", &array[i]); exit(0); } Allocating arrays with hard-coded sizes like this is often a bad idea. The value of MAXN is arbitrary and has no relation to the actual amount of available virtual memory on the machine. Further, if the user of this program wanted to read a file that was larger than MAXN, the only recourse would be to recompile the program with a larger value of MAXN. While not a problem for this simple example, the presence of hard-coded array bounds can become a maintenance nightmare for large software products with millions of lines of code and numerous users. A better approach is to allocate the array dynamically, at run...
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