S
S
sabn1k2016-03-01 08:01:49
C++ / C#
sabn1k, 2016-03-01 08:01:49

How to make std::bad_alloc exception fire?

The book devotes one page to the bad_alloc exception and gives an example like this:

typedef unsigned long ULONG;
int main()
{
  setlocale(0, "");
  const ULONG SIZE = 10200;
  char* ptr;
  try
  {
    ptr = new char[SIZE];
  }
  catch (std::bad_alloc)
  {
    std::cout << "\n Исключение bad_alloc: невозможно разместить данные в памяти.\n";
  }
  delete[] ptr;
  std::cout << "\nПамять используется без сбоев.\n";
  getch();
  return 0;
}

What do I need to change or add in order for the exception to work? Just the most interesting in which case the overflow occurs.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mercury13, 2016-03-01
@sabn1k

#include <iostream>
#include <conio.h>

int main()
{
  setlocale(LC_ALL, "Russian");
  const size_t SIZE = 3000000000ul;
  char* ptr = NULL;
  try
  {
    ptr = new char[SIZE];
    std::cout << "Память используется без сбоев.\n";
  }
  catch (std::bad_alloc&)
  {
    std::cout << "Исключение bad_alloc: невозможно разместить данные в памяти.\n";
  }
  delete[] ptr;
  getch();
  return 0;
}

So much code and so many bugs! The points.
1. The answer to your question. Make the constant bigger. By the way, this constant is size_t. In 64-bit code, even more is needed.
2. The behavior of delete[] has not been worked out if an error occurs. Roll initialization NULL.
3. If an error occurs, both messages will be displayed.
4. getch is a function from the platform-specific conio.h header. DOS/Windows only.
5. Accident handling is usually done by reference.
6. For the first parameter setlocale, you must specify a non-zero mask, on which parts to set the locale. LC_ALL - everywhere. What to write as the second parameter depends on the runtime library.

M
MiiNiPaa, 2016-03-01
@MiiNiPaa

I'll post the answers to the questions in the comments here:

Is system("pause") platform independent?
Not
After all, the program immediately closes. Or is it handled in some other way?
Set up the IDE normally, or run programs from the command line instead of double-clicking. This is normal and correct behavior for console programs.
Is the reference only used with std::bad_alloc or with classes too?
C++ exception handling standard: throw by value, catch by reference.
That is, pointers of this type must always be initialized? If not char* ptr = new char[SIZE], then char* ptr = NULL and on the other line ptr = new char[SIZE]?
If it char* ptr = new char[SIZE]throws an exception, then the ptr variable does not exist and there is nothing to delete. You should always initialize a variable when you declare it. What is another question.
And yes, on modern systems, the only guaranteed way to get bad_alloc is to exhaust the program's address space. And make sure the compiler doesn't throw away the memory allocation as unnecessary. It's easier to throw bad_alloc on your own.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question