What is the array form of 'delete'?

后端 未结 5 2060
时光取名叫无心
时光取名叫无心 2020-12-01 16:30

When I compiled a code using the array name as a pointer, and I deleted the array name using delete, I got a warning about deleting an array without using the a

相关标签:
5条回答
  • 2020-12-01 16:32

    You either want:

    int *data = new int[5];
    ... // time passes, stuff happens to data[]
    delete[] data;
    

    or

    int data[5];
    ... // time passes, stuff happens to data[]
    // note no delete of data
    

    The genera rule is: only apply delete to memory that came from new. If the array form of new was used, then you must use the array form of delete to match. If placement new was used, then you either never call delete at all, or use a matching placement delete.

    Since the variable int data[5] is a statically allocated array, it cannot be passed to any form of the delete operator.

    0 讨论(0)
  • 2020-12-01 16:37

    As the other have said, you must use the vector form of delete:

    void some_func(size_t n)
    {
      int* data = new int[n];
    
      . . . // do stuff with the array
    
      delete [] data; // Explicitly free memory
    }
    

    Be very wary of this, because some compilers will not warn you.

    Even better, there is very rarely any need for using vector new/delete. Consider whether your code can be altered to make use of std::vector:

    void some_func(size_t n)
    {
      std::vector<int> data(n);
    
      . . . // do stuff with the array
    
    } // memory held by data will be freed here automatically
    

    And if you are dealing with the memory in a local scope, consider using STLSoft's auto_buffer, which will allocate from an internal buffer (held on the stack, as part of the instance) if possible, only going to the heap if it cannot:

    void some_func(size_t n)
    {
      stlsoft::auto_buffer<int, 10> data(n); // only allocates if n > 10
    
      . . . // do stuff with the array
    
    } // memory held by data will be freed here automatically, if any was allocated
    

    Read more about auto_buffer.

    0 讨论(0)
  • 2020-12-01 16:42

    The code as shown has the array either on the stack, or in initialized part of the data segment, i.e. you don't deallocate it (which, as mentioned by others, would be "undefined behavior".) Were it on the "free store", you'd do that with delete [] data.

    0 讨论(0)
  • 2020-12-01 16:51

    Just as RichieHindle stated above when you want to free the space dynamically allocated for an array pointed by data you have to put two brackets [] between the reserved word delete and the pointer to the beginning of the allocated space. Since data can point to a single int in memory as well as to the first element in the array this is the only way you let the compiler know that you want to delete the whole chunk of memory. If you don't do it the proper way the behaviour is "undetermined" (Stroustrup, The C++ Programming Language).

    0 讨论(0)
  • 2020-12-01 16:53

    The array form of delete is:

    delete [] data;
    

    Edit: But as others have pointed out, you shouldn't be calling delete for data defined like this:

    int data[5];
    

    You should only call it when you allocate the memory using new like this:

    int *data = new int[5];
    
    0 讨论(0)
提交回复
热议问题