memory allocation failure
-
If a memory allocation with new fails in C++, should it return NULL or throw an exception? I have this code:
#include
#include
#include
using namespace std;int main() {
try{
char *p = new char[44'321'833'923];if (!p) { cout << "memory allocation failed"; exit(1); } } catch(exception&e) { cout << "caught it." << e.what(); exit(1); } catch (...) { cout << "caught exception;"; exit(1); } return 0;
}
The class I'm taking says it returns NULL, but the code seems to throw an exception. Anyone know? Thanks.
-
If a memory allocation with new fails in C++, should it return NULL or throw an exception? I have this code:
#include
#include
#include
using namespace std;int main() {
try{
char *p = new char[44'321'833'923];if (!p) { cout << "memory allocation failed"; exit(1); } } catch(exception&e) { cout << "caught it." << e.what(); exit(1); } catch (...) { cout << "caught exception;"; exit(1); } return 0;
}
The class I'm taking says it returns NULL, but the code seems to throw an exception. Anyone know? Thanks.
I don’t know who teaches the class you are taking but I hope you don’t pay much for it.
new
has been throwingstd::bad_alloc
for the past 20+ years. Also, while I don’t mind at all answering your questions, why don’t you try to googling? A search for “c++ new allocation failure” gives you many results and undoubtedly can teach you more than my limited answers. Edit: Using exactly the search I suggested, I found this good article that discusses the issue: C++ Exceptions and Memory Allocation Failure[^].Mircea
-
If a memory allocation with new fails in C++, should it return NULL or throw an exception? I have this code:
#include
#include
#include
using namespace std;int main() {
try{
char *p = new char[44'321'833'923];if (!p) { cout << "memory allocation failed"; exit(1); } } catch(exception&e) { cout << "caught it." << e.what(); exit(1); } catch (...) { cout << "caught exception;"; exit(1); } return 0;
}
The class I'm taking says it returns NULL, but the code seems to throw an exception. Anyone know? Thanks.
-
I don’t know who teaches the class you are taking but I hope you don’t pay much for it.
new
has been throwingstd::bad_alloc
for the past 20+ years. Also, while I don’t mind at all answering your questions, why don’t you try to googling? A search for “c++ new allocation failure” gives you many results and undoubtedly can teach you more than my limited answers. Edit: Using exactly the search I suggested, I found this good article that discusses the issue: C++ Exceptions and Memory Allocation Failure[^].Mircea
The class is right here: https://www.edx.org/learn/computer-programming/iitbombay-programming-basics Here is some code from the class:
int main()
{
int numStudents;
int * A;
cin >> numStudents;
A = new int[numStudents];
if (A != NULL) { A[0] = 10; A[1] = 15;}
return 0;
} -
The class is right here: https://www.edx.org/learn/computer-programming/iitbombay-programming-basics Here is some code from the class:
int main()
{
int numStudents;
int * A;
cin >> numStudents;
A = new int[numStudents];
if (A != NULL) { A[0] = 10; A[1] = 15;}
return 0;
} -
The class is right here: https://www.edx.org/learn/computer-programming/iitbombay-programming-basics Here is some code from the class:
int main()
{
int numStudents;
int * A;
cin >> numStudents;
A = new int[numStudents];
if (A != NULL) { A[0] = 10; A[1] = 15;}
return 0;
}Colour me not impressed! As long as you take the free option, I guess you didn't loose much, except maybe some time. Also there is the question of getting bad programming habits, but those can be fixed over time. These days there are many free online course from very reputable sources like MIT OpenCourseWare | Free Online Course Materials[^]. You might want to take a look at some of them.
Mircea