DOC PREVIEW
UT Dallas CS 4337 - #Sebesta ch05A Memory layout of C program - APUE7.6

This preview shows page 1-2-3-4-5 out of 14 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Slide 17.6 Memory Layout of C Program7.6 Memory Layout of C Program7.6 Memory Layout of C Program7.6 Memory Layout of C Program7.6 Memory Layout of C Program7.6 Memory Layout of C Program7.6 Memory Layout of C Program7.6 Memory Layout of C Program7.6 Memory Layout of C Program7.7 Shared Libraries7.7 Shared Libraries7.8 Memory Allocation7.8 Memory Allocation7. The Environment of Unix Process7.6 Memory Layout of C Program Advanced Programming in the UNIX® Environment, 3ed. W. Richard Stevens & Stephen A. Rago. © 2013 Addison-Wesley7.6 Memory Layout of C ProgramHistorically, 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 specifically 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 Program7.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 segmentusually called simply the data segment, containing variables that are specifically initialized in the program. For example, the C declarationint 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 declarationlong 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 lower-numbered 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/shtext data bss dec hex filename 346919 3576 6680 357175 57337 /usr/bin/cc102134 1776 11272 115182 1c1ee /bin/sh•The fourth and fifth 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 file, instead maintaining a single copy of the library routine somewhere in memory that all processes reference. •This reduces the size of each executable file but may add some runtime overhead, either when the program is first executed or the first 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 file—the classic hello.c program—was first created without shared libraries and then with shared libraries:7.8 Memory AllocationISO C specifies three functions for memory allocation: malloc, calloc, realloc•malloc, which allocates a specified number of bytes of memory. The initial value of the memory is indeterminate.•calloc, which allocates space for a specified number of objects of a specified 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 contents and the end of the new area is indeterminate.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


View Full Document

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

Download #Sebesta ch05A Memory layout of C program - APUE7.6
Our administrator received your request to download this document. We will send you the file to your email shortly.
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 2 2 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?