Can I tell the size of an array created with "new type[n]" in C++?
Using C++11, I like to create an array of booleans and immediately clear it
bool *mymap = new bool[n];
n is variable.
Now, does this already clear the memory occupied by the array? If not, is
there a better approach to clear the array than using a loop over all
elements, setting each to false individually?
I considered using std:memset(), but that requires me to know the size of
the array. Now, beginners might say: Easy, the size is n*sizeof(bool). But
I don't buy that. The compiler might decide to pack them differently, even
pack them as bits, couldn't it?
So, is there a way to tell the size of the array more cleanly? I imagine
there might be a std:arraysize() function that simply returns the space of
the allocated array in memory. After all, that information must be
maintained at runtime somehow, or a delete invocation wouldn't know how
much to release, right?
No comments:
Post a Comment