Created
January 28, 2020 04:24
-
-
Save Armiixteryx/c4cb2b4f37b2a9578c0a828246ceb299 to your computer and use it in GitHub Desktop.
Troubles deleting a specific element from an array created with new
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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