I am in my 1st year in CS ,in the middle of an OOP course.We have started learning about dynamically allocated arrays in C++. And we are now solving a lot of problems, where we have to push back an object into an already dynamically allocated array of classes.
void functionInAClass(ClassName newObject){
ClassName *tmp = new ClassName [this->size+1];
for(int i = 0; i < this->size; i++){
tmp[i] = this->arr[i];
}
tmp [this->size++] = newObject;
delete [] this->arr;
this->arr = tmp;
}
->>> delete [] tmp // why is it that if i add this line, my code doesnt run, our proffesors haven't really explained anything, can't i delete [] tmp since i don't need it anymore,or if i do need it,why? and why does it work if i put in tmp = nullptr; this is what i get:
***Error***
free(): double free detected in tcache 2
Aborted (core dumped)