简体   繁体   中英

Allocating twice - c++

I saw a lot of similar questions about deleting twice, but what happens when you allocating twice and delete only once? isn't the old version is still exist and how does the program compile? Don't I have to kind of release the new one too, because according to the d'tor it gets called only once. 像这样的东西

For example:

    int main()
{ 

    int *ptr = new int;
    *ptr=5;
    ptr=new int; //again, different memory location
    *ptr=25;
    delete ptr;
    return 0;
}

what with the 5?? it will be a memory leak or something?

what with the 5?? it will be a memory leak or something?

Yes! The second new will overwrite ptr , and you will need the old address for deleting the first allocation. Unless you save ptr it in another variable, or delete it before the second new , or use another variable name for the second pointer, you will have no way of knowing the address of the first memory block, and you will not be able to free it. That is a memory leak.

By the way, welcome to SO

code.cpp:

int main()
{ 

    int *ptr = new int;
    *ptr=5;
    ptr=new int; //again, different memory location
    *ptr=25;
    delete ptr;
    return 0;
}

The above code cause memory leak allocated by first new.

compile the code like:

g++ -fsanitize=leak code.cpp -o code
./code

output (ubuntu budgie x64):

=================================================================
==15281==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 4 byte(s) in 1 object(s) allocated from:
    #0 0x7fa9f020e3d1 in operator new(unsigned long) (/lib/x86_64-linux-gnu/liblsan.so.0+0x103d1)
    #1 0x5555ec6d217e in main (/home/srilakshmikanthanp/Documents/test/code+0x117e)
    #2 0x7fa9f00330b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)

SUMMARY: LeakSanitizer: 4 byte(s) leaked in 1 allocation(s).

Thanks.

Yes , there will be a memory leak, even this doing something like this where you are losing the pointer address will cause a leak:

myPointer = new int;
myPointer = NULL; //leaked memory, no pointer to above int
delete myPointer; //no point at all

Notice that we can use delete on NULL it just won't do anything.

You can check out : Smart Pointers Which handle those things for you with a little overhead.

After you have reassigned the ptr with the address of another object

ptr = new int; //again, different memory location

you no longer have an access to the first object and its address. You won't be able to delete it. And that's a memory leak!

You could think of this in terms of online shopping. When you call new , you place an order (for memory). When you call delete , you return the item (memory) for a refund. The goal is to end with what you started; everything should be returned for a refund.

If you return more than you order, the store will rightfully charge you with fraud (possible crash). This is the problem you read about in the other questions.

If you order more than you return, stuff (memory) accumulates in your home (process). This causes no problem for the store, as it is good for business. Authorities won't step in until your home overflows. However, you'll have problems as you try to navigate around the junk.

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