Skip to main content
Engineering LibreTexts

10.2: Program Memory

  • Page ID
    76143
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Unless the size of the array is known when the program is compiled, memory for arrays is allocated in heap memory. This means that most arrays are stored in heap memory. In languages such as Java, there is no other option, all arrays are stored in heap memory. To understand why this is true, the reader needs to know a little about memory management in a program.

    From a program point of view, on most computers there are 4 areas of memory: text, where executable programs instructions are stored; static memory, where data that is defined when compiling or assembling the program, and thus occurs once for the program is stored; program stack memory, where one set of data (often called automatic data) is stored for each function or method invocation, and heap memory. A generic view of a user memory process space is represented in the following diagram.

    Screen Shot 2022-03-25 at 6.13.32 PM.png

    In this memory model, the text memory (instructions) and static memory (variables defined at compile of assembly time) are constrained, and thus cannot have their size changed at run time. The stack is dynamic and new memory is allocated at run time, the records on the stack must be a size that is fixed at compile or assemble time to work correctly, so the size of any stack allocated on an array would also have a fixed size that cannot be changed at run time. This leaves only heap memory where variable size arrays (or arrays that the size can be set and changed at run time) can be allocated.

    Heap memory is a managed area of memory. Memory is allocated and deallocated as needed. This allocation and deallocation can either be done explicitly using function calls (malloc, calloc, and free) or using language operators such as new (C++, Java) and delete (C++, Java has automatic garbage collection).

    To present arrays, the first array example in section 10.3 is a fixed sized array of integer values that is be allocated in static (or data) memory. The program will print the elements in this array. This was chosen as the first example because the array can be (must be) allocated as part of the program, and the values to the array assigned when the program is written.

    The second example shows the use of a null terminated character array to allocated on the stack. It will show an example of using a toUpper function to convert this string to all uppercase characters, and then print it out. It will iterate until a null value is found in the array

    This section will present arrays using heap access with malloc and free function calls. To allocate an array, malloc is called with a parameter that specifies the number of bytes to allocate, and returns a pointer to a block of memory that is properly aligned for any built-in type, and at least the size requested. When using malloc the memory is not initialized, so the memory should be initialized before being used. If the programmer wants memory initialized to zero, the calloc call should be used.


    This page titled 10.2: Program Memory is shared under a CC BY 4.0 license and was authored, remixed, and/or curated by Charles W. Kann III via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.