I need to write a code segment to do the following:
1. Define a dynamic array to hold 10 integers
2. Read values into the array (use a pointer to access the elements of the array)
3. Print the elemenets of the array in reverse order (use a pointer to access the elements of the array)
It is not outputting what I need. I get an error: pointer being freed was not allocated
Here is what I have so far, any help or suggestions would be appreciated.
The issue is inside the loop, lines 13-17. Inside of that loop you are try to delete a non-null pointer multiple times, resulting in the error you are getting.
To fix this issue you should release pt's memory after you are sure that you are no longer using it's memory. However, in your loop you are deleting pt even though it is clear you are going to be using it. In this case, delete pt at the end of main, after the loop, as it will no longer be used afterward.
int *pt = newint;
for(i = 0; i < 10; i++)
cout << "Enter number: ";
cin >> *(pt + i);
cout << "The numbers in reverse order are: " << endl;
for (i = 10; i < 10; i--) //you still need to implement this logic
cout << pt[i] << ", " << endl;
delete pt; //delete pt and free the memory after all work is done using it.