In: Computer Science
C++ Program:
Write another program (in C++) that will allocate a local static array of integers and then a dynamic array of integers. Are they stored next to each other? You can examine this by examining the memory addresses where they are located. As described in class, on some systems the size of a dynamic array is actually stored in the bytes previous to a dynamically allocated array. Through some experiments on your own, try to see if this is true on delmar. Is this true or not true also for the local array? As in the first part, describe the procedure you used to test for this.
`Hey,
Note: Brother if you have any queries related the answer please do comment. I would be very happy to resolve all your queries.
We made the code in cpp and defined 2 arrays first is local static and the second dynamically allocated array using new.
Then, we checked the first and last address of both to check if they are placed next to each other in memory.
Below is the cpp code
#include <iostream>
using namespace std;
//public class
int main()
{
static int arr[10];
int *arr1=new int[10];
cout<<"Starting address of local static array:
"<<arr<<", Ending address of local static array:
"<<(arr+9)<<endl;
cout<<"Starting address of dynamic array:
"<<arr1<<", Ending address of dynamic array:
"<<(arr1+9)<<endl;
return 0;
}
The result is no, they are not placed next to each other since the ending address of first array is not 4 byte less than the second. Although the memory allotted to both are continuous.
Kindly revert for any queries
Thanks.