Hello,
I am trying to write a program that uses dynamically allocated arrays in C++. I got the code working, but I am not sure I understand why it works.
Here is the code:
The code shown above gives the output:Code:
#include <iostream>
#include <iomanip>
using namespace std;
void allocateh (double * arr, size_t n, double seed)
{
for (size_t i=0;i<n;i++)
*arr++=seed/(i+1);
return;
}
void allocates (double * & arr, size_t n, double seed)
{
double * ptarr = new double [n];
if (ptarr != NULL)
{
arr=ptarr;
for (size_t i=0;i<n;i++)
{
*arr++=seed/(i+1);
}
}
return;
}
void display (const double * arr, size_t max, size_t rowformat, size_t width)
{
cout<<fixed<<setprecision(3);
size_t icells=0;
short correct = 9;
size_t poscount=rowformat*width-(rowformat-1);
for (size_t i=0;i!=poscount;i++)
cout<<"=";
cout<<endl;
while (max > rowformat)
{
for (size_t i=0;i!=rowformat;i++)
{
cout<<"| Cell #"<<setw(width-correct)<<icells+1;
icells++;
}
cout<<"|"<<endl;
for (size_t i=0;i!=rowformat;i++)
{
cout<<"| Temp= "<<setw(width-correct)<<*arr++;
}
cout<<"|"<<endl;
max-=rowformat;
for (size_t i=0;i!=poscount;i++)
cout<<"-";
cout<<endl;
}
if (max)
{
for (size_t i=0;i!=max;i++)
{
cout<<"| Cell #"<<setw(width-correct)<<icells+1;
icells++;
}
cout<<"|"<<endl;
for (size_t i=0;i!=max;i++)
{
cout<<"| Temp= "<<setw(width-correct)<<*arr++;
}
cout<<"|"<<endl;
}
for (size_t i=0;i!=poscount;i++)
cout<<"=";
cout<<endl;
return;
}
void reset_ptr(double * & p, size_t n)
{
p-=n;
return;
}
int main()
{
size_t max=30;
size_t maxrow = 6;
size_t width=20;
double * pstack;
allocates(pstack,max,10000.);
reset_ptr(pstack,max);
display(pstack,max,maxrow,width);
double * pheap = new double;
allocateh(pheap,max,100000.);
//reset_ptr(pheap,max);
display(pheap,max,maxrow,width);
return 0;
}
My questions are in regards to the way the pointers pstack and pheap behave: I need to call the function reset_ptr if I use pstack, but I don't need it when I use pheap (if I uncomment the line reset_ptr(pheap,max) then I get the first two positions skipped).Code:$ ./lab4-hot.out
===================================================================================================================
| Cell # 1| Cell # 2| Cell # 3| Cell # 4| Cell # 5| Cell # 6|
| Temp= 10000.000| Temp= 5000.000| Temp= 3333.333| Temp= 2500.000| Temp= 2000.000| Temp= 1666.667|
-------------------------------------------------------------------------------------------------------------------
| Cell # 7| Cell # 8| Cell # 9| Cell # 10| Cell # 11| Cell # 12|
| Temp= 1428.571| Temp= 1250.000| Temp= 1111.111| Temp= 1000.000| Temp= 909.091| Temp= 833.333|
-------------------------------------------------------------------------------------------------------------------
| Cell # 13| Cell # 14| Cell # 15| Cell # 16| Cell # 17| Cell # 18|
| Temp= 769.231| Temp= 714.286| Temp= 666.667| Temp= 625.000| Temp= 588.235| Temp= 555.556|
-------------------------------------------------------------------------------------------------------------------
| Cell # 19| Cell # 20| Cell # 21| Cell # 22| Cell # 23| Cell # 24|
| Temp= 526.316| Temp= 500.000| Temp= 476.190| Temp= 454.545| Temp= 434.783| Temp= 416.667|
-------------------------------------------------------------------------------------------------------------------
| Cell # 25| Cell # 26| Cell # 27| Cell # 28| Cell # 29| Cell # 30|
| Temp= 400.000| Temp= 384.615| Temp= 370.370| Temp= 357.143| Temp= 344.828| Temp= 333.333|
===================================================================================================================
===================================================================================================================
| Cell # 1| Cell # 2| Cell # 3| Cell # 4| Cell # 5| Cell # 6|
| Temp= 100000.000| Temp= 50000.000| Temp= 33333.333| Temp= 25000.000| Temp= 20000.000| Temp= 16666.667|
-------------------------------------------------------------------------------------------------------------------
| Cell # 7| Cell # 8| Cell # 9| Cell # 10| Cell # 11| Cell # 12|
| Temp= 14285.714| Temp= 12500.000| Temp= 11111.111| Temp= 10000.000| Temp= 9090.909| Temp= 8333.333|
-------------------------------------------------------------------------------------------------------------------
| Cell # 13| Cell # 14| Cell # 15| Cell # 16| Cell # 17| Cell # 18|
| Temp= 7692.308| Temp= 7142.857| Temp= 6666.667| Temp= 6250.000| Temp= 5882.353| Temp= 5555.556|
-------------------------------------------------------------------------------------------------------------------
| Cell # 19| Cell # 20| Cell # 21| Cell # 22| Cell # 23| Cell # 24|
| Temp= 5263.158| Temp= 5000.000| Temp= 4761.905| Temp= 4545.455| Temp= 4347.826| Temp= 4166.667|
-------------------------------------------------------------------------------------------------------------------
| Cell # 25| Cell # 26| Cell # 27| Cell # 28| Cell # 29| Cell # 30|
| Temp= 4000.000| Temp= 3846.154| Temp= 3703.704| Temp= 3571.429| Temp= 3448.276| Temp= 3333.333|
===================================================================================================================
The pointer pstack is defined on the stack and points to a double; the function allocates takes the address of that pointer and makes it point to a newly allocated array of doubles on the heap. Once the array is being populated in allocates, then the pointer must be reset back to the first element.
The pointer pheap is defined on the stack but made to point to a double on the heap. The function allocateh populates the array with doubles. Now why don't I need to reset this pointer back to the first element? If I do it will skip the first two elements.
Thank you in advance.