How to test memory allocation with new ?

J

jjleto

Hello,

How do I handle the following :

------>
#include <iostream>
using namespace std;

int main()
{
int n = 0x7FFFFFFF;
char *pp = new char(n);
if ( pp != NULL ) {
pp[0] = 0;
pp[n-1] = 0;
cout << "OK" << endl;
} else {
cout << "FAILED" << endl;
}
}
------>


It compiles OK, but when I run it, I get a segmentation fault. I thought
that testing a NULL value was enough for testing memory allocation (or
perhaps it is a bug ? I use gcc (GCC) 3.3.4)

Regards,
jjleto
 
S

Sharad Kala

jjleto said:
Hello,

How do I handle the following :

------>
#include <iostream>
using namespace std;

int main()
{
int n = 0x7FFFFFFF;
char *pp = new char(n);

This does not do what you are thinking. It allocates memory for one char and
tries to initialize it with 0x7FFFFFFF. To allocate an array use -
char *pp = new char[...];
if ( pp != NULL ) {

This is a big misconception that new returns NULL on failure. The fact is
that it throws std::bad_alloc exception on failure (unless you specify
nothrow)
pp[0] = 0;
pp[n-1] = 0;

This is the cause of your segmentation fault. You are trying to write to an
illegal memory location.
cout << "OK" << endl;
} else {
cout << "FAILED" << endl;
}
}


Sharad
 
J

jjleto

char *pp = new char[...];

Thanks, the segmentation fault was actually for the reason you gave.

Nevertheless, I still have an error when running this program ("give up"
but not a segfault)

#include <iostream>
using namespace std;

int main()
{
int n = 0x7FFFFFFF;
char *pp = new char[n];
if ( pp != NULL ) {
pp[0] = 0;
pp[n-1] = 0;
cout << "OK" << endl;
} else {
cout << "FAILED" << endl;
}
}

How do I test memory allocation with new ?
 
S

Sharad Kala

jjleto said:
char *pp = new char[...];

Thanks, the segmentation fault was actually for the reason you gave.

Nevertheless, I still have an error when running this program ("give up"
but not a segfault)

How do I test memory allocation with new ?

I told you in my earlier reply too that new throws std::bad_alloc on
exception. You need to wrap the new in a try-catch block (catching
std::bad_alloc).

Sharad
 
S

Sharad Kala

Sharad Kala said:
jjleto said:
char *pp = new char[...];

Thanks, the segmentation fault was actually for the reason you gave.

Nevertheless, I still have an error when running this program ("give up"
but not a segfault)

How do I test memory allocation with new ?

I told you in my earlier reply too that new throws std::bad_alloc on
exception. You need to wrap the new in a try-catch block (catching
std::bad_alloc).

Something like -
try{
char *pp = new char[n];
//...
}
catch(std::bad_alloc)
{
cout << "New failed";
}

Sharad
 
S

Shailesh Humbad

Sharad said:
Sharad Kala said:
jjleto said:
char *pp = new char[...];

Thanks, the segmentation fault was actually for the reason you gave.

Nevertheless, I still have an error when running this program ("give up"
but not a segfault)

How do I test memory allocation with new ?

I told you in my earlier reply too that new throws std::bad_alloc on
exception. You need to wrap the new in a try-catch block (catching
std::bad_alloc).


Something like -
try{
char *pp = new char[n];
//...
}
catch(std::bad_alloc)
{
cout << "New failed";
}

Sharad

Note for those using MS VC++ (5.0 or higher), it has a bug in the
implementation of new where it doesn't throw the std::bad_alloc
exception on failure. I just found this out when I tried your example.

http://support.microsoft.com/?kbid=167733
 
S

Sharad Kala

Shailesh Humbad said:
Something like -
try{
char *pp = new char[n];
//...
}
catch(std::bad_alloc)
{
cout << "New failed";
}

Sharad

Note for those using MS VC++ (5.0 or higher), it has a bug in the
implementation of new where it doesn't throw the std::bad_alloc
exception on failure. I just found this out when I tried your example.

