samedi 21 mars 2020

C++: how to reallocate array using same memory space but going down in size

If I have a dynamically allocated array char* arr = new char[100];.

1: If I want do keep the first half of the array and deallocate the last 50 how do i do that. So I essentially with get the same pointer but I get as if i did char* arr = new char[50].

2: If I want do keep the second half of the array and deallocate the first 50 how do i do that. So I essentially with get the original pointer plus 50 but I get as if i did char* arr = new char[50].

Can I do delete[99] arr; and put it in a loop. I can do this by reallocating the array but I want it to be as fast as possible.

Please don't pester me about spelling and grammar I know I need to do better on that.

Aucun commentaire:

Enregistrer un commentaire