Hello!
Is that array deleted on the right way?
Should not a be deleted instead of z?
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
|
#include<iostream>
using namespace std;
int* push(int b){
int top;
int max;
max=5;
int* a=new int[5];
srand(time(0));
int number;
for(top=0;top<max;top++){
number=rand()%10;
a[top]=number;
}
return a;
}
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;
delete z;
return 0;
}
|
Last edited on
When you use new
, you use delete
. When you use new[]
, you use delete[]
. So, to answer your question, no, it is not deleted the right way in your code.
By the way, you should be using a C++ container class or smart pointers (unless your professor or assignment forbid you).
Hello!
If I wrote just delete[] in front of return 0; would it delete the places of heap where I put a[5] from the function pus?
Many thanks!!!
Thanks a lot!
And I was also told different, that made confusion!:tup