Search code examples
c++delete-operator

Delete array from an offset


Lets say I allocate an array of ints

int test[] = new int[100];

I take a pointer to somewhere in the middle

int *temp = &test[50];

Then I call delete[] on the temp

delete[] temp

How will the compiler know the size of elements to delete in this case?


Solution

  • It won't (or will, I don't know). You're invoking undefined behavior. You're only allowed to call delete[] on a pointer allocated with new[].

    For example, I get a crash in MSVS.