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; 
4
  • 5
    Why not use vector? It does,what you want.. Commented Aug 20, 2012 at 4:19
  • 3
    I don't want to use vectors. Where can I allocate the new memory? And why do you downvote so quickly? Commented Aug 20, 2012 at 4:29
  • @EEstud - You can allocate memory in the constructor. And I dint downvote this question.. yet. Commented Aug 20, 2012 at 4:53
  • Possible duplicate of Can you resize a C++ array after initialization? Commented Jul 8, 2017 at 17:17

5 Answers 5

23

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.

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

7 Comments

You can't use realloc on a block allocated with new[].
That's a good point, I didn't actually look at the code and assumed C... my mistake.
Also as a general rule, don't mix new/delete with *alloc/free.
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.
Don't use new/delete, except as a learning experience. c++ has smart pointers and container classes.
|
14
 int* newArr = new int[new_size]; std::copy(oldArr, oldArr + std::min(old_size, new_size), newArr); delete[] oldArr; oldArr = newArr; 

1 Comment

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

Comments

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; 

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.