dynamic array
#include <iostream>
using namespace std;
int main()
{
int *a;
a=new int[10];
a=new int[5];
cout<<&a[10];
delete []a;
return 0;
}
it mean the array size can expand if i create like this?
| it mean the array size can expand if i create like this? |
No,
arrays are static. Once you create one with size 10 it will stay like this.
If you need a different size you have to create a different array.
1 2 3 4 5
|
a=new int[10];
// Before you create a new array you need to delete the old one first otherwise
// you will have a memory leak.
delete []a;
a=new int[5];
|
Be careful array index is from 0 to size-1. If you try ro access elements outside this range your program will probably crash.
it mean the size of the array can't expand?
| it mean the size of the array can't expand? |
Correct. You're better off using vectors, if you want a container that can expand to contain more data.
Topic archived. No new replies allowed.