An exercise is to write a program that shows how memory is allocated to stack variables compared to heap variables. I ran the following code.

#include "../../std_lib_facilities.h"

int main ()
{
   char c = ' ';
   char ch0[10];
   char *ch = new char[10];
   char *init_ch = ch;
   cout << "Enter characters separated by a space.\nUse the '!' to indicate the end of your entries." << endl;
	
   int i = 0;
   while (cin >> c) {
      if (c == '!') {
         break;
      } else {
	 *ch = c;
	 ch0[i] = c;
	 ch++;
	 i++;
      }
   }

   ch = init_ch;
   for (int i = 0; i < 10; i++) {
      cout << *ch << "  " << reinterpret_cast<void*>(ch) << endl;
      cout << ch0[i] << "  " << reinterpret_cast<void*>(&ch0[i]) << endl;
      cout << endl;
      ch++;
   }

   ch = init_ch;
   delete[] ch;

   keep_window_open();
   return 0;
}

The output I get is an address of 00345A48 for the first element of the ch (heap) array, and an address of 0012FF48 for the first element of the ch0 (stack) array. The addresses of the other elements increase by 1 unit in both arrays.

The illustration in my book of the memory layout shows stack memory 'below' heap memory. I am guessing that means stack memory addresses will have smaller numbers than heap memory addresses, as my output shows.

Is that supposed to happen?

Thanks in advance for explanations.

I found several different illustrations of memory layout on the Web. Some of them explicitly show the stack above the heap. But the stack addresses decline, while the heap addresses increase.

Why does my output show stack addresses increasing?

OK. I answered my own question. I was printing the addresses of elements in an array, which always increase. I should have printed the addresses of different arrays. Subsequent arrays would have either higher or lower addresses. I ran a different program.

#include "../../std_lib_facilities.h"

int main ()
{
   char c = ' ';
   char ch0[10];
   char ch1[10];
   char *cha = new char[10];
   char *chb = new char[10];
   char *init_cha = cha;
   char *init_chb = chb;

   cout << "Enter characters separated by a space.\nUse the '!' to indicate the end of your entries." << endl;
	
   int i = 0;
   while (cin >> c) {
      if (c == '!') {
         break;
      } else {
	*cha = c;
	*chb = c;
	ch0[i] = c;
	ch1[i] = c;
	cha++;
	chb++;
	i++;
      }
   }  

   cha = init_cha;
   chb = init_chb;
   for (int i = 0; i < 10; i++) {
      cout << *cha << "  " << reinterpret_cast<void*>(cha) << endl;
      cout << *chb << "  " << reinterpret_cast<void*>(chb) << endl;
      cout << ch0[i] << "  " << reinterpret_cast<void*>(&ch0[i]) << endl;
      cout << ch1[i] << "  " << reinterpret_cast<void*>(&ch1[i]) << endl;
      cout << endl;
      cha++;
      chb++;
   }

   cha = init_cha;
   chb = init_chb;
   delete[] cha;
   delete[] chb;

   keep_window_open();
   return 0;
}

My results are as expected. The address of the first element of cha, the first heap array, is 00345A48. The address of the first element of chb, the second heap array, is 00345A90. The address increased. The address of the first element of ch0, the first stack array, is 0012FF48. The address of the first element of ch1, the second stack array, is 0012FF34. The address decreased.

So the two types of memory are presumably working as they are supposed to. However, now I am confused as to how stack memory can be below heap memory and decrease addresses, while heap is above stack memory and increases addresses. Some of the illustrations from the Web show stack memory 'above' heap memory. I think I am misreading these illustrations.

What is the correct memory layout?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.