Even though you've allocated storage for 32 std::set
's you haven't initalized this span of memory (ie. the constructor of your std::set
's has not been called) therefore the memory you are trying to operate on/access in entry[0].insert (23)
will cause undefined behavior.
Mixing C++ objects with malloc
and it's equivalent is normally (I'm tempted to write "always") considered to be bad practice.
Instead turn to operator new
which will allocate memory and handle construction of your object in a proper manner, also remember to delete
the memory allocated to release the memory back to your system (and make the object(s) destruct in a true manner).
The proper way to do it in C++
Some answers will contain text saying that you are better of using a std::vector<std::set>
, though that isn't really an answer to your question so I'll leave you with this example snippet
int
main (int argc, char *argv[])
{
std::set<int> *entries = new std::set<int> [32];
entries[0].insert (123);
delete [] entries;
}