 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
mkaushik Guest
|
Posted: Sun Aug 20, 2006 9:10 am Post subject: How does "delete [] <pointer>" work? |
|
|
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 |
|
| Back to top |
|
 |
red floyd Guest
|
Posted: Sun Aug 20, 2006 9:10 am Post subject: Re: How does "delete [] <pointer>" work? |
|
|
mkaushik wrote:
| Quote: | 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.
| Quote: | 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.
| Quote: |
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
| Quote: | 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.
| Quote: | 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. |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|