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?

6
  • You can't delete single array elements. This isn't python. Commented May 24, 2018 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? Commented May 24, 2018 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. Commented May 24, 2018 at 12:06
  • std::vector has support for adding and removing individual elements. Commented May 24, 2018 at 12:07
  • Both crash on my machine, since both are in error. The second one should be delete[] arr;. Commented May 24, 2018 at 13:12

3 Answers 3

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;
Sign up to request clarification or add additional context in comments.

6 Comments

Is c++ compiler everytime keep which data is create using new ?
@DigviJayPatil Most times the compiler can know the size just from the address, but I've seen placement new store extra information for arrays sometimes.
When delete arr called will it be a memory leak ?
@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.
Have a look at this list of books. Each book adds a layer until finally you can refer to the standard direcly. I haven't read through the standard yet. I find cppreference is easier to read than the standard.
|
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.

Comments

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.