CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    Join Date
    Mar 2009
    Posts
    7

    Question Simple dynamic array question

    I'm wondering how "delete[]" works, and if the following code does what I expect:
    string* A = new string[10];
    string* B = new string[20];
    // now swap
    string* temp = A;
    A = B;
    B = temp;
    // do interesting stuff, then delete
    delete []A;
    delete []B;

    I'm hoping the above "works" and that the correct sizes of arrays are deleted.

    Obviously "delete[]" knows how many elements to delete somehow. But where is this stored? With the ptr? In the heap array allocation?

    Storing with the ptr violates what I know about C/C++ ptrs. Storing with the heap array allocation means that normal ptr arithmetic for array elements would break. Is it maybe stored as a negative offset from the first array element?

    Many thanks for any enlightenment. (Normally, I would just use a Vector or similar, but I want to understand what's going on behind the scenes here.)

    -- penguinFan

  2. #2
    Join Date
    Mar 2009
    Location
    Bulgaria
    Posts
    63

    Re: Simple dynamic array question

    The Standard says nothing about where the size should be stored, so this depends on your compiler. For example, it can be stored just before the allocated memory. But it's never IN the array.

  3. #3
    Join Date
    Mar 2009
    Posts
    7

    Re: Simple dynamic array question

    OK, got it. But is my code correct? A simplified version goes this way:
    string *A = new string [20];
    string *B = A;
    delete [] B;
    Is the system going to be able to figure out how big the array is that B points to?

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Simple dynamic array question

    Yes, that's fine.

    You should consider using std::vector rather than using dynamic arrays directly. It will make your code cleaner.

  5. #5
    Join Date
    Mar 2009
    Posts
    7

    Re: Simple dynamic array question

    Thanks for the confirmation!

    BTW I do use std::vector in normal life. I'm just trying to understand how dynamic arrays work here.

    A related question: if the extent of a dynamic array is guaranteed to be knowable to the run-time, why isn't it accessible via an API call to the programmer? (I know, you didn't design the language ... but maybe there's a philosophical reason?)

    Thanks again.

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Simple dynamic array question

    Quote Originally Posted by penguinFan
    A related question: if the extent of a dynamic array is guaranteed to be knowable to the run-time, why isn't it accessible via an API call to the programmer?
    But it is, via the capacity() member function.

    EDIT:
    Oh wait, you are talking about the dynamically allocated arrays managed manually, rather than dynamic array functionality provided by std::vector. In that case, perhaps a possible answer is that such knowledge is likely to be implementation dependent, and hence unsuitable for a standard library.
    Last edited by laserlight; March 14th, 2009 at 11:44 AM.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  7. #7
    Join Date
    Mar 2009
    Location
    Granada, Spain
    Posts
    40

    Re: Simple dynamic array question

    Code:
    string *A = new string [20];
    string *B = A;
    delete [] B;
    Do you really think that delete [] B will delete the array? It will not. I will just delete the pointer *B, with a size of 4 bytes.

    Try this:

    Code:
    int *v=new int[10];
    v[1]=7;
    int *ptrv=v;
    delete [] ptrv;
    cout<<v[1]<<endl;

  8. #8
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Simple dynamic array question

    Eh, I can't think of any reason not to provide a "query allocated size" mechanism. The information has to be there after all. It just wasn't included in the language for whatever reason.

    There actually is one for malloc()ed memory, but it shouldn't be used with new'd memory since there's no guarantee that new uses malloc internally.

  9. #9
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Simple dynamic array question

    Quote Originally Posted by Tronfi View Post
    Code:
    string *A = new string [20];
    string *B = A;
    delete [] B;
    Do you really think that delete [] B will delete the array? It will not.
    Yes it will.

  10. #10
    Join Date
    Mar 2009
    Location
    Granada, Spain
    Posts
    40

    Re: Simple dynamic array question

    Quote Originally Posted by Lindley View Post
    Yes it will.
    Why you think so? After delete the pointer, v[1] is still accesible.

  11. #11
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Simple dynamic array question

    Quote Originally Posted by yzaykov View Post
    The Standard says nothing about where the size should be stored, so this depends on your compiler. For example, it can be stored just before the allocated memory. But it's never IN the array.
    Most compilers store it in the lower left back corner of the computer. If you look with a microscope you may be able to see them. .

    And Lindley is 100* correct in that "delete []" will delete the entire array. If the array elements have a destructor they wil each be fired (of course this does not apply to POD or raw pointers).

    ps: before the array is a common location also. Qucik test: new something that is not an array, then new an array, look at the addresses and do some math....
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  12. #12
    Join Date
    Mar 2009
    Location
    Granada, Spain
    Posts
    40

    Re: Simple dynamic array question

    Quote Originally Posted by TheCPUWizard View Post
    Most compilers store it in the lower left back corner of the computer. If you look with a microscope you may be able to see them. .

    And Lindley is 100* correct in that "delete []" will delete the entire array. If the array elements have a destructor they wil each be fired (of course this does not apply to POD or raw pointers).

    ps: before the array is a common location also. Qucik test: new something that is not an array, then new an array, look at the addresses and do some math....
    And I guess int doesn't have destructor, does it?

    Thank you!

  13. #13
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Simple dynamic array question

    More to the point, destructing anything, POD or not, isn't guaranteed to make the values in that region of memory change at all. All it's guaranteed to do is make it inappropriate for you to touch that memory.

  14. #14
    Join Date
    Mar 2009
    Posts
    7

    Re: Simple dynamic array question

    > More to the point, destructing anything, POD or not, isn't guaranteed to make the values in that
    > region of memory change at all. All it's guaranteed to do is make it inappropriate for you to
    > touch that memory

    I would hope that it would guarantee that the dynamic storage will not become a memory leak :-)

  15. #15
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Simple dynamic array question

    Quote Originally Posted by penguinFan View Post
    > More to the point, destructing anything, POD or not, isn't guaranteed to make the values in that
    > region of memory change at all. All it's guaranteed to do is make it inappropriate for you to
    > touch that memory

    I would hope that it would guarantee that the dynamic storage will not become a memory leak :-)
    WHY do you think it should????

    Code:
    class Foo
    {
       public: 
           Foo() { m_Leak = new char[100]; }
          ~ Foo() { }
       private: char *m_Leak;
    }
    
    Foo *fp = new foo[100];
    delete [] fp;
    The bold line can not possibly prevent the leakage.....
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

Page 1 of 2 12 LastLast

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