|
-
July 13th, 2010, 02:35 PM
#31
Re: delete[] mechanics
Don't have to call destructors on primitives.
Code:
char * array = new char[5];
delete[] array;
No dtors.
Also, you don't need to know the number of elements to call of the destructors. You can easily determine that by total_allocated_space / sizeof(element). In fact, the iterating is probably done by using a running pointer, not an index anyway. My guess is that most compilers record the size of the space, not the number of items, because in the end, new[]/delete[] uses malloc / free. free doesn't care how many items is in it, just how many bytes.
Last edited by ninja9578; July 13th, 2010 at 02:37 PM.
-
July 13th, 2010, 02:41 PM
#32
Re: delete[] mechanics
You're right, my example shows if there's no destructor, then the number of elements is not stored at all, because it is not needed anyway.
What you say about total_allocated_space etc is correct, but I guess it's faster for the run-time to store the number of elements like the way I said, than to ask the heap handler for the size of the space given a pointer...
Plus there's no such thing as
Code:
int GetSizeOfSpace( void * ptr );
-
July 13th, 2010, 02:47 PM
#33
Re: delete[] mechanics
 Originally Posted by ProgrammerC++
Plus there's no such thing as
Code:
int GetSizeOfSpace( void * ptr );
I never said that there was. But the compiler and/or operating system knows how much space is allocated by ptr.
An int can't tell you how much space anyway, ints aren't big enough is most casts, and a size would certainly be unsigned :P
Last edited by ninja9578; July 13th, 2010 at 02:49 PM.
-
July 13th, 2010, 02:53 PM
#34
Re: delete[] mechanics
Yes the compiler know how much space is allocated, that's why the number of elements is stored, but only when an array of a class is allocated of which the class has a destructor, else it won't because it's never needed anyway.
The OS ofcourse knows how much bytes is allocated for a specific pointer, but I guess it's faster for the run-time to store the number of elements this way than to ask the OS how much bytes the size of space is for a specific pointer
-
July 13th, 2010, 02:56 PM
#35
Re: delete[] mechanics
Now I'm curious. Put this into your compiler:
Code:
DWORD * array = new DWORD[0x1FFFFFFFFUL];
std::cout << array[-1] << std::endl;
If I remember correctly, DWORD is only 32 bits, what happens if you go outside of it's range?
Also, don't be so sure that the compiler knows how much space is allocated, it might be asking a runtime library or something.
Last edited by ninja9578; July 13th, 2010 at 02:58 PM.
-
July 14th, 2010, 04:43 PM
#36
Re: delete[] mechanics
 Originally Posted by ProgrammerC++
Yes the compiler know how much space is allocated, that's why the number of elements is stored,
You think that compiler stores that number?
Try this modified version of your code:
Code:
int _tmain(int argc, _TCHAR* argv[])
{
cout << sizeof( ABC ) << endl;
ABC * arr = new ABC[argc];
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|