0

I've searched through many topics here, but they didn't seem to answer me exactly.

I'm trying to do some dynamic reallocation od arrays in C++. I can't use anything from STL libraries as I need to use this in homework where STL (vectors,...) is explicitly forbidden.

So far, I've tried to elaborate with code like this:

int * items = new int[3]; //my original array I'm about to resize
int * temp = new int[10];
for (int i = 0; i < 3; i++) temp[i] = items[i];

delete [] items;   //is this necessary to delete?
items = new int [10];
for (int i = 0; i < 10; i++) items[i] = temp[i];
delete [] temp;

This seem to work, but what bothers me is the excessive number of iterations. Can't this be done anyhow smarter? Obviously, I'm working with much larger arrays than this. Unfortunately, I have to work with arrays though.

edit: When I try to do items = temp; instead of

for (int i = 0; i < 10; i++) items[i] = temp[i]; and try to std::cout all my elements, I end up with losing first two elements, but valgrind prints them correctly.

Saraph
  • 992
  • 1
  • 11
  • 25
  • When this is for homework, the smarter thing would be to encapsulate it, i.e. rewrite the parts of std::vector that you need... depending on what your definition of "stl" is, you might be able to use things like std::copy to encapsulate the copying process. – PlasmaHH Mar 27 '13 at 11:27
  • Remember that pointers are normal variables, and therefore can be reassigned... – Some programmer dude Mar 27 '13 at 11:28
  • 2
    Been a while since I touched C++ but, do you need to copy items[] to temp[] then from temp[] abck to items[]? Can't you just create a new pointer to items[] - `int *temp = items;` then recreate items - `items = new int[10]`. Do your copy from `temp` to `items` - perhaps look at the `memcopy` function or the `std::copy` function to do this most efficiently. Then delete `temp`. – wmorrison365 Mar 27 '13 at 11:34
  • @wmorrison365 `int * temp = items;` makes my program crash. – Saraph Mar 27 '13 at 11:43
  • @wmorrison365 Thank you very much! `copy (temp, temp+10, items);` works like a charm! – Saraph Mar 27 '13 at 12:14
  • @Saraph... curious, what's the crash reporting? As I say I'm out of date but that just sounds odd. Have you made progress, anyway? – wmorrison365 Mar 28 '13 at 14:26
  • @wmorrison365 Yes, I've made that work already. For the error thing, it was just that classic over-100-lines junk. I can't really read through it. – Saraph Mar 30 '13 at 15:41

2 Answers2

5

Yes, the first delete[] is necessary. Without it, you'd be leaking memory.

As to the code that comes after that first delete[], all of it can be replaced with:

items = temp;

This would make items point to the ten-element array you've just populated:

int * items = new int[3]; //my original array I'm about to resize
int * temp = new int[10];
for (int i = 0; i < 3; i++) temp[i] = items[i];
delete [] items;   //delete the original array before overwriting the pointer
items = temp;

Finally, don't forget to delete[] items; when you are done with the array.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
0

The containers of the STL were made to ease work like this. It is tedious, but there is not much of a choice, when you need to use C-arrays.

The deletion of

delete [] items; 

is necessary, as when you abandon the reference to the array, which you would do with assigning a new reference in

items = new int [10];

will cause a memory leak, so this is necessary.

bash.d
  • 13,029
  • 3
  • 29
  • 42