Skip to content

Instantly share code, notes, and snippets.

@Armiixteryx
Created January 28, 2020 04:24
Show Gist options
  • Save Armiixteryx/c4cb2b4f37b2a9578c0a828246ceb299 to your computer and use it in GitHub Desktop.
Save Armiixteryx/c4cb2b4f37b2a9578c0a828246ceb299 to your computer and use it in GitHub Desktop.
Troubles deleting a specific element from an array created with new
#include <iostream>
using namespace std;
void print(int* myarr, int len) {
for(int i = 0; i < len; i++) {
cout << *(myarr + i) << " ";
}
cout << endl;
}
int main()
{
int len = 20;
int* myarr = new int[len];
for(int i = 0; i < len; i++) {
*(myarr + i) = i;
}
print(myarr, len);
delete (myarr + (len-1));
len--;
print(myarr, len);
}
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
free(): invalid pointer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment