-2

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)
Marek R
  • 32,568
  • 6
  • 55
  • 140
Ice
  • 1
  • You shouldn't delete `tmp` because `tmp` and `this->arr` are pointing to the same thing, and you need `this->arr` – ChrisMM Mar 29 '22 at 12:21
  • When you do `this->arr = tmp;` you tell `arr` to point to the data that `tmp` does. If you then do `delete [] tmp` after that, what memory does `arr` now point to? – NathanOliver Mar 29 '22 at 12:21
  • Just because this is the line where the program crashes or reports an error doesn't mean this is where the problem is. C++ does not work this way. The problem can be anywhere in your code, but after the bug occurs the program keeps running for a little bit before it finally crashes here. This is why stackoverflow.com's [help] requires you to show a [mre] that everyone else can cut/paste ***exactly as shown***, then compile, run, and reproduce your problem. See [ask] for more information. Until you do that, it is unlikely that anyone will be able to figure out your problem. – Sam Varshavchik Mar 29 '22 at 12:24
  • You have performed ownership transfer from `tmp` to `this->arr` (`delete [] this->arr;` is kind of replacement of deleting `tmp`), so why you think delete of `tmp` is needed here? – Marek R Mar 29 '22 at 12:29
  • 1
    University computer science C++ courses using `new` and `delete` should be reserved for the very advanced courses in the C++ series. They need to [Stop Teaching C](https://www.youtube.com/watch?v=YnWhqhNdYyk) as C++. I've not needed `new` or `delete` in the last 12 years of the 33 years I've been doing C++. – Eljay Mar 29 '22 at 12:36
  • Does this answer your question? [How do I declare a 2d array in C++ using new?](https://stackoverflow.com/questions/936687/how-do-i-declare-a-2d-array-in-c-using-new) – 0xDEADBEEF Mar 29 '22 at 13:32

1 Answers1

1

You allocated dynamically an array

ClassName *tmp = new ClassName [this->size+1];

then why are you going to delete it?

delete [] tmp;

What is the sense of the function in this case?

The class has data member arr. You need to reallocate the array pointed to by the pointer arr and to add one more element in the newly allocated array.

So you allocate a new array with the number of elements greater by one than the number of the current dynamically allocated array pointed to by the data member arr.

ClassName *tmp = new ClassName [this->size+1];

copy existent element in the new allocated array

for(int i = 0; i < this->size; i++){
    tmp[i] = this->arr[i];
}

and add the new element

tmp [this->size++] = newObject;

Now the previously allocated array is not needed any more and can be deleted.

delete [] this->arr;

and the data member arr is reassigned with the address of the new allocated array

this->arr = tmp;

If you will delete the new allocated array like

delete [] tmp;

then the pointer arr will be invalid. So there is no any sense to allocate a new array and at once to delete it.

I think the reason of misunderstanding is that you think that this statement

delete [] tmp;

deletes the pointer tmp itself.

But actually this statement does not delete the pointer tmp. It deletes the dynamically allocated memory pointed to by the pointer tmp.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335