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.

7
  • 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. Commented Mar 27, 2013 at 11:27
  • Remember that pointers are normal variables, and therefore can be reassigned... Commented Mar 27, 2013 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. Commented Mar 27, 2013 at 11:34
  • @wmorrison365 int * temp = items; makes my program crash. Commented Mar 27, 2013 at 11:43
  • @wmorrison365 Thank you very much! copy (temp, temp+10, items); works like a charm! Commented Mar 27, 2013 at 12:14

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Weird, I had some errors with items = temp;. I somehow lost first two elements.
@NPE didn't you mixed up the sequence? better write down the code than describe it
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.