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

9
  • Why don't you want to use std::vector? Commented Feb 15, 2017 at 0:30
  • Why "without vectors"? Anyway, don't use global variables. They're Evil™. Commented Feb 15, 2017 at 0: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. Commented Feb 15, 2017 at 0:35
  • If you don't use a vector, you will need to perform the allocation and copy yourself. Commented Feb 15, 2017 at 0:35
  • 5
    T *bag Earl Grey please. Commented Feb 15, 2017 at 1:11

4 Answers 4

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.

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

2 Comments

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?
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.
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;

Comments

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)

Comments

-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;
}

1 Comment

The realloc function doesn't call constructors like operator new does. Poor choice.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.