Home > Back-end >  Dynamic array memory release for help!!!!!!!!!!
Dynamic array memory release for help!!!!!!!!!!

Time:10-31

Code is like this: how can I get a and b memory at the same time, the release, this program is not correct
While (1) {
Long double * a=new long double [2];
cout <& lt;" Array is a address: "& lt; cout <& lt;" Array is a [0] address: "& lt; <& A [0] cout <& lt;" Array is a [1] address: "& lt; <& A [1] A [0]=1.0;
A [1]=2.0;
Long double * b=new long double [2].
cout <"Array b address:" & lt; cout <"[0] array b address:" & lt; <& B [0] cout <"[1] array b address:" & lt; <& B [1] B [0]=3.0;
B [1]=4.0;
cout <"-- -- -- -- -- -- -- -- --" & lt; B=a;
cout <"Array b address:" & lt; cout <"[0] array b address:" & lt; <& B [0] cout <"[1] array b address:" & lt; <& B [1] cout cout cout The delete [] a;
The delete [] b;
}

CodePudding user response:

You have a b=a so before b revealed

CodePudding user response:

delete[] b;
B=a;
Delete a;

CodePudding user response:

That b=a is I intentionally added, the solution already know, should be to define a long double but not to the assignment of pointer type, null pointer does not allocate memory, then the null pointer before b=a code point b, then the code is correct, neither in the code to run to the delete [] b when an error, nor a memory leak
  • Related