Well there was no Standard formally in place in the time of MS VC 6.0
(that's 1997) and its ancestors. So one can't actually blame the compilers
for being non-conformant.

Sharad
 
C

Cosmin Calinescu

If you have less then 2G of memory you're scrued. I don't think you do
what you intend to do.
 
U

Ulrich Lauther

: > I told you in my earlier reply too that new throws std::bad_alloc on
: > exception. You need to wrap the new in a try-catch block (catching
: > std::bad_alloc).

: Something like -
: try{
: char *pp = new char[n];
: //...
: }
: catch(std::bad_alloc)
: {
: cout << "New failed";
: }

: Sharad


or else:

char* pp = new(nothrow) char[n];
if (! pp) { // new failed
// do something
}
 
L

Larry I Smith

jjleto said:
Hello,

How do I handle the following :

------>
#include <iostream>
using namespace std;

int main()
{
int n = 0x7FFFFFFF;
char *pp = new char(n);
if ( pp != NULL ) {
pp[0] = 0;
pp[n-1] = 0;
cout << "OK" << endl;
} else {
cout << "FAILED" << endl;
}
}
------>


It compiles OK, but when I run it, I get a segmentation fault. I thought
that testing a NULL value was enough for testing memory allocation (or
perhaps it is a bug ? I use gcc (GCC) 3.3.4)

Regards,
jjleto

#include <iostream>
using namespace std;

int main()
{
char * pp;

// given this value, 'new' will fail if less than 2GB of free RAM
// is available
int n = 0x7FFFFFFF;

try
{
cout << "Allocate an array of " << n << " bytes in RAM" << endl;
pp = new char[n];
}
catch (bad_alloc)
{
cout << "Allocation Attempt Failed" << endl;
return 1;
}

pp[0] = 0;
pp[n - 1] = 0;
cout << "OK" << endl;

delete[] pp; // free the allocated array

return 0;
}

Regards,
Larry
 
D

David Lindauer

what is the syntax for specifying nothrow?

Thanks,

David

Sharad said:
jjleto said:
Hello,

How do I handle the following :

------>
#include <iostream>
using namespace std;

int main()
{
int n = 0x7FFFFFFF;
char *pp = new char(n);

This does not do what you are thinking. It allocates memory for one char and
tries to initialize it with 0x7FFFFFFF. To allocate an array use -
char *pp = new char[...];
if ( pp != NULL ) {

This is a big misconception that new returns NULL on failure. The fact is
that it throws std::bad_alloc exception on failure (unless you specify
nothrow)
pp[0] = 0;
pp[n-1] = 0;

This is the cause of your segmentation fault. You are trying to write to an
illegal memory location.
cout << "OK" << endl;
} else {
cout << "FAILED" << endl;
}
}

Sharad
 
O

Old Wolf

Cosmin Calinescu said:
If you have less then 2G of memory you're scrued. I don't think you do
what you intend to do.
int n = 0x7FFFFFFF;
char *pp = new char[n];

Many operating systems have "lazy allocation", they will honour
this request but then swap maniacally or kill the application
when it tries to actually use that memory.
 
M

Method Man

Sharad Kala said:
jjleto said:
Hello,

How do I handle the following :

------>
#include <iostream>
using namespace std;

int main()
{
int n = 0x7FFFFFFF;
char *pp = new char(n);

This does not do what you are thinking. It allocates memory for one char and
tries to initialize it with 0x7FFFFFFF. To allocate an array use -
char *pp = new char[...];
if ( pp != NULL ) {

This is a big misconception that new returns NULL on failure. The fact is
that it throws std::bad_alloc exception on failure (unless you specify
nothrow)
pp[0] = 0;
pp[n-1] = 0;

This is the cause of your segmentation fault. You are trying to write to an
illegal memory location.

Do you mean both statements are problematic? Shouldn't 'pp[0] = 0' be OK
since it is equivalent to *pp = 0 (assigning the allocated char the null
value)?
 
S

Sharad Kala

Method Man said:
Do you mean both statements are problematic? Shouldn't 'pp[0] = 0' be OK
since it is equivalent to *pp = 0 (assigning the allocated char the null
value)?

No, only pp[1] is problematic. This gets evaluated to *(pp + 1), pp [0] as
you point out is same as *pp.

Sharad
 

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

No members online now.

Forum statistics

Threads
474,183
Messages
2,570,967
Members
47,517
Latest member
Andres38A1

Latest Threads

Top