12

I've been trying to make a program that adds 2 arrays of different size. But I would like to know to to dynamically increase the array size capacity? Ex: array[4] then upgrade the size to 2 to make array[6];? EDIT: WIthout using vectors

I tried creating a new ptr but it does not work. I get the error: Read only variable is not assignable.

int *ptr2 = new int[a2.size];


            // new ptr2 copies ptr1
            for (int i=0; i<(a1.size); i++) {
                ptr2[i] = a1.ptr[i];
            }


            // we want ptr1 to point to ptr2
            for (int i=0; i<(a2.size); i++) {
                ptr2[i] += a2.ptr[i];
            }

            delete [] a1.ptr;

            a1.ptr=ptr2;
Mat
  • 202,337
  • 40
  • 393
  • 406
EEstud
  • 309
  • 1
  • 3
  • 12
  • 5
    Why not use vector? It does,what you want.. – vidit Aug 20 '12 at 04:19
  • 3
    I don't want to use vectors. Where can I allocate the new memory? And why do you downvote so quickly? – EEstud Aug 20 '12 at 04:29
  • @EEstud - You can allocate memory in the constructor. And I dint downvote this question.. yet. – vidit Aug 20 '12 at 04:53
  • Possible duplicate of [Can you resize a C++ array after initialization?](https://stackoverflow.com/questions/756906/can-you-resize-a-c-array-after-initialization) – Archmede Jul 08 '17 at 17:17

5 Answers5

22

You can't change the size of the array, but you don't need to. You can just allocate a new array that's larger, copy the values you want to keep, delete the original array, and change the member variable to point to the new array.

  1. Allocate a new[] array and store it in a temporary pointer.

  2. Copy over the previous values that you want to keep.

  3. Delete[] the old array.

  4. Change the member variables, ptr and size to point to the new array and hold the new size.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • 4
    You can't use `realloc` on a block allocated with `new[]`. – David Schwartz Aug 20 '12 at 04:21
  • That's a good point, I didn't actually look at the code and assumed C... my mistake. – Ed S. Aug 20 '12 at 04:22
  • 1
    Also as a general rule, don't mix new/delete with *alloc/free. – Dennis Meng Aug 20 '12 at 04:22
  • He could switch to `malloc`/`free` given that his type is POD, or better yet, just use `vector`. Eventually, you need to learn how to use all these things properly. – David Schwartz Aug 20 '12 at 04:23
  • Where should I allocate it?The only way I see where I could allocate it would be in the local scope of the else statement. But then, the new allocation would disappear after the scope's termination. also why I was downvoted so much? – EEstud Aug 20 '12 at 04:29
  • @EEstud: You'll copy the returned pointer into `ptr` before the scope ends. The pointer will go out of scope, but that will have no effect on the object it pointed to (and you've saved a copy of the pointer in a member variable anyway). Since you will `new[]` that object, it won't go away until you `delete[]` it. – David Schwartz Aug 20 '12 at 04:31
  • 1
    Don't use `new`/`delete`, except as a learning experience. c++ has smart pointers and container classes. – HAL9000 Jul 31 '21 at 22:11
11
   int* newArr = new int[new_size];
   std::copy(oldArr, oldArr + std::min(old_size, new_size), newArr);
   delete[] oldArr;
   oldArr = newArr;
Sanjay Agarwal
  • 111
  • 2
  • 2
  • Considering std::vector already exists and the OP is specifically looking for a low level code dealing with pointers and trying to implement what already exists in stl, why would you prefer std::copy over a for loop in your answer? – Silidrone Apr 17 '23 at 13:11
0
#include<bits/stdc++.h>
using namespace std;

main(){
    
    int *p = new int[5]; // locate memory in heap
    int *q = new int[10];// locate memory in heap
    
    for(int j=0; j<5;j++)
        p[j] = j;
    
    for(int i=0; i<5;i++)
        q[i] = p[i];
        
    delete []p;//Delete the old array 'p'
    p = q; // Assign the pointer of 'q' to 'p'
    q = NULL; // delete the location of pointer 'q'
    
    return 0;
}
BoP
  • 2,310
  • 1
  • 15
  • 24
0

It may be late too answer but i'll explain some things to you..

Array Size cannot be increase because of contiguous memory allocation in the memory. For Example => The address of the every location is

arr[5] => [2001,2002,2003,2004,2005]

Now, The main problem is if you allocate it to arr[10] so, as we don't know the next location 2006 will be free . because array need to be in contiguous so, we won't be able to allocate memory

From my Suggestions use Vector or use dynamic Memory allocation in Cpp

int *old = new int[5];
int *nw = new int[10];

for (size_t i = 0; i < sizeof(old); i++)
    nw[i] = old[i];

delete []old;
old = nw;
nw = NULL;
-1
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *p,*q;
int i;
p=(int *)malloc(5*sizeof(int));
p[0]=3;p[1]=5;p[2]=7;p[3]=9;p[4]=11;
q=(int *)malloc(10*sizeof(int));
for(i=0;i<5;i++)
q[i]=p[i];
free(p);
p=q;
q=NULL;
for(i=0;i<5;i++)
printf("%d \n",p[i]);
return 0;
}