Imperative programming: Difference between revisions

Content deleted Content added
m C -- changed wikilink to more stable article
m C -- added wikilinks for heap
Line 136:
 
[[File:Computer-memory-map.png|thumb|right|Computer memory map]]
''C'' allows the programmer to control which region of memory data is to be stored. ''Global variables'' and ''static variables'' require the fewest [[Clock signal|clock cycles]] to store. The [[call stack|stack]] is automatically used for the standard variable [[Declaration (computer programming)|declarations]]. ''[[Manual memory management|Heap'']] memory is returned to a [[Pointer (computer programming)|pointer variable]] from the [[C dynamic memory allocation|<code>malloc()</code>]] function.
 
* The ''global and static data'' region is located just above the ''program'' region. (The program region is technically called the ''text'' region. It's where machine instructions are stored.)
Line 169:
:* ''Local variables'' declared using the <code>static</code> prefix are also stored in the ''global and static data'' region.<ref name="geeksforgeeks"/> Unlike global variables, static variables are only visible within the function or block. Static variables always retain their value. An example usage would be the function <code>int increment_counter(){ static int counter = 0; counter++; return counter;}</code>
 
* The ''[[call stack|stack]]'' region is a contiguous block of memory located near the top memory address.<ref name="lpi-ch6-p121">{{cite book
|title=The Linux Programming Interface
|last=Kerrisk
Line 187:
|page=122}}</ref> are called ''automatic variables''<ref name="cpl-ch1-p31"/> and are stored in the stack.<ref name="geeksforgeeks"/> They are visible inside the function or block and lose their scope upon exiting the function or block.
 
* The ''[[Manual memory management|heap'']] region is located below the stack.<ref name="geeksforgeeks"/> It is populated from the bottom to the top. The [[operating system]] manages the heap using a ''heap pointer'' and a list of allocated memory blocks.<ref name="cpl-ch1-p185">{{cite book
|title=The C Programming Language Second Edition
|last1=Kernighan