简体   繁体   中英

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?

If not, how to free correctly?

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

pointer a has to be reserved for other uses, so it is prohibited to delete a directly.

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.

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 holds the memory address of a originally pointed to. And you have freed c by 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. You must free c .

After the execution of code above, has the memory "a" originally pointed to been freed?

Yes, the memory is freed. As c=a , delete [] c; clears the memory of a .

However, you don't need *c to clear memory, just directly delete a and b

delete[]a;
delete[]b;

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 . So here's what you need to do:

1- First free the memory pointed to by a

delete []a;

2- Now point a to the memory location pointed to by b

a=b;

Now the memory location originally pointed to by a is cleared and a now points to where b is pointing.

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.

Yes it has. Pointer are normal variables that happen to contain addresses. Since a is assigned to 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. 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; 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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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