问题
Quick question regarding memory management in C++
If I do the following operation:
pointer = new char [strlen(someinput_input)+1];
And then perform it again, with perhaps a different result being returned from strlen(someinput_input)
.
Does this result in memory being left allocated from the previous "new
" statement? As in, is each new
statement receiving another block of HEAP memory from the OS, or is it simply reallocating?
Assuming I do a final delete pointer[];
will that deallocate any and all memory that I ever allocated via new
to that pointer?
回答1:
Every call to new
must be matched with a corresponding call to delete
.
As an aside, you should probably consider using std::string or even std::vector<char> (depending on the exact situation), rather than trying to allocate char arrays yourself. Then you don't ever need to worry.
回答2:
When you say "that pointer", it's important to understand that the value of "that pointer" is going to change with each allocation. After one allocation, perhaps it's 0x100234, and after the next allocation, it's 0x104234. What this means is that if you reassign a pointer to a new address (a new block of memory) without freeing (delete[]ing) the old block, you will leak memory for the duration of the process.
回答3:
Does this result in memory being left allocated from the previous "new" statement?
Yes, this is called a memory leak.
IE, is each new statement receiving another block of HEAP memory from the OS, or is it simply reallocating?
It is a new block of memory from the heap.
Assuming I do a final delete pointer[]; will that deallocate any and all memory that I ever allocated via new to that pointer?
If you "delete [] pointer;", it will deallocate the memory that it points to. Only the memory from the last "new char[]" call.
回答4:
It will leave your memory dangling. You'll need to call the delete [] command before you use the 'new' operator again on the same pointer (unless you assigned that address to another pointer).
回答5:
A pointer is simply a variable that holds a memory address. It does not do any implicit reference counting.
Each new[] statement returns the address of the first element in the buffer. Where you store this address, or even if you store it at all does not matter.
The memory will only be deallocated when you call delete[].
In C++ there is no automatic reference counting/automatic deleting. When you do another allocation, it doesn't free the old one.
回答6:
In short No. Every time you call new you are allocating new memory off of the heap. There is no magic in c++ that will remember what you assigned pointer too.
回答7:
Every new
is another allocation. If you reassign the result to the same pointer variable without delete
in the middle then memory is lost, i.e. you got yourself a memory leak.
回答8:
As previously said, the new ahs to be matched with a delete. BUT if you can use stdlib's malloc/free, there is a realloc function: cplusplus.com
来源:https://stackoverflow.com/questions/2557866/reallocating-memory-via-new-in-c