Programming Resources
For Fun and Learning
Charles Cusack
Computer Science
Hope College
main

Python

C++


JAVA
PHP
SQL
Alice

DynamicMemory


Description

dynamicMemory.cpp
dynamicMemory.out

dynamicMemory.cpp

// ----------------------------------------------------------------------------
//dynamicMemory.cpp
// Examples of how to generate dynamic 1- and 2-dimensional arrays
//----------------------------------------------------------------------------

#include <iostream>  // used for cout, etc.
#include <iomanip>   // Used for setw.
using namespace std;

//----------------------------------------------------------------------------
// The following functions are defined after main.
// I have used functions so you can clearly see exactly what code is needed
// to accomplish various tasks with dynamic arrays without anything else
//
int *getArray(int n);
int **getArray(int n,int m);
int **getArray2(int n,int m);
void printArray(int *A,int n);
void printArray(int **A,int n,int m);
void initializeArray(int *A,int n,int value=0);
//----------------------------------------------------------------------------


int main() {

//------------------------------------
// A Dynamic one dimensional array
//------------------------------------

int length=10;      // Size of our first array
int *A;             // A pointer to an int, which we will use to point to an
                    // array of ints.
A=getArray(length); // Assign the pointer to dynamic memory

initializeArray(A,length,1); // Initialize the array to all "1"s

cout<<"A:\n";
printArray(A,length);        // Print the entries of the array
cout<<"\n";


//------------------------------------
// A Dynamic two dimensional array
//------------------------------------

int n=10,m=20;      // The size of the array will be 10x20
int **B;            // A pointer to a pointer
B=getArray(n,m);    // Get the dynamic memory

// We can now assign values to A just as if it was an array
for(int i=0;i<n;i++) 
{
   for(int j=0;j<m;j++) 
   {
      B[i][j]=i+j;
   }
}

cout<<"B:\n";
printArray(B,n,m);
cout<<"\n";

//------------------------------------
// A Dynamic two dimensional array
//   Using a slightly different method
//   (The only change is the call to 
//    getArray2 instead of getArray)
//------------------------------------

int **C;            // A pointer to a pointer
C=getArray2(n,m);   // Get the dynamic memory
                    // Here we use the same n and m as we did in B.

// We can now assign values to C just as if it was an array
for(int i=0;i<n;i++) 
{
   for(int j=0;j<m;j++) 
   {
      C[i][j]=i+j; 
   }
}

cout<<"C:\n";
printArray(C,n,m);
cout<<"\n";

return 0;
}
//----------------------------------------------------------------------------


//----------------------------------------------------------------------------
// This function allocates an array of n ints.
// The only reason this is in a function is so you can see the code for
// creating a dynamic array separate from the rest of the program.  In reality,
// I almost always just include this code inline.
//
int *getArray(int n) 
{
    int *A;           // Get a pointer 
    A=new int[n];     // Point A to an array of ints.
    return A;            // Now return the pointer.
                         // Since all memory was created with "new", it does not
                         // go out of scope after the function finishes.
}
//----------------------------------------------------------------------------
// This function allocates a 2-dimensional array of ints of size n by m.
// The only reason this is in a function is so you can see the code for
// creating a dynamic array separate from the rest of the program.  In reality,
// I almost always just include this code inline.
//
int **getArray(int n,int m) 
{
    int **A;             // Get a pointer to a pointer.  You'll see why shortly.
    A=new int*[n];       // Point the pointer to the pointer to an array of pointers
    for(int i=0;i<n;i++) // Now point each pointer to an array of ints.
    {
       A[i]=new int[m];  // Each A[i] is now pointing to an array
    }
    return A;            // Now return the pointer to the pointer.
                         // Since all memory was created with "new", it does not
                         // go out of scope after the function finishes.
}
//----------------------------------------------------------------------------
// This function allocates a 2-dimensional array of ints of size n by m.
// This is better than the previous version since the memory is allocated in
// one chunk, so it is guarenteed to be closer together, which can increase
// performance.
//
int **getArray2(int n,int m) 
{
    int **A;             // Get a pointer to a pointer.  You'll see why shortly.
    A=new int*[n];       // Point the pointer to the pointer to an array of pointers
    A[0]=new int[n*m];   // Make A[0] point to an array of size n*m
    for(int i=1;i<n;i++) // Now point each pointer to an array of ints.
    {
       A[i]=A[i-1]+m;    // Each A[i] points m ints beyond the previous
    }
    return A;            // Now return the pointer to the pointer.
                         // Since all memory was created with "new", it does not
                         // go out of scope after the function finishes.
}
//----------------------------------------------------------------------------
// Initialize an array so all entries have value "value"
//
void initializeArray(int *A,int n,int value)
{
    for(int i=0;i<n;i++) 
    {
        A[i]=value;
    }
}
//----------------------------------------------------------------------------
// A function that prints the values in a one dimensional dynamic array.
// You should notice two things:
// 1) You index the array exactly as you would if it was static
// 2) The length of the array has to be passed so you know how many elements
//    are present.
//
void printArray(int *A,int n)
{
    for(int i=0;i<n;i++) 
    {
        cout<<A[i]<<" ";
    }
    cout<<"\n";

}
//----------------------------------------------------------------------------
// A function that prints the values in a two dimensional dynamic array.
// You should notice two things:
// 1) You index the array exactly as you would if it was static
// 2) The number of rows and columns must be passed to the function.
//
void printArray(int **A,int n,int m)
{
    for(int i=0;i<n;i++) 
    {
       for(int j=0;j<m;j++) 
       {
          cout<<setw(3)<<A[i][j]<<" ";
       }
       cout<<"\n";
    }
}
//----------------------------------------------------------------------------