If you have a dynamic array of strings and you delete[] it, will the strings still delete their dynamic arrays?
1 2 3 4 5 6 7 8 9 10 11
#include <string>
int main(){
std::string * da = new std::string [3];
//da now holds an address to 3 string objects which
//have dynamic char arrays
da[0] = "hdbye"; // da[0] makes a dynamic array holding the chars "hdbye"
delete[] da;
// I delete my string objects, but do the string objects
// delete their character arrays?
}
The problem with your code is that you are not guaranteed to make it to line 8. Your code immediately leaks all those strings if line 7 throws an exception.
The bottom line is that new and delete are for careful use by experts. Strongly prefer library components that eliminate these problems such as std::vector, std::string or std::unique_ptr.
If you need an array made at run-time, you should use smart pointers instead -
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
int main()
{
int size = 0;
std::cout << "Input Size: ";
std::cin >> size;
std::unique_ptr<int[]> pp = std::make_unique<int[]>(size);
for (int i = 0; i < size; i++)
pp[i] = i + 10;
std::cout << "Input elements are :\n";
for (int i = 0; i < size; i++)
std::cout << pp[i] << std::endl;
} //unique_ptr will deallocate when out of scope
highwayman, line 7 can throw an exception if there is no more memory to allocate an std::string (has to turn that literal into an std::string). However, if your system is in a state to allow that, it might be that you're so constrained that even so much as printing an error message fails as well. https://stackoverflow.com/questions/7749066/how-to-catch-out-of-memory-exception-in-c
Most programs in the wild probably are not tested sufficiently for out-of-memory failure conditions.
@Ganado I know about not having enough memory, but I’m not using my memory for anything else. The sole items in my memory are the three strings, a single pointer and a 5 byte string literal. I’m pretty sure that won’t cause a memory exception. Thanks for the clearing up, was a little confused on what he meant.
@zapshe I keep on hearing about smart pointers, but I’ve never really used them and I have no idea how they work and I have no idea where they are in the reference page in cplusplus.com, so I can’t really bring my self to trust them because I don’t know what they’ll do at any moment. If you could explain them all to me or maybe just show me where they are on the site though that would help.
@TheIdeasMan just look at the zapshe response, I’m to lazy to write it again.
Smart pointers are like regular pointers but with a small wrapper on them to make them less dangerous. Instead of needing to use new or delete, you allocate memory as shown on line 7 - "size" being the variable taken in so we know how big to make the pointer/array.
It's useful in that when the smart pointer goes out of scope, it deallocates the memory rather than giving you a memory leak as described on line 17. So at the end of an if statement, loop, or function, you don't need to manually delete the pointer or anything. If you need the pointer memory deallocated earlier, you can use the .reset() function.
The make_unique one makes it so you don't accidentally make two pointers that are staring at the same memory space.
Thanks for the clearing up, was a little confused on what he meant.
Really my point is that in any real program, the code between new xyz and delete xyz is typically prone to failure -- either by simply exiting early, or throwing any kind of exception.
Writing code that doesn't leak in realistic cases is typically very easy as long as you're not writing plain new and delete everywhere.