When new operator fails in pre-1997 C++.

G

Guest

Hi,

I've been trying to look up what to do when the new operator is unable
to allocate any memory in pre-1997 C++ (I have managed to get an
answer to this question for current versions.). "Teach Yourself C++"
states that after I use

int *y=new int[6];

I should test with

if (!y).

However, Borland C++ states that, instead of y equalling null, an
exception (xalloc) is thrown, so I'm not sure which is correct. It
also states that I need to #include <except.h> to get the exception
and doesn't state what happens if I use new without that inclusion and
DMA fails.

Can anyone help?

James McLaughlin.
 
K

Karl Heinz Buchegger

Hi,

I've been trying to look up what to do when the new operator is unable
to allocate any memory in pre-1997 C++

There is no definite answer to this. C++ was standardized in 1998, there
was no standard before, so every compiler vendor did as it plpeased
him.
(I have managed to get an
answer to this question for current versions.). "Teach Yourself C++"
states that after I use

int *y=new int[6];

I should test with

if (!y).

However, Borland C++ states that, instead of y equalling null, an
exception (xalloc) is thrown, so I'm not sure which is correct.

In prestandard C++: neither. There is correct answer per definition
(better said: per missing definition :)
It
also states that I need to #include <except.h> to get the exception
and doesn't state what happens if I use new without that inclusion and
DMA fails.

Can anyone help?

Get a newer compiler.
That's the best option you have.
 
T

tom_usenet

Hi,

I've been trying to look up what to do when the new operator is unable
to allocate any memory in pre-1997 C++ (I have managed to get an
answer to this question for current versions.).

pre-1997 C++ was a collection of C++ dialects used supported by
various compiler vendors.

"Teach Yourself C++"
states that after I use

int *y=new int[6];

I should test with

if (!y).

However, Borland C++ states that, instead of y equalling null, an
exception (xalloc) is thrown, so I'm not sure which is correct. It
also states that I need to #include <except.h> to get the exception
and doesn't state what happens if I use new without that inclusion and
DMA fails.

Can anyone help?

Read your compiler documentation - different compilers do it in
different ways. This is exactly the kind of reason why C++ was
standardised in the first place! :)

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,174
Messages
2,570,940
Members
47,485
Latest member
Andrewayne909

Latest Threads

Top