Re: How does delete know how much to deallocate?
Hi
If one know the things in Detail one can use the things better way, efficiently and can avoid the probable BUG's.
As in CString eg. You can take the advantage of the explained behaviour, which might be possible by using public interfaces.
BECAUSE NOTHING IS IDEAL IN UNIVERSE
Thanks!
Anant
Re: How does delete know how much to deallocate?
Quote:
Originally Posted by anantwakode
BECAUSE NOTHING IS IDEAL IN UNIVERSE
The universe is full of ideals. The problem is achieving... ;)
Re: How does delete know how much to deallocate?
Quote:
Originally Posted by ovidiucucu
The universe is full of ideals. The problem is achieving... ;)
Off topic ...apologies for that...but Ovidiu..Achieving comes later...the problem is realizing. this goes better...and a note - if you get annoyed by some repeated question..you might not want to reply to it..or just re-direct them to the previous thread where it was answered. Now since people usually make mistakes like this (not searching if their questions have been already answered..) and once they have done it...there is no way to undo this event. Just re-direct or answer or ignore (if it annoys anyone)...but to question someone's curiosity doesnt sound good to me. It sometimes de-motivates, which we would never (NEVER) intend to do. Cheers. :thumb:
Re: How does delete know how much to deallocate?
Quote:
Originally Posted by exterminator
Off topic ...apologies for that...but Ovidiu..Achieving comes later...the problem is realizing. this goes better...and a note - if you get annoyed by some repeated question..you might not want to reply to it..or just re-direct them to the previous thread where it was answered. Now since people usually make mistakes like this (not searching if their questions have been already answered..) and once they have done it...there is no way to undo this event. Just re-direct or answer or ignore (if it annoys anyone)...but to question someone's curiosity doesnt sound good to me. It sometimes de-motivates, which we would never (NEVER) intend to do. Cheers. :thumb:
Thank you for the good, competent advices. I have printed them, hung on the wall, and further will be law for me. :D ;)
:wave:
Re: How does delete know how much to deallocate?
Quote:
Originally Posted by ovidiucucu
Thank you for the good, competent advices. I have printed them, hung on the wall, and further will be law for me. :D ;)
:wave:
Really? Then that would be an honor for me (since you are so..so..senior to me.. but if not :) .
Okay now back to the topic of the thread. I saw the links pointed to by Paul. The parashift faq lite one. And in both the implementations I saw that placement new operator was being used. I had started a thread looking for possible usages of placement new and I didnt get this as an application.
Although, is this really true (in all cases..if not all ..most cases) that the new[] is implemented in terms of placement new? I would quote one of the methods' code here:
Code:
// Original code: Fred* p = new Fred[n];
char* tmp = (char*) operator new[] (WORDSIZE + n * sizeof(Fred));
Fred* p = (Fred*) (tmp + WORDSIZE);
*(size_t*)tmp = n;
size_t i;
try {
for (i = 0; i < n; ++i)
new(p + i) Fred(); // Placement new
}
catch (...) {
while (i-- != 0)
(p + i)->~Fred(); // Explicit call to the destructor
operator delete[] ((char*)p - WORDSIZE);
throw;
}
Here, there is usage of 'operator new[]' and then there is the usage of placement new 'new(p + i) Fred()'. Now, as far as I know - the main objective of the placement new operator is to make a call to the corresposnding constructor once the memory allocations are done. My questions are
1. Does the operator new[] not make a call to the corresponding constructor as well? If this is true...then I guess there would be 2 subsequent calls to the constructor .... which is wierd and highly undesirable and I am sure not even happening... ! If no then actually we are inside the definition of the operator and is there again some other definition of itself?
2. Is this operator new[] defined again somewhere and that too differently so as to ensure just the memory allocations (and no constructor calls?)?
3. Is this definition in some function? What would be the signature of that function? Does it somehow not indicate that the calls are becoming recursive? [I mean operator new[] being called from inside of the operator new[]'s definition]
4. Should there have been a malloc or something that only allocates memory instead of the operator new[]?
In simple words..I am consfused about this implementation. I understand the underlying concept and objective but not this implementation.. [Hope I explained my confusions/doubts clearly]