views:

226

answers:

7

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? IE, 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?

Thanks

+5  A: 

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.

warrenm
for the duration of the process <-- at *least*, depending on platform. +1
Billy ONeal
+7  A: 

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.

Dean Harding
And every call to new[] must be matched with delete []
Billy ONeal
And if you really want the pointer, use std::tr1::share_ptr to get the handling of freeing memory.
jpyllman
@jpyllman: But AFAIK `shared_ptr<>` cannot deal with arrays out of the box. You would have to supply a fitting deleter
sbi
+2  A: 

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).

j0rd4n
+2  A: 

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.

Brian R. Bondy
+2  A: 

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.

goldsz
+2  A: 

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.

Nikolai N Fetissov
+5  A: 

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.

Stephen
+1 for a well structured and understandable answer.
vladr