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

192k 39 gold badges 376 silver badges 390 bronze badges

asked Aug 20 '12 at 4:16

4

  • Why not use vector? It does,what you want..

    Aug 20 '12 at 4:19

  • I don't want to use vectors. Where can I allocate the new memory? And why do you downvote so quickly?

    Aug 20 '12 at 4:29

  • @EEstud - You can allocate memory in the constructor. And I dint downvote this question.. yet.

    Aug 20 '12 at 4:53

3 Answers 3

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.

answered Aug 20 '12 at 4:19

7

  • You can't use realloc on a block allocated with new[].

    Aug 20 '12 at 4:21

  • That's a good point, I didn't actually look at the code and assumed C... my mistake.

    Aug 20 '12 at 4:22

  • Also as a general rule, don't mix new/delete with *alloc/free.

    Aug 20 '12 at 4: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.

    Aug 20 '12 at 4:23

  • Don't use new/delete, except as a learning experience. c++ has smart pointers and container classes.

    Jul 31 at 22:11

                                      int* newArr = new int[new_size];    std::copy(oldArr, oldArr + std::min(old_size, new_size), newArr);    delete[] oldArr;    oldArr = newArr;                                  

answered Jan 19 '14 at 6:31

0

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

answered Sep 7 '19 at 6:48

0

Not the answer you're looking for? Browse other questions tagged c++ or ask your own question.