delete z or delete[] z?-TWO ARRAYS A!!!

Hello!
Please, what is the right way to delete the thing in that program?
I thought delete[] z, because we acutally DO have an array in the end.
Am I right?
I want to delete the whole array with all its elements, so I suppose delete[] z woudl do this.
Delete z would delete just variable z which is a pointer and contains as a value the adress of the first elemen of a array created in pus function, doesn' ti?

MANY THANKS!!!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
 #include<iostream>
    using namespace std;
     
     
    int* pus(int b){
      int top;
      int max;
      max=5;
     
      int* a=new int[5];
     
     
      srand(time(0));
      int number;
      for(int i=0;i<top;i++){
      number=rand()%10;
      a[top]=number;
     
      }
    return a;
    }
     
    int* po(int arraypo[5], int g){
      for(int i=5;i>=0;i++){
      arraypo[i]=0;
      }
    return arraypo;
    }
     
     
     
    int main(){
      int st=5;
      int * z;
      z=pus(st);
     
     
     
      cout<<"main: "<<pus(st)<<endl;
      cout<<z[0]<<" "<<z[1]<<" "<<z[2]<<" "<<z[3]<<" "<<z[4]<<" "<<endl;
      cout<<&z[0]<<" "<<&z[1]<<" "<<&z[2]<<endl;
     
      int arraypo[5]=int z[5];
      z=po(st);
      cout<<z[0]<<" "<<z[1]<<" "<<z[2]<<" "<<z[3]<<" "<<z[4]<<" "<<endl;
     
     
      delete z;
      cout<<z[4]<<endl<<z[0]<<endl;
    return 0;
    }
Last edited on
delete [] and delete work on different memory structures. If you use delete [] on single object created with new, behavior is unpredicted (probably writing over memory). Same when executing delete on objects created with new [] - it would lead to memory leak and again - undefined behavior.

in your case you wrote:
int* a=new int[5];
so you should do delete [] a;
Last edited on
Yeah. Just remember: if you allocate with new, delete with delete.
If you allocate with new[], then delete with delete[].
Simple as that :)
Simple as that :)


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

class Whine
{
public:

    Whine()
    {
        std::clog << this << " -> Whine::Whine()\n";
    }

    ~Whine()
    {
        std::clog << this << " -> Whine::~Whine()\n";
    }
};

int main()
{
    typedef Whine wa[5];

    Whine *pw = new wa;

    delete pw; // wrong!
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>

class Whine
{
public:

    Whine()
    {
        std::clog << this << " -> Whine::Whine()\n";
    }

    ~Whine()
    {
        std::clog << this << " -> Whine::~Whine()\n";
    }
};

int main()
{
    // typedef Whine wa[5];

    Whine *pw = new Whine[5]; //Whine *pw = new wa;

    delete[] pw; // correct!
}
Last edited on
Hello!
I think U did not understand my question!
In my function we have array a on two places, cause function pus() is called twice.

1.First call alloctes value of its elements to z.
But , as we told before, z is not an array. A "virtual" array will be creatied by pointer arthmetics.

So, I think, to free the FIRST adress of the array a(called by the line35, we have to write delete[]z, as I did, before return 0.

But, if we write delete z, then we delete- what? z is the pointer, and its adress is different form the address of the z[0], is it?

The adress of z[0] is just its VALUE!!!

So, if we write delete z, what do owe acheive IN THIS PROGRAM at all?

2. Anyway, w ecall pus() twice in that program. In second call array a is created again , but on ANOTHER ADRESS. WHat do we have to do, to clear the second adress field from its value?


MANY THANS!!!
1. Calling non-matching variant of delete leads to undefined behavior. You should call correct variant of delete. Calling incorrect one will achieve nothing aside from possible problems with your program
2.
In second call array a is created again , but on ANOTHER ADRESS. WHat do we have to do, to clear the second adress field from its value?
You need to delete[] it. Just like the first one.
Hello, MiiNiiPaa! Was waiting for U!
Look, what does this mean in praxix? Should I write delete[] z twice? I cannot delete a, cause in that case I wouldn't be able to pass it to main, would I ?

So if I want to delete array from its first address (the adress it got in line 35) I just write delete[] z, don't I?

And, what do I do to free places of the second adres? What do I exactly have to do, and WHERE????


MANY THANKS, hope U all understad what I want to achieve! (and as program is always giving errors, I cannnot test the values on the adresses)



to free places of the second adres?
You need to save that address to a variable and then delete[] it.

Or you can do delete[] pus(), but it isn't of much use unless you just want to see "Created array at address" line been outputted in your function.

Should I write delete[] z twice?
No, unless you assign it new value between two delete[]. Double deleting a memory leads to undefined behavior.
Hello!
It seems to me , U understood my question better then I did the answer!!!

Right this "smelts" like what I wanted to do:
Or you can do delete[] pus(), but it isn't of much use unless you just want to see "Created array at address" line been outputted in your function.


well, line 39 , pus(st) creates an array on second adress, doesn+t it? And it IS created by new[], isnt it?

So, sth like delete[] pus() would exactly describe what I acutally thought to do. Is that a right way to to that , and what did U think with the second part of yr sentence?
Your line with thif function: cout<<"main: "<<pus(st)<<endl;
You cannot use delete[] pus(st) here because you want to output result to cout. You need to save return value to a variable to delete it later
1
2
3
4
5
6
7
8
9
//1 Recommended
int* arr = pus(st);
cout<<"main: "<< arr <<endl;
delete[] arr;

//2 If you like obscure syntax
int* arr;
cout<<"main: "<< (arr = pus(st)) <<endl;
delete[] arr;
Last edited on
Topic archived. No new replies allowed.