Change array size C++

被刻印的时光 ゝ 提交于 2020-04-10 06:39:51

问题


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


回答1:


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

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

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

int* newArray = new int[newSize];
... copying from old array ...
int* temp = oldArray;
oldArray = newArray;
delete[] temp;



回答2:


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.




回答3:


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)




回答4:


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


来源:https://stackoverflow.com/questions/42238712/change-array-size-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!