How does "delete [] <pointer>" work?

M

mkaushik

Hi everyone,

Im just starting out with C++, and am curious to know how "delete []
<pointer>", knows about the number of memory locations to free.

I read somewhere that delete frees up space assigned to <pointer> by
"new". Does "new" create a list of pointer names and the size of the
memory array they point to? I also read that some compilers may store
the number of consec mem locations a pointer points to, just before the
first data element. Is that correct?

Also, if i used malloc() to make <pointer> point to a number of memory
locations, will calling "delete [] <pointer>" still work? if yes, how
does delete here know the number of memory locations to free? does the
malloc() implementation in C++ also create a pointer/memsize list like
"new"?

And lastly, if i made another pointer <pointer2> point to the same
location as <pointer>, would calling "delete [] <pointer2>" still free
up the correct number of locations?

Thanks in advance for answering my deluge of questions!
regards,
Mayank
 
R

red floyd

mkaushik said:
Hi everyone,

Im just starting out with C++, and am curious to know how "delete []
<pointer>", knows about the number of memory locations to free.

The answer is implementation dependent.
I read somewhere that delete frees up space assigned to <pointer> by
"new". Does "new" create a list of pointer names and the size of the
memory array they point to? I also read that some compilers may store
the number of consec mem locations a pointer points to, just before the
first data element. Is that correct?

In general, that's how it works, but you can't rely on it. Each
implementation has its own methods.
Also, if i used malloc() to make <pointer> point to a number of memory
locations, will calling "delete [] <pointer>" still work?

No. If you malloc() it, free() it. If you "new" it, "delete" it. If
you "new[]" it, "delete[]" it. It's that simple.

if yes, how
does delete here know the number of memory locations to free? does the
malloc() implementation in C++ also create a pointer/memsize list like
"new"?

Many implementations of operator new call malloc behind the scenes, but
it is not required. Again, it's implementation dependent. Besides, you
can't delete[] memory allocated with malloc.


And lastly, if i made another pointer <pointer2> point to the same
location as <pointer>, would calling "delete [] <pointer2>" still free
up the correct number of locations?

int* p = new int[30];
int* p2 = p;
delete[] p2;

Is valid, but dangerous, as p now points at unallocated memory.
Accessing anything through p (or p2) is undefined behavior.
 
J

Jerry Coffin

Hi everyone,

Im just starting out with C++, and am curious to know how "delete []
<pointer>", knows about the number of memory locations to free.

It's required to do so, but exactly how it does so is up to the
implementation.
I read somewhere that delete frees up space assigned to <pointer> by
"new". Does "new" create a list of pointer names and the size of the
memory array they point to? I also read that some compilers may store
the number of consec mem locations a pointer points to, just before the
first data element. Is that correct?

In both cases, the answer is a definite maybe -- it can keep track of
all of the data in one place, or it can put data in each node as it's
allocated, or whatver else it prefers.
Also, if i used malloc() to make <pointer> point to a number of memory
locations, will calling "delete [] <pointer>" still work?

If you use malloc to allocate some memory and then use delete on that
memory, the results are undefined -- it might work for some sufficiently
loose definition of "work". Then again, it might simply crash and burn,
or it might do just about anything else.
if yes, how
does delete here know the number of memory locations to free? does the
malloc() implementation in C++ also create a pointer/memsize list like
"new"?

It could, but it's not required to do so -- allocating memory with
malloc and then attempting to delete it isn't required to work at all.
And lastly, if i made another pointer <pointer2> point to the same
location as <pointer>, would calling "delete [] <pointer2>" still free
up the correct number of locations?

If I understand your question correctly, yes. What you get back from new
is an address. You have to pass that address to delete exactly once --
but the pointer you store the address is makes no difference at all.

To summarize: if you use new to allocate some memory, you must use
delete exactly once on the address you get from new. If you use new[] to
allocate the memory, you must use delete[] exactly once on that address.
If you allocate some memory with malloc, you must call free exactly once
with that address malloc returned.

Beyond that, exactly how each of these accomplishes what it's supposed
to is up to the implementer. When you do something like:

struct whatever {
whatever() { std::cout << "ctor\n"; }
~whatever() { std::cout << "dtor\n"; }
};

whatever *w = new whatever[10];
// ... other stuff here

delete [] whatever;

The 'new' is reuqired to use whatever's ctor to create each of the ten
objects, and the delete is required to use whatever's dtor to destroy
each of those ten objects. How it knows there are ten objects to destroy
is entirely up to it. It might allocate some extra memory and store it
along with the object. It might have a separate table of sizes of
objects. It's even possible that it could (for one example) use the
exact value of the pointer to tell it what it needs -- assuming, for the
moment, a CPU that had no alignment requirements, it could (for one
example) make some bits in the address it assigns equal to the size of
the array that was allocated. Obviously this has the potential for
wasting some address space, but on (for one example) a machine with 64-
bit addressing, that's not likely to be a problem at least in the near
future.
 
J

Jerry Coffin

[ ... ]
whatever *w = new whatever[10];
// ... other stuff here

delete [] whatever;

Oops -- that should (of course) be:

delete [] w;

Sorry 'bout that.
 
G

Greg Comeau

Im just starting out with C++, and am curious to know how "delete []
<pointer>", knows about the number of memory locations to free.

I read somewhere that delete frees up space assigned to <pointer> by
"new". Does "new" create a list of pointer names and the size of the
memory array they point to? I also read that some compilers may store
the number of consec mem locations a pointer points to, just before the
first data element. Is that correct?

Exactly how it works is implementation defined, so yes, that's
one allowed possibility as I recall.
Also, if i used malloc() to make <pointer> point to a number of memory
locations, will calling "delete [] <pointer>" still work? if yes, how
does delete here know the number of memory locations to free? does the
malloc() implementation in C++ also create a pointer/memsize list like
"new"?

It may work, but generally a bad idea; most likely it'll crash,
or appear to work only for something seemingly unrelated to fail later.
Normally you want to stay in the same deallocation family that you
allocated from. So malloc/free, new/delete, new[]/delete[],
class new/delete / new[]/delete[] respectively. "placement new"
has its own concerns too.
And lastly, if i made another pointer <pointer2> point to the same
location as <pointer>, would calling "delete [] <pointer2>" still free
up the correct number of locations?

Absolutely. Just make sure your code doesn't use either after
that point though because they'd be invalid pointers unless
reassigned, etc.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,947
Members
47,498
Latest member
log5Sshell/alfa5

Latest Threads

Top