How to: Optimize the Memory Space by Heap Memory Allocation in C++

Memory allocation at the applications run-time enables the use of a storage space, expressed in number of bytes, having the size of heap memory required for data storing defined by the variables. Compile-time allocation allows the reservation and use of storage space with predefined size expressed in number of bytes. In this situation, software developers must anticipate the maximum size of memory space that can be used in the application.

So, most times when the application is executing, it is not used the entire memory space reserved at compile time. The result is an inefficient use of resources by non-usage of the entire allocated memory space. Another disadvantage results from the users’ need to use data memory areas bigger than they were set at compile time.
The below application determines the optimizing degree of the memory space resulted from the use heap memory instead of memory allocated at compile time. The indicator is determined as the ratio between the number of bytes of unused memory area allocated at compile time and the total number of bytes reserved at compile time. Data are stored as strings, vectors of char, and the memory allocated at compile time is determined as product between the number of strings and the maximum length of these strings. Strings and their number are entered from the keyboard.

#include <string.h>
#include <stdio.h>
#include <iostream>

using namespace std;

void main()
    char **psir, sir[1000];
    int i, j, max = 0, dim, sumlg = 0, n;

    cout << "Number of strings is:";
    cin >> n;
    psir = new char *[n];
    for (i = 0; i < n; i++)
        cout << "Give the string no. " << i + 1 << ":";
        dim = strlen(sir);
        sumlg += dim;
        if (max < dim)
            max = dim;
        psir[i] = new char[dim + 1];
        strcpy(psir[i], sir);
    float gEc = ((max * n - sumlg) * 1.) / (max * n);
    cout << "Optimization degree is: " << gEc << endl;

    for (i = 0; i < n; i++)
        delete[] psir[i];
    delete[] psir;

Meanings of the main variables defined in the application are:

  • psir: pointer variable for heap memory management where the strings are stored;
  • sir: vector variable of type char used as buffer for strings entered from the keyboard;
  • n: number of strings stored in the heap memory;
  • max:size of the string with maximum length expressed in number of characters;
  • dim: dimension of a string;
  • sumlg: sum of strings lengths, heap memory space occupied by strings
  • gEc: indicator level of memory optimization

When taking into account the heap memory areas occupied by beginning addresses of strings and the memory area associated to the variable psir then the expression of indicator gEc in C++ code is:

gEc = ((max * n - sumlg - n * sizeof(*psir) - sizeof(psir)) * 1.) / (max * n);

If the value of the above expression is negative then it is not done memory economy. Since the numerator is composed of expressions that have as result values of type int, and the denominator is also an expression of result of type int, the expression of numerator is multiplied by the value 1. in relation to the outcome to be a real number. Otherwise, the ratio generates an integer result.