Dynamic Array how do I change the size

I created this function to change the size of the dynamic array
but i dont think it working properly

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
  void dynArray::doubleSize(  )
{

  size = 4; 
  int *temparray;
  int currentsize = size;
  int  newsize =  currentsize * 2;

  temparray = new int[newsize];

  for (int i = 0 ; i < newsize;i++)
  {
  temparray[i] = list[i]; 
   }

  size =  newsize; 
  delete [] list;
  list = new int[size];
  list = temparray;


  // this it help see if i made any changes
  cout << sizeof(temparray) << "temp size:\n";
  cout << sizeof(list) << "list size:\n";
  cout << size << "new size:\n\n\n";
}

I want it to output the size of array is the function each time it changes size.I know this can be done with vectors but I would like to understand how to do it with arrays

what can I do differently to make this happen.
You are using the new size in the loop to copy the values, but I think the list array is only as big as the old size.
Last edited on
What exactly is line 4 accomplishing?

sizeof applied to a pointer is the size of a pointer, not the size of whatever is pointed to. That cannot be obtained via a language construct. You must keep track of it yourself.
Topic archived. No new replies allowed.