简体   繁体   English

C ++如何正确释放内存?

[英]C++ how to correctly free memory?

int main(){
    int *a = new int[5];
    int *b = new int[10];
    int *c;
    for (int i = 0; i < 5; ++i){
        a[i] = i * i;
    }
    for (int i = 0; i < 10; ++i){
        b[i] = 50;
    }
    c = a;
    a = b;
    delete[]b;
    delete[]c;
    return 0;
}

After the execution of code above, has the memory a originally pointed to been freed? 上面的代码执行后,有记忆a原来指向被释放?

If not, how to free correctly? 如果没有,如何正确释放?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~

pointer a has to be reserved for other uses, so it is prohibited to delete a directly. 指针a必须保留用于其他用途,因此禁止直接删除。

the purpose of this code is to access the memory belonging to b originally via a and free the memory a used to possess correctly. 该代码的目的是通过a访问属于b的存储器并释放用于正确拥有的存储器。

Yes, the memory is freed. 是的,内存被释放了。 However, be very careful when changing what a pointer points to because it can easily lead to memory leak. 但是,在更改指针指向的内容时要非常小心,因为它很容易导致内存泄漏。

It is freed. 释放了。

Because after c = a; 因为在c = a; , c holds the memory address of a originally pointed to. c持有的内存地址a原来指向。 And you have freed c by delete [] c; 而你已经释放c通过delete [] c; .

// Assume originally a -> 1, b -> 2

c = a;  // Now a -> 1, b -> 2, c -> 1
a = b;  // Now a -> 2, b -> 2, c -> 1

You can free a or b , at least one of them, they point to the same memory block. 你可以释放ab ,至少其中一个,它们指向同一个内存块。 You must free c . 你必须释放c

After the execution of code above, has the memory "a" originally pointed to been freed? 执行上面的代码后,最初指向的内存“a”是否已被释放?

Yes, the memory is freed. 是的,内存被释放了。 As c=a , delete [] c; c=adelete [] c; clears the memory of a . 清除内存a

However, you don't need *c to clear memory, just directly delete a and b 但是,您不需要*c来清除内存,只需直接删除ab

delete[]a;
delete[]b;

UPDATE UPDATE

As you have edited your answer and now it is more clear what you are trying to achieve,ie access memory pointed to by b via a but before that you want to free memory pointed to by a . 正如你已经编辑了你的答案,现在你更清楚你想要实现的是什么,即b通过a指向的访问内存,但在此之前你想释放a指向的内存。 So here's what you need to do: 所以这就是你需要做的:

1- First free the memory pointed to by a 1-首先释放a指向的内存

delete []a;

2- Now point a to the memory location pointed to by b 2-现在将a指向b指向的内存位置

a=b;

Now the memory location originally pointed to by a is cleared and a now points to where b is pointing. 现在的内存位置原本指向a被清除, a现在指向哪里b指向。

Note: Keep in mind that now when you have both a and b pointing to the same memory location, if you use either delete []a or delete []b , memory location pointed to by both a and b will be cleared. 注:请记住,现在当你同时拥有ab指向同一个内存位置,如果您使用delete []adelete []b ,内存位置都指向ab将被清除。

Yes it has. 是的,它有。 Pointer are normal variables that happen to contain addresses. 指针是碰巧包含地址的常规变量。 Since a is assigned to c. 由于a被分配给c。 Your first allocation is freed. 您的第一个分配已被释放。

Imho a lot of confusion can be avoided when you think of delete as not acting on the pointer, but on the object it points to. 当您认为delete 不是作用于指针,而是指向它指向的对象时,Imho可以避免很多混乱。 It is not the pointer that is deleted, but the array. 它不是删除的指针,而是数组。 Take this example, which is actually very similar to yours: 举个例子,这个例子与你的非常相似:

struct Foo { 
     int id; 
     Foo(int i) : id(i) {} 
}; 
Foo* a = new Foo(1);
Foo* b = new Foo(2);
Foo* c;
c = a;
delete c;
c = b;
delete c;

The only effect delete c; delete c;的唯一效果delete c; has on c is that we are not allowed to derefernce it afterwards, but what gets deleted is first the Foo object with id 1 and then the one with id 2 . c上我们不允许我们之后退出它,但删除的是首先是id 1为1的Foo对象,然后是id 2那个。

Another way - you can to think about optimizations. 另一种方式 - 您可以考虑优化。 Maybe you don't need to allocate memory here. 也许你不需要在这里分配内存。 See this case 看这个案子

int a[5];
int b[10];

int func(int param) {
    for (int i = 0; i < 5; ++i){
        a[i] = i * i;
    }
    for (int i = 0; i < 10; ++i){
        b[i] = 50;
    }
}

int main(){
    for (int i = 0; i < 10000; ++i){
        func(i);
    }
    return 0;
}

You don't need to allocate/free memory per iteration. 每次迭代不需要分配/释放内存。 But if you have multithreading, it will be a bit more complicated. 但是如果你有多线程,它会更复杂一些。

Using similar method I greatly improved the speed of calculations in the project for science. 使用类似的方法,我大大提高了科学项目的计算速度。

Your code should be clear for everybody. 你的代码应该对每个人都清楚。 Think about who will be after you. 想想谁将会追随你。 If the choice between readability and speed - should choose readability. 如果在可读性和速度之间做出选择 - 应该选择可读性。

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

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