简体   繁体   English

如何释放 function 中分配的 memory 而不返回其指针?

[英]How to free memory allocated in a function without returning its pointer?

I have a function like this:我有一个像这样的 function:

int fun(){
    int* arr = new int[10];
    for(int i = 0; i < 10; i++){
        arr[i] = 5;
    }
    delete[] arr;  // 
    return arr[6];
}

int main(){
    std::cout << fun();
    return 0;
} 

What am i going to do is to free the memory whick is pointed to by the pointer arr .我要做的是释放指针arr指向的 memory whick。 But the function is not returning pointer arr .但是 function 没有返回指针arr So i tryed to free it inside the function.所以我试图在 function 中释放它。
It won't print anything if delete[] arr is above return arr[6] (Using Visual Studio 2019).如果delete[] arr高于return arr[6]则不会打印任何内容(使用 Visual Studio 2019)。
But if return arr[6] is above delete[] arr , would the memory be freed or this sentence be skipped?但是如果return arr[6]高于delete[] arr ,memory 会被释放还是跳过这句话?
Or should i declare arr inside main() then free it in main() ?或者我应该在main()中声明arr然后在main() ) 中释放它?

Unless it's for academic purposes, you rarely see a C++ program using manual memory allocation, you don't need to do it since you have a set of containers in the STL containers library that do this memory management reliably for you. Unless it's for academic purposes, you rarely see a C++ program using manual memory allocation, you don't need to do it since you have a set of containers in the STL containers library that do this memory management reliably for you. In your particular example, a std::vector is recommended.在您的特定示例中,建议使用std::vector

That said, to answer your question:也就是说,回答你的问题:

It won't print anything if delete[] arr is above return arr[6] (Using Visual Studio 2019).如果delete[] arr高于 return arr[6]则不会打印任何内容(使用 Visual Studio 2019)。

If you delete the array before accessing the data stored in it the behavior is undefined.如果在访问存储在其中的数据之前删除数组,则行为未定义。 It's only natural that it doesn't print anything.它不打印任何东西是很自然的。 It could also print the expected result , that's one of the features of undefined behavior .它还可以打印预期的结果,这是 未定义行为特征之一。 Using Visual Studio or not, it's the same.无论是否使用 Visual Studio,都是一样的。

But if return arr[6] is above delete[] arr , would the memory be freed or this sentence be skipped?但是如果 return arr[6]高于delete[] arr ,memory 会被释放还是跳过这句话?

Yes it would be skipped, or more accurately, all code after the return statement will not be executed.是的,它会被跳过,或者更准确地说,return 语句之后的所有代码都不会被执行。 The memory will not be freed. memory 不会被释放。

Or should I declare arr inside main() then free it in main() ?或者我应该在main()中声明arr然后在main() ) 中释放它?

If the data should belong in the main 's scope you should definitely declare it there, you can pass it to the function as an argument:如果数据应该属于main的 scope 你肯定应该在那里声明它,你可以将它作为参数传递给 function :

#include <cassert>
#include <iostream>

int fun(int* arr) {
    assert(arr);
    for (int i = 0; i < 10; i++) {
        arr[i] = 5;
    }
    return arr[6];
}

int main() {
    int* arr = new int[10];
    std::cout << fun(arr);
    delete[] arr;
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM