In the past, I used to return the first address. For example, my program will return a ten-day array, but the month is dynamic:
Int * cjustwatersatisfyrate: fun_getxun_array (coledatetime begin_time, coledatetime end_time ){
... // Determine the array size n Based on begin_time and end_time
Int * xun_array = new int [N]; [2] // This function is intended to be deleted from the next function. However, this function must return its first address, which obviously cannot be deleted.
... // Assign values to the xun_array
Return xun_array;
}
When using
Cjustwatersatisfyrate WSR;
Int * xun_array = new int [N]; [1]
Xun_array = WSR. fun_getxun_array (begin_time, end_time); // you want to assign a value to the first address.
For (INT I = 0; I <n; I ++)
... // Use xun_array [I] for other operations
[Color = # ff0000] Delete xun_array;
Xun_array = NULL; [/color]
The program can run, but the problem occurs. The release at [1] cannot be lost, and the red part is released, not [1], but [2]. I thought about it for a long time, later I thought that there was a problem with the assignment of the first address. It was equal to opening up two pieces of memory space. When I released the space, I only released the space at [2, and [1] This place is not moving.
It seems that the release is [1]. Actually it is not. Later I changed the function and used the reference method.
Void cjustwatersatisfyrate: fun_getxun_array (coledatetime begin_time, coledatetime end_time, int * xun_array )){
... // Determine the array size n Based on begin_time and end_time
// This sentence is deleted. int * xun_array = new int [N]; [2] // This function is intended to be deleted in the next function, but this function must return its first address, which obviously cannot be deleted.
... // Assign values to the xun_array
// Return xun_array is deleted;
}
It also changed when used:
Int * xun_array = new int [N]; // a ten-day Array
WSR. fun_getxun_array (begin_time, end_time, xun_array); // reference
In this way, the two memory blocks are merged and deleted successfully.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service