0

I have following line of code.

Code Sample 1

char * arr = new char[10];
arr++;
delete arr;

Code Sample 2

char * arr = new char[10];
delete arr;

I have two sample codes. Code sample one is crashing at delete while code sample 2 works okay. There is an only difference of arr++. What exactly happens in these two code samples. Can anybody explain?

DigviJay Patil
  • 986
  • 14
  • 31
  • You can't delete single array elements. This isn't python. – Timo May 24 '18 at 12:01
  • 1
    C++ has no such thing as "delete an element of an array", unless that element is itself a pointer to new-ed memory (and then the array will still contain a dangling pointer). What did you want to happen here? – aschepler May 24 '18 at 12:02
  • 1
    The operand of operator `delete` must be the result of a `new` expression, otherwise the behaviour is undefined. `arr` is initialised with a `new` expression. After incrementing, its value is no longer the result of a `new` expression. Hence the behaviour of your code is undefined. – Peter May 24 '18 at 12:06
  • `std::vector` has support for adding and removing individual elements. – Bo Persson May 24 '18 at 12:07
  • Both crash on my machine, since both are in error. The second one should be `delete[] arr;`. – Eljay May 24 '18 at 13:12
  • I am using Visual Studio 2015 in debug mode. But It is undefined behavior. Somehow Vs2015 debug mode works. – DigviJay Patil May 24 '18 at 13:21

3 Answers3

7

It is not possible to delete a single element because delete must be used to delete the same memory that was allocated.

It is crashing because the pointer that new returns must be the same one that is used for the call to delete.

Incrementing the pointer and using that means that the program no longer sees the other bookkeeping information (possibly stored just before the pointer that new returned)

Also, you should use delete[] to delete an array. For this reason, the following is undefined behavior:

char * arr = new char[10];
delete arr;

It should be:

char * arr = new char[10];
delete[] arr;
wally
  • 10,717
  • 5
  • 39
  • 72
  • Is c++ compiler everytime keep which data is create using new ? – DigviJay Patil May 24 '18 at 12:12
  • @DigviJayPatil Most times the compiler can know the size just from the address, but I've [seen placement new](https://stackoverflow.com/q/8720425/1460794) store extra information for arrays sometimes. – wally May 24 '18 at 12:15
  • When delete arr called will it be a memory leak ? – DigviJay Patil May 24 '18 at 12:19
  • @DigviJayPatil It is undefined behavior. So at that point anything might happen. The operating system is responsible for providing the memory and it will surely be confused by the request to deallocate something that wasn't allocated in the first place. – wally May 24 '18 at 12:22
  • Where do I get these type of important concepts online ? – DigviJay Patil May 24 '18 at 12:38
  • 1
    Have a look at [this list of books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Each book adds a layer until finally you can refer to [the standard](http://eel.is/c++draft/) direcly. I haven't read through the standard yet. I find [cppreference](http://en.cppreference.com/w/) is easier to read than the standard. – wally May 24 '18 at 12:47
1

C++ does not support deleting elements in an array, as a C array is a reserved contiguous memory block. You may be looking for C++ vectors.

With them, you can do something like: (Modified example code from the link)

#include <iostream>
#include <vector>

int main()
{
    // Create a vector containing integers
    std::vector<int> v = {7, 5, 16, 8, 6, 3, 5, 6};

    // Add two more integers to vector
    v.push_back(25);
    v.push_back(13);

    // Iterate and print values of vector
    for(int n : v) {
        std::cout << n << '\n';
    }

    v.erase(3);
    v.erase(5, 6);

    for(int n : v) {
        std::cout << n << '\n';
    }
}

A reference specificially about Visual C++ can be found at https://msdn.microsoft.com/en-us/library/9xd04bzs.aspx#vector__erase.

Thomas Jager
  • 4,836
  • 2
  • 16
  • 30
0

proper way to delete the array of allocations,

char * arr = new char[10]; delete[] arr;

// when you do this one element won't be deleted and will cause leak

char * arr = new char[10];
arr++ 
delete[] arr;
rams time
  • 179
  • 5