Search code examples
c++arraysmemorydynamicexpand

Is there a way to expand a dynamic memory array in C++?


Is there a way to expand a dynamic memory array? like this:

int *a = new int[5];
*a = new int[2];

Is this legal?


Solution

  • You cannot expand this type of a dynamic memory array. You can use malloc and realloc though if you need this facility but I would advice against that and suggest including <vector> and using std::vector instead. It has a resize method.

    Also, what you described won't compile. The following will:

    1: int *a = new int[5];
    2: a = new int[2];
    

    The above will allocate two memory blocks, neither of which will be destroyed. Second line will simply assign a new array to the same int *a pointer. When an allocated memory stops being referenced by any pointer, this is called a memory leak. The above code loses any reference to new int[5] and there is no way to free this memory to the operating system.

    Although this is not a very practical example, there are multiple ways to resize an array/vector. As it is usually practical to increase the array size, I will do just this:

    { // C++ vector on the stack (although internally vector uses memory from the heap)
        std::vector<int> a(1024);
        // do smth
        a.resize(4096); // note: this does not always reallocate
        // do smth else
    }
    
    { // C++ everything on the heap
        std::vector<int> *a = new std::vector<int>(1024);
        // do smth
        a->resize(4096); // note: this does not always reallocate
        // do smth else
        delete a;
    }
    
    { // C style
        int *a = (int*)malloc(1024*sizeof(int));
        // do smth
        a = realloc(a, 4096*sizeof(int));
        // do smth else
        free(a);
    }
    

    It is worth to note that realloc does not do anything smart. All it does is:

    • Allocate new memory block malloc
    • Copy data from old memory block to new memory block memcpy
    • Free old memory block free
    • Return new memory block