Code segment
Code Segment in reference to Programming language C
Every function has two basic parts in it:
Part 1.The code that executes or may execute when the function is called.
Part 2.The data that the function will use.
"Part 1" is what is actually compiled by the C compiler and translated into binary code.
Since "Part 2" contains data, i.e. all variables local to this function, their values cannot be determined statically(also called "during compilation" or "before execution").
Their values are available only when the function is executing(Runtime).
So, references to relative memory location are stored, and replaced during execution.
So, for efficient memory use, the compiler makes following provisions:
1.Code in "part 1" is allocated memory when the program is executed, and de-allocated when program terminates. This memory is called "Code Segment".
2.Code in "part 2" is allocated memory when this function is called during execution, and de-allocated when the function returns. This memory is called "Activation record".
Notes:
1.This reduces memory usage in case of recursion: Code segment will be allocated once, activation record will be allocated as many times as the function is called, and de-allocated when the corresponding call returns.
2.constants and static variables in function are allocated in code segment.
3.Another optimization is that code segment for functions is not allocated memory as soon as program execution begins.
Instead, memory allocation is done when the function is called for first time during execution.
Thus, functions that aren't called at all, will not be allocated.
( What is the need of such functions? Some vital functions such as error handlers must be written / coded but might not be called.)
But, runtime memory allocation for code segment increases execution time.
-extracted from book "The Concepts of Programming Languages" by Robert Sebesta.
In computing, a code segment, also known as a text segment or simply as text, is a phrase used to refer to a portion of memory or of an object file that contains executable instructions.
It has a fixed size and is usually read-only. If the text section is not read-only, then the particular architecture allows self-modifying code. Read-only code is reentrant if it can be executed by more than one process at the same time.
As a memory region, a code segment resides in the lower parts of memory or at its very bottom, in order to prevent heap and stack overflows from overwriting it.
See also
External links