Dynamic array

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 int main()
{
    int *pt = new int[10];
    int i;

        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--)
        {
            cout << pt[i] << ", " << endl;
            delete[] pt;
        }
        return(0);

}
Hello ac829,

Your for loop is backwards. "i < 10" or "10 < 10". it starts out false, so the for loop does not execute.

What you want is "i >= 0" since you are starting at the end and working bask.

Hope that helps,

Andy
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;

int main()
{
    int *pt = new int[10];
    int i;

    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.
    return(0);
}
Last edited on
Hello ac829,

After testing your program you are missing header file "<iostresm>". "cout", "cin" and "endl" all needed to be qualified with "std" "delete"needs to be moved outside the for loop just before return.

There does seem to be an extra number I have not figured out yet. I will have to put that in my IDE and test it.

Hope that helps,

Andy
Thanks guys, I made those changes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main()
{
    int *pt = new int[10];
    int i;

        for(i = 0; i < 10; i++)
        {
            cout << "Enter number: ";
            cin >> *(pt + i);
        }
    
        cout << "The numbers in reverse order are: " << endl;
        for (i = 10; i >= 0; i--)
        {
            cout << pt[i] << endl;

        }
        delete[] pt;
        return(0);

}


This outputs:
The numbers in reverse order are:
8128
10
9
8
7
6
5
4
3
2
1
Program ended with exit code: 0

Any idea where the first result is coming from?
Last edited on
Hello ac829,

Yes, the first number is because pt[10] is outside the bounds of the array. Arrays along with some others start their indexing at 0. This means the highest index for the array would be 9 not 10.

Starting the second for loop at 9 works properly.

Other than the second for loop starting at 10 everything is the way it should be and works.

Andy
Topic archived. No new replies allowed.