UT Dallas CS 4337 - #Sebesta ch05A Memory layout of C program - APUE7.6 (14 pages)

Previewing pages 1, 2, 3, 4, 5 of 14 page document View the full content.
View Full Document

#Sebesta ch05A Memory layout of C program - APUE7.6



Previewing pages 1, 2, 3, 4, 5 of actual document.

View the full content.
View Full Document
View Full Document

#Sebesta ch05A Memory layout of C program - APUE7.6

33 views


Pages:
14
School:
University of Texas at Dallas
Course:
Cs 4337 - Organization of Programming Languages
Unformatted text preview:

Advanced Programming in the UNIX Environment 3ed W Richard Stevens Stephen A Rago 2013 Addison Wesley 7 The Environment of Unix Process 7 6 Memory Layout of C Program 7 6 Memory Layout of C Program Historically a C program has been composed of the following pieces Text segment consisting of the machine instructions that the CPU executes Initialized data segment usually called simply the data segment containing variables that are speci cally initialized in the program Uninitialized data segment often called the bss segment named after an ancient assembler operator that stood for block started by symbol Stack where automatic variables are stored along with information that is saved each time a function is called Heap where dynamic memory allocation usually takes place 7 6 Memory Layout of C Program 7 6 Memory Layout of C Program Text Segment consisting of the machine instructions that the CPU executes Usually the text segment is sharable so that only a single copy needs to be in memory for frequently executed programs such as text editors the C compiler the shells and so on Also the text segment is often read only to prevent a program from accidentally modifying its instructions 7 6 Memory Layout of C Program Initialized data segment usually called simply the data segment containing variables that are speci cally initialized in the program For example the C declaration int maxcount 99 appearing outside any function causes this variable to be stored in the initialized data segment with its initial value 7 6 Memory Layout of C Program Uninitialized data segment often called the bss segment named after an ancient assembler operator that stood for block started by symbol Data in this segment is initialized by the kernel to arithmetic 0 or null pointers before the program starts executing The C declaration long sum 1000 appearing outside any function causes this variable to be stored in the uninitialized data segment 7 6 Memory Layout of C Program Stack where automatic variables are stored along with information that is saved each time a function is called Each time a function is called the address of where to return to and certain information about the caller s environment such as some of the machine registers are saved on the stack The newly called function then allocates room on the stack for its automatic and temporary variables This is how recursive functions in C can work Each time a recursive function calls itself a new stack frame is used so one set of variables doesn t interfere with the variables from another instance of the function 7 6 Memory Layout of C Program Heap where dynamic memory allocation usually takes place Historically the heap has been located between the uninitialized data and the stack 7 6 Memory Layout of C Program Figure 7 6 shows the typical arrangement of these segments This is a logical picture of how a program looks there is no requirement that a given implementation arrange its memory in this fashion Nevertheless this gives us a typical arrangement to describe With Linux on a 32 bit Intel x86 processor the text segment starts at location 0x08048000 and the bottom of the stack starts just below 0xC0000000 The stack grows from higher numbered addresses to lowernumbered addresses on this particular architecture The unused virtual address space between the top of the heap and the top of the stack is large 7 6 Memory Layout of C Program The size 1 command reports the sizes in bytes of the text data and bss segments For example size usr bin cc bin sh text data 346919 3576 bss dec 6680 hex lename 357175 57337 usr bin cc 102134 1776 11272 115182 1c1ee bin sh The fourth and fth columns are the total of the three sizes displayed in decimal and hexadecimal respectively 7 7 Shared Libraries Most UNIX systems today support shared libraries Shared libraries remove the common library routines from the executable le instead maintaining a single copy of the library routine somewhere in memory that all processes reference This reduces the size of each executable le but may add some runtime overhead either when the program is rst executed or the rst time each shared library function is called Another advantage of shared libraries is that library functions can be replaced with new versions without having to relink edit every program that uses the library assuming that the number and type of arguments haven t changed 7 7 Shared Libraries The following executable le the classic hello c program was rst created without shared libraries and then with shared libraries 7 8 Memory Allocation ISO C speci es three functions for memory allocation malloc calloc realloc malloc which allocates a speci ed number of bytes of memory The initial value of the memory is indeterminate calloc which allocates space for a speci ed number of objects of a speci ed size The space is initialized to all 0 bits realloc which increases or decreases the size of a previously allocated area When the size increases it may involve moving the previously allocated area somewhere else to provide the additional room at the end Also when the size increases the initial value of the space between the old 7 8 Memory Allocation free the function free causes the space pointed to by ptr to be deallocated This freed space is usually put into a pool of available memory and can be allocated in a later call to one of the three alloc functions


View Full Document

Access the best Study Guides, Lecture Notes and Practice Exams

Loading Unlocking...
Login

Join to view #Sebesta ch05A Memory layout of C program - APUE7.6 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view #Sebesta ch05A Memory layout of C program - APUE7.6 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?