Hey guys, I have a quick question about dynamic memory. I know that if you are dynamically allocating memory for a single data array and it fails, you can immediately abort the program via a return statement, but is this also true with multiple data arrays? For instance:
1 2 3 4 5 6 7 8 9
int *foo, *bar
foo = new (nothrow) int [5];
bar = new (nothrow) int [5];
if (foo == nullptr || bar == nullptr)
return -1;
else
/*rest of execution */
If the answer to the previous question is no, do you need to do a delete[] on the arrays that succeeded before terminating the application? Say foo is correctly allocated but bar fails, would you have to do something like this?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
int *foo, *bar
foo = new (nothrow) int [5];
bar = new (nothrow) int [5];
if (foo == nullptr || bar == nullptr)
{
if (foo == nullptr && bar != nullptr)
delete[] bar;
elseif (foo != nullptr && bar == nullptr)
delete[] foo;
return -1;
}
else
/*rest of execution */
If the allocation failed, then there is nothing to delete. So no, you do not need to delete in that instance.
However you have a minor issue with your code:
1 2 3 4 5 6
int *foo, *bar
foo = new (nothrow) int [5];
bar = new (nothrow) int [5];
if (foo == nullptr || bar == nullptr)
return -1;
If 'foo' succeeds, but 'bar' fails... then you will exit without deleting foo, and foo will leak. Of course this is extremely unlikely.
That larger point I want to make here is that if you are doing your deletions manually, you're doing it wrong. Use RAII so that leaking is all but impossible. IE, use a vector or use a smart pointer:
1 2 3 4 5
// vector approach:
std::vector<int> foo(5);
std::vector<int> bar(5);
// no need to delete, vector does it automatically
1 2 3 4 5
// smart pointer approach
std::unique_ptr<int[]> foo( newint[5] );
std::unique_ptr<int[]> bar( newint[5] );
// no need to delete, unique_ptr does it automatically
Also note that if you use the normal version of new instead of the nothrow version, you will never get a null pointer, so you don't have to riddle your code with a ton of "if ptr == null" checks.
Huh... I haven't seen those two approaches before. Of course everything I've learned is from various tutorials and google searches so I can see why I'd miss stuff like this. It seems like it just combines all the re-sizing, deleting, and allocation into one line. Is that all these do or are there other reasons besides just cleaning up your code to use these?
Is that all these do or are there other reasons besides just cleaning up your code to use these?
unique_ptr will delete the memory when it goes out of scope. Other than that, it mostly acts just like a normal pointer.
vector is a resizable array. There's a resize() function which allows you to add/remove elements. You can also add/remove elements to the end of the vector with push_back and pop_back.
Well, don't worry to much about 'allocation fails'. Todays computer have gigabytes of memory. If you manage to run out of memory you have a major issue and since a program cannot run properly without memory you should exit es quick as possible anyway