4

I have created an array pointer as a global variable like this:

T *bag;
        bag = new T[size];

I have a method where I insert things into the array; however, if it detects that it will overflow the array, I need to resize the array (without vectors). I've been reading about this question all over stack overflow but the answers don't seem to apply to me because I need the data from the old array copied into the new array. Additionally, if I create a new array of a larger size inside the method and then copy the data over to the new array, once the method ends, the array will disappear, but I need it to be a global variable again so all my methods can see it...How should I proceed? Thank you

Chris
  • 73
  • 1
  • 2
  • 8
  • Why don't you want to use `std::vector`? – Greg Kikola Feb 15 '17 at 00:30
  • Why "without vectors"? Anyway, don't use global variables. They're [Evil™](http://stackoverflow.com/a/485020/464581). – Cheers and hth. - Alf Feb 15 '17 at 00:31
  • It's for a project, we aren't allowed to use vectors. Without a global variable, I don't know how I would be able to use the array in all of my functions. – Chris Feb 15 '17 at 00:35
  • If you don't use a `vector`, you will need to perform the allocation and copy yourself. – Thomas Matthews Feb 15 '17 at 00:35
  • @Chris You can pass information to a function through its parameters. You don't necessarily need a global. Globals are usually best avoided. – Greg Kikola Feb 15 '17 at 00:38
  • If I have the old array and copy the contents of the array to the new array, the next time it needs resized, it would be creating a new array with the same name as the old array. Am I thinking about this wrong? – Chris Feb 15 '17 at 00:38
  • @Chris `new` essentially gives you a nameless array sitting on the heap. The "name" that you're using to access it is the name of the pointer variable. That pointer can be made to point to a new location, there's no conflict there. – Greg Kikola Feb 15 '17 at 00:41
  • @Greg Kikola So if I declare this code inside the method: if (size == (index+1)) { bag = new bag[size * 2]; } Will this work fine? – Chris Feb 15 '17 at 01:01
  • 4
    `T *bag` Earl Grey please. – user4581301 Feb 15 '17 at 01:11

4 Answers4

4

Memory, allocated by new, would not disappear after your method ends.

You can return pointer to a new array by using a reference: void f(int *&ptr, size_t &size).

Also, be aware, that you need to clear memory manually after you use it. For example:

int* newArray = new int[newSize];
// copying from old array:
int* temp = oldArray;
oldArray = newArray;
delete[] temp;
Elijah Mock
  • 587
  • 8
  • 21
Semyon Burov
  • 1,090
  • 1
  • 9
  • 15
4

To resize an array you have to allocate a new array and copy the old elements to the new array, then delete the old array.

T * p_bag;
p_bag = new T[old_size];
//...
T * p_expanded_bag = new T[new_size];
for (unsigned int i = 0; i < old_size; ++i)
{
  p_expanded_bag[i] = p_bag[i];
}
delete[] p_bag;
p_bag = p_expanded_bag;

You could use std::copy instead of the for loop.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
  • If I do this, won't the newly created array be a local variable of the method, thus not letting me use use the array in other methods? – Chris Feb 15 '17 at 00:37
  • 1
    The array pointer needs to be accessible and have read/write attributes. If this code is in a free standing function, you should pass the pointer by reference. If you are creating your own `std::vector` class, then the pointer should be a data member and the function a method within the class. – Thomas Matthews Feb 15 '17 at 15:52
0

The thing you need can do the following things

  • Automatically handle the resizing when requested size is larger than current array size.

  • When resizing, they can copy the original content to the new space, then drop the old allocation immediately .

  • There is a non-global-variable way mechanism they can track the array pointer and the current size.

The thing is very similar to std::vector. If it is not allowed to use, you may need manage a dynamic allocated resource like std::vector on your own. You can reference the implementation in that answer link.

If eventually you need to wrap it in a class, make sure to follow the big 3 rules (5 rules in C++11)

Community
  • 1
  • 1
Chen OT
  • 3,486
  • 2
  • 24
  • 46
-1

You can use realloc from c if you have array of chars/ints/doubles... or some other fundamental data type or classes with only those variables (eg. array of strings won't work). http://www.cplusplus.com/reference/cstdlib/realloc/

bag = (T*) realloc(bag, new_size * sizeof(T));

Realloc automatically allocate space for your new array (maybe into the same place in memory) and copy all data from given array. "The content of the memory block is preserved up to the lesser of the new and old sizes, even if the block is moved to a new location."

Example:

#include <stdio.h>      /* printf*/
#include <stdlib.h>     /* realloc, free */

#include <iostream>

int main()
{
    int old_size = 5;
    int new_size = 10;

    int *array = new int[old_size];

    printf("Old array\n");
    for (int i=0; i<old_size; i++) {
        array[i] = i;   
        printf("%d ", array[i]);
    }
    printf("\nArray address: %d\n", array);

    array = (int*) realloc(array, new_size * sizeof(int));

    printf("New array\n");
    for (int i=0; i<new_size; i++)
        printf("%d ", array[i]);
    printf("\nArray address: %d\n", array);

    free(array);
    return 0;
}
laky55555
  • 37
  • 5