c programming notes

2
DIFFERENT MEMORY AREAS IN C Four Areas of Memory: Constant Data Area : Stores string constants and other data whose values are known at compile time. Read only Static-Extent Data Area : Exist for the lifetime of the program Variables are read-writable Constants data and static-extent area are allocated when the program begins and destroyed when it terminates, and are managed by the compiler. Stack : Stores local variables A local variable is allocated memory at the point where it is defined and this memory is released immediately as the variable goes out-of-scope. When variables are defined, they are “pushed onto the stack”, and the stack-size increases. And at the end of a block, if several variables go out-of-scope at once, the variables are destroyed, or “popped off the stack”, in reverse order to their allocation. LIFO queue Stack memory allocation is managed entirely by the compiler Heap : Dynamically allocated storage and is managed by the programmer. Requests for memory, and its subsequent destruction, lifetime of allocated memory block are controlled by programmer. The compiler does not check that the memory is managed correctly, and dynamic memory errors are a plentiful source of subtle runtime bugs.

Upload: cathareen

Post on 17-Dec-2015

6 views

Category:

Documents


0 download

DESCRIPTION

Notes for comp2129

TRANSCRIPT

DIFFERENT MEMORY AREAS IN C

Four Areas of Memory:Constant Data Area: Stores string constants and other data whose values are known at compile time. Read only

Static-Extent Data Area: Exist for the lifetime of the program Variables are read-writable

Constants data and static-extent area are allocated when the program begins and destroyed when it terminates, and are managed by the compiler.

Stack: Stores local variables A local variable is allocated memory at the point where it is defined and this memory is released immediately as the variable goes out-of-scope. When variables are defined, they are pushed onto the stack, and the stack-size increases. And at the end of a block, if several variables go out-of-scope at once, the variables are destroyed, or popped off the stack, in reverse order to their allocation. LIFO queue Stack memory allocation is managed entirely by the compiler

Heap: Dynamically allocated storage and is managed by the programmer. Requests for memory, and its subsequent destruction, lifetime of allocated memory block are controlled by programmer.

The compiler does not check that the memory is managed correctly, and dynamic memory errors are a plentiful source of subtle runtime bugs.It is worth noting that stack memory allocation is generally much faster than heap memory allocation, as stack allocation involves only an increment of the stack pointer, while heap allocation involves much more complex operations. For this reason, stack allocation is often preferred over heap allocation even at the cost of some wasted storage space.

KERNELThe kernel provides basic services for all other parts of the operating system, typically including memory management, process management, file management and I/O (input/output) management (i.e., accessing the peripheral devices). These services are requested by other parts of the operating system or by application programs through a specified set of program interfaces referred to as system calls.

The kernel is a program that constitutes the central core of a computer operating system. A kernel can be contrasted with a shell (such as bash, csh or ksh in Unix-like operating systems), which is the outermost part of an operating system and a program that interacts with user commands. The kernel itself does not interact directly with the user, but rather interacts with the shell and other programs as well as with the hardware devices on the system, including the processor (also called the central processing unit or CPU), memory and disk drives.