Ok, heres a situation:
Now I have 700 bytes: 50 x 4-bytes in pointers and 50 x 10-bytes in strings. Which of the following scenarios is the proper way to delete these strings.Code:// Create pointers for 50 strings char **strings = new char * [50]; for (int i = 0; i < 50; ++i){ // Create a string with 10 characters of room strings[i] = new char[10]; }
A)B)Code:delete [] strings;C) none of the above (please elaborate).Code:for (i = 0; i < 50; ++i) delete [] strings[i]; delete [] strings;
Thanks for your help.
I would go with B, eventho A would do the job aswell.
But, since the order is something like, when using delete [] strings the pointers to the containers are freed, but the container itself isn't entirely freed, it is first, when some program is desperate for room the container it self will be freed, since no program is claiming it.
Using the for() loop, will assure that every container is freed, befor freeing the pointers to them. This ensures the system will be aware of the available space, and not wait for rmap to investigate, when the system is running low on memory.
Bookmarks