1

I am using C++ Builder for programming iOS Applications . In my application I set an array of TImage

  TImage *Image[10] ;

But i dont know the length of the array at runtime. So how do i replace the 10 for a variable to set a variable array length. When i just replace the 10 for an integer Variable i get an erfror that the Array cant be declared with a Variable length

Philip1895
  • 81
  • 12

1 Answers1

3

You can use a std::vector

std::vector<TImage*> Image(10);

Or you could use a dynamically allocated array (but I would strongly suggest using a std::vector instead)

TImage** Image = new TImage*[10];
// later
delete[] Image;
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • Thanks for the fast answer. I did it with an dynamic allocated array cause i don't have any experience with vectors. I tried to declare it with vector but i get an error : vector is not implemented in std:: – Philip1895 Jan 11 '16 at 13:25
  • @Philip1895 Did you `#include ` in your file? – Cory Kramer Jan 11 '16 at 13:26
  • @Philip1895 do you have experience with dynamic memory allocation? Because that's much harder than using vector. – eerorika Jan 11 '16 at 13:33
  • Could you explain me the difference between vector and arrays ? why should i use vector ? No i don't have much experience with dynamic memory allocation – Philip1895 Jan 11 '16 at 13:34
  • @Philip1895 The short story is that the number of elements of `std::array` must be known at compile time, and then it is fixed size. `std::vector` can be known at run-time, and the size can change, but it is allocated from the heap. Both can be treated as arrays (since they both are under the hood) so they are contiguous, etc. – Cory Kramer Jan 11 '16 at 13:51
  • Ok thank you very helpful answer. It solved my problem now :) – Philip1895 Jan 11 '16 at 13:54
  • I got a new problem . How to refresh the length of array ? Because when c counts more then at from create i have to refresh the length of the array (c) – Philip1895 Jan 11 '16 at 16:04
  • @Philip1895 If you don't initially size the vector, then instead of saying `Image[5] = something`, you can say `Image.push_back(something)`. It will then increase the array if needed to store that item. – Cory Kramer Jan 11 '16 at 16:18
  • Ok but when i defined it like : vector Image(c); how would the push_back call would be ? Image.push_back(c) ? – Philip1895 Jan 11 '16 at 16:26