-2

I tried many ways i searched on the internet but none helped may be cause i cant ask the right question so please help with explaining the current problem and any problems that i might face close to this thx in advance

 #include <iostream>
    using namespace std;

    int main()
    {   
        int i,n;

        //1 - Automatic array allocation (The size of array is known before run - constant) ------------
        int A[10];
        cout<< "\nValue of variable A=" << A<< " ('Roughly Speaking' Array name is a constant pointer )";
        for(i=0; i<10; i++)
            A[i]=2*i;


        cout<<"\nValues of the array A:  ";
        for(i=0; i<10; i++) 
            cout<<A[i]<<" ";

        //Using pointers to access array elements
        int* P=A;
        P[0]=99;
        P[9]=888;
        cout<<"\n\nValues of the array A: ";
        for(i=0; i<10; i++)
            cout<<A[i]<<" ";


        cout<<"\n\nEnter the number of array elements:";
        cin>>n;

        //The new opertor. (dynamic allocation)
        P=new int[n];
        for(i=0; i<n; i++)
            P[i]=5*i;

        cout<<"\n\nValues of the array pointed to by pointer P:\n";
        for(i=0; i<n; i++)  
            cout<<P[i]<<" ";


        //To free the array memory. notice the square brackets
        delete [] P;

        //P=NULL; //Note: good to set P = NULL after deletion
        cout<<"\n\nValues of the array pointed by pointer P (after delete):\n";
        for(i=0; i<n; i++)  
            cout<<P[i]<<" "; 

        cout<<"\n\nDeleting pointer P does not mean you can not use it again";
        P = A+4;  
        cout<<"\nP[0]="<<P[0]<<", A[4]="<<A[4];                       

        cout<<"\nP[5]="<<P[5]<<", A[9]="<<A[9];

        A[3] = 6;


        //for(int j=0;j<10;j++)
        //{
        //  delete P[j];
        //}

        delete [] P; //--------------here the problem
        cout<<endl;

        getchar();

        return 0;

    }

....................................................... ......................................... .................................. .........................

1 Answers1

4

Up to and including the first delete [] P, the code is fine. But once the delete[] is called, P is left pointing at invalid memory, so the next loop that accesses P[i] has Undefined Behavior:

P=new int[n];
...
delete [] P; // <-- P IS LEFT POINTING AT INVALID MEMORY!!!
...
for(i=0; i<n; i++)  
    cout<<P[i]<<" ";  // <-- UNDEFINED BEHAVIOR!!!

After that loop, P is updated to point at the 5th element of A, so the next delete [] P fails because the memory that P is now pointing at was not allocated with new[] to begin with.

P = A+4; // <-- NO new[] HERE!!
...
delete [] P; // <-- BOOM!!!

NEVER call delete or delete[] to free memory that was not allocated with new or new[], respectively.

Remy Lebeau
  • 454,445
  • 28
  • 366
  • 620
  • So to clean the pointer i just equal it to NULL? and for the line "P is updated to point at the 5th element of A" i dont get it xD – blabla blabla Sep 25 '19 at 00:04
  • There is no need to "clean the pointer" after you `delete` it. If you want to set it to NULL, that is your choice, it is optional. As for `P = A+4`, it is the exact same thing as doing `P = &A[4]`, just using **pointer arithmetic** instead. When you refer to a fixed-sized array's name by itself, it *decays* into a pointer to the 1st element in the array. Adding an integer `N` to a pointer advances the pointer by `N` number of elements (ie, by `sizeof(element) * N` bytes). This is legal to do when you have a pointer to something that represents an array of sequential elements. – Remy Lebeau Sep 25 '19 at 00:08
  • ahh i get it now , thanks man u helped alot <3 looking forward to ask more and get more answers it is the 2nd time i take this **** subject *data STRUCTURE* so it is time to ask and stop relaying on the subjects ta xD – blabla blabla Sep 25 '19 at 00:14