I am trying to make a list of array pointers,
Is there a way that after declaring an array i can adjust the size?

For example at start i create a array called : "list[2]"
Is there a way that later on i can change "list[2]" and make it "list[3]" ?

And is there a way i can also delete a layer of the array, so lets say there is "list[3]" is there a way to make it "list[2]" ?

Or does c++ have any data types for list's?

Recommended Answers

All 4 Replies

You are looking for std::vectors. Here is an example :

std::vector<int> aList(2,0); // 0 0  [size = 2]
aList[1] = 1; // 0 1  [size = 2 ] 
aList.push_back(2);   // 0 1 2   [size = 3]
aList.pop_back();     // 0 1     [size = 2]

You can also simulate what you want using dynamic array, but i suggest otherwise.

I am trying to make a list of array pointers,
Is there a way that after declaring an array i can adjust the size?

For example at start i create a array called : "list[2]"
Is there a way that later on i can change "list[2]" and make it "list[3]" ?

And is there a way i can also delete a layer of the array, so lets say there is "list[3]" is there a way to make it "list[2]" ?

Or does c++ have any data types for list's?

If you are using malloc/calloc/realloc/free, realloc will reallocate the space reserved for the array (hence the name).

If you are using new/delete, I think you don't have that option, but I am not sure. You create a new array with the new size, hard copy the array, then delete the old array:

int* array = new int[10];
// fill in array

// change to size 20
int* temp = new int[20];
for (i = 0; i < 10; i++)
    temp[i] = array[i];

delete []array;
array = temp;

You are looking for std::vectors. Here is an example :

std::vector<int> aList(2,0); // 0 0  [size = 2]
aList[1] = 1; // 0 1  [size = 2 ] 
aList.push_back(2);   // 0 1 2   [size = 3]
aList.pop_back();     // 0 1     [size = 2]

You can also simulate what you want using dynamic array, but i suggest otherwise.

Hmmm if i dont push back i can still acces an vector array i havent created?

I have this code and it runs fine :

int apple=1;
    vector<int> aList(2,0); // 0 0  [size = 2]
    aList[1] = 1; // 0 1  [size = 2 ]
   aList[apple+2]=100;
    cout << aList[3]<<" "<<aList[5] << endl;
    return 0;

vector3 was never created but for somereason i can assign values to it :S
Or is the compiler doing the pushback for me automaticly?


@VernonDozier
Trying your code out at the moment

[edit]
Hmmm your code seems logical but for some reason i dont fully get it,

I dont get this part :

delete []array;
array = temp;

If we deleted array...how can he still exist to copy from array :S ?

@VernonDozier
Trying your code out at the moment

[edit]
Hmmm your code seems logical but for some reason i dont fully get it,

I dont get this part :

delete []array;
array = temp;

If we deleted array...how can he still exist to copy from array :S ?

I'm deleting the 40 bytes that I allocated for the array, not the pointer to those array bytes.

Make up some random addresses.

  1. int* array is 4 bytes, stored at 0x000100
  2. int* temp is 4 bytes, stored at 0x000200.
  3. 40 bytes storing integers are allocated from addresses 0x000300 to 0x000327 in the first "new" call.
  4. 80 bytes storing integers are allocated from addresses 0x000400 to 0x00044F in the second "new" call.

Now step through my code:

int* array = new int[10];
// fill in array

// change to size 20
int* temp = new int[20];
for (i = 0; i < 10; i++)
    temp[i] = array[i];

delete []array;
array = temp;
  1. Line 1 : Bytes at 0x000300 to 0x000327 are set aside for integer storage. The value of 0x000100 is now 0x000300.
  2. Line 5 : Bytes at 0x000400 to 0x00044F are reserved for integer storage. Address 0x000200 now contains 0x000400.
  3. Lines 6 and 7: Values at 0x000300 to 0x000327 are copied to 0x000400 to 0x000427.
  4. Line 9 : Addresses 0x000300 to 0x000327 are freed/deleted/go back into the available memory pool. 0x000100 is NOT deleted. It is still reserved for my use as an integer to a pointer.
  5. Line 10 : array points to now-obsolete memory. The values are stored at 0x0004000, which is what temp points to. After this line, array points to it as well.
  6. After line 10: 0x000300 to 0x000327 can be reallocated by the OS, but 0x000100 and 0x000200 cannot, nor can 0x000400 to 0x00044F.

So to some up, delete []array deletes what the pointer is pointing to, but it does not delete the pointer itself.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.