when can new fail to accocate memory??

D

doublemaster007

Is that only when system has insufficient memory?? is there any ways
to know why new has failed? (i am on using unix)
 
M

Michael Doubez

Is that only when system has insufficient memory??

The object allocated could also have redefined new/delete to suit his
needs and have cases where allocation is impossible or forbidden
(allocation in a pool or if memory is too fragmented for the size of
the chunk required).

is there any ways
to know why new has failed? (i am on  using unix)

AFAIK no.
 
P

peter koch

Is that only when system has insufficient memory?? is there any ways
to know why new has failed? (i am on  using unix)

If new fails these are the reasons:

1) there is not enough memory available to the program (the system
might have loads of available memory but has limited the memory
available to your program).
2) There is enough memory, but it has been fragmented and can't
allocate a large enough chunk for you.
3) (quite common on comp.lang.c++) you have involved undefined
behaviour, e.g. by accessing memory you should not touch yourself,
causing the memory manager to give erroneous results.

/Peter
 
J

James Kanze

Is that only when system has insufficient memory??

Technically, it's anytime the runtime library decides that it
can't or doesn't want to give you memory. Typically, the
runtime library will try to get more memory from the system, and
operator new will only fail if this fails, but there's no
requirement either way. On one hand, the runtime might attempt
to create more virtual memory, on systems where that's possible,
or simply wait awhile, and try again, on the hope that some
other process will have freed memory in the meantime. On the
other, it might decide to artificially restraint the process to
some maximum, for who knows what reason. (My debugging operator
new will "fail" on command, for test purposes.)

In practice, such cases are rare---most operator new simply call
malloc, and most malloc request more memory from the system, if
they need it, and only fail if that request fails.
is there any ways to know why new has failed?

Not really. For the most part, you can simply suppose that it
is because the system ran out of memory to allocate.
(i am on using unix)

You might, but I doubt it. Unix reports the error reason in
errno, but the only documented error for malloc in Posix is
ENOMEM: insufficient storage available.

Note that some Unix, at least by default, don't fail when there
is insufficient memory. (Linux is in this category.) They just
return a pointer which will core dump when you use it.
 
P

peter koch

Is that only when system has insufficient memory?? [snip]
(i am on  using unix)

You might, but I doubt it.  Unix reports the error reason in
errno, but the only documented error for malloc in Posix is
ENOMEM: insufficient storage available.

Note that some Unix, at least by default, don't fail when there
is insufficient memory.  (Linux is in this category.)  They just
return a pointer which will core dump when you use it.
I am not by any means a Linux expert, but I believe that you are wrong
here. Overcommitment of memory is configurable (man sysconf, if I
remember correctly) and look for something like "overcommit".

/Peter
 
J

James Kanze

[snip]
(i am on using unix)
You might, but I doubt it. Unix reports the error reason in
errno, but the only documented error for malloc in Posix is
ENOMEM: insufficient storage available.
Note that some Unix, at least by default, don't fail when there
is insufficient memory. (Linux is in this category.) They just
return a pointer which will core dump when you use it.
I am not by any means a Linux expert, but I believe that you
are wrong here. Overcommitment of memory is configurable (man
sysconf, if I remember correctly) and look for something like
"overcommit".

It's configurable, but the default configurations in all of the
distributions I know do the wrong thing.
 
J

Jorgen Grahn

If new fails these are the reasons:

1) there is not enough memory available to the program (the system
might have loads of available memory but has limited the memory
available to your program).
2) There is enough memory, but it has been fragmented and can't
allocate a large enough chunk for you.
3) (quite common on comp.lang.c++) you have involved undefined
behaviour, e.g. by accessing memory you should not touch yourself,
causing the memory manager to give erroneous results.

4) Unix user limits (ulimit/limit) restrict how much memory your
process is allowed to use. (These are normally set to 'unlimited',
but I guess you may want to set limits here, at least as a
debugging tool).

/Jorgen
 

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,159
Messages
2,570,881
Members
47,418
Latest member
NoellaXku

Latest Threads

Top