In the obj-c, we can create vector objects as follows:
SomeClass* example[100];
or
int count[7000];
But what if we know the size of the vector only at the time init the class?
(Maybe we need example[756] or count[15])
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
First of all, those aren’t vector objects, they’re compile-time arrays. One of the features of compile time arrays is automatic memory management; that is, you don’t have to worry about allocation and deallocation of these arrays.
If you want to create an array whose size you don’t know until runtime, you’ll need to use
new[]anddelete[]:The catch is that after you’re done with this array, you have to deallocate it:
If you forget to deallocate something created by
newwith a correspondingdelete1, you’ll create a memory leak.You are probably better off using
std::vectorthough because it manages memory for you automatically:1 Make sure you use
deleteon pointers that you created withnewanddelete[]on pointers you make withnew[x]. Do not mix and match them. Again, if you usestd::vector, you don’t have to worry about this.