I have a C++ class with vector<float> members which are initialized in the constructor to a size determined by one of the constructor’s arguments.
summingBuffer = vector<float>(requiredSize);
How do I check whether the vector constructor has successfully allocated the the required space? The instance vars aren’t pointers (should they be?) so if (NULL==myVector) doesn’t work. Does vector throw an exception on allocation error? How about checking .size() afterwards?
Thank you…
The vector constructor will raise
bad_allocif it couldn’t allocate enough storage, no need for extra checks.Using pointers is not a good idea if you don’t absolutely need them.
Also, looks like you could initialize your vectors directly rather than how you’re doing it by using your constructor’s initializer list. Something like: