CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 3 of 3 FirstFirst 123
Results 31 to 36 of 36
  1. #31
    Join Date
    Jan 2009
    Posts
    1,689

    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.

  2. #32
    Join Date
    Jul 2009
    Posts
    154

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

  3. #33
    Join Date
    Jan 2009
    Posts
    1,689

    Re: delete[] mechanics

    Quote Originally Posted by ProgrammerC++ View Post
    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.

  4. #34
    Join Date
    Jul 2009
    Posts
    154

    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

  5. #35
    Join Date
    Jan 2009
    Posts
    1,689

    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.

  6. #36
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: delete[] mechanics

    Quote Originally Posted by ProgrammerC++ View Post
    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...

Page 3 of 3 FirstFirst 123

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
  •  





Click Here to Expand Forum to Full Width

Featured