473,398 Members | 2,125 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

How to test memory allocation with new ?

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
Jul 22 '05 #1
13 2186

"jjleto" <jj****@laposte.net> wrote in message
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
Jul 22 '05 #2
> 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 ?
Jul 22 '05 #3

"jjleto" <jj****@laposte.net> wrote in message
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
Jul 22 '05 #4

"Sharad Kala" <no*****************@yahoo.com> wrote in message

"jjleto" <jj****@laposte.net> wrote in message
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
Jul 22 '05 #5
Sharad Kala wrote:
"Sharad Kala" <no*****************@yahoo.com> wrote in message
"jjleto" <jj****@laposte.net> wrote in message
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
Jul 22 '05 #6

"Shailesh Humbad" <no*****@nowhere.com> wrote in message
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
Jul 22 '05 #7
If you have less then 2G of memory you're scrued. I don't think you do
what you intend to do.

jjleto wrote:
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

Jul 22 '05 #8
In gnu.g++.help Sharad Kala <no*****************@yahoo.com> wrote:

: > 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
}

--
-lauther

[nosave]
----------------------------------------------------------------------------
Ulrich Lauther ph: +49 89 636 48834 fx: ... 636 42284
Siemens CT SE 6 Internet: Ul************@siemens.com
Jul 22 '05 #9
jjleto wrote:
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

--
Anti-spam address, change each 'X' to '.' to reply directly.
Jul 22 '05 #10
what is the syntax for specifying nothrow?

Thanks,

David

Sharad Kala wrote:
"jjleto" <jj****@laposte.net> wrote in message
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

Jul 22 '05 #11
Cosmin Calinescu <co*****@nortelnetworks.com> wrote:
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.
Jul 22 '05 #12

"Sharad Kala" <no*****************@yahoo.com> wrote in message
news:2u*************@uni-berlin.de...

"jjleto" <jj****@laposte.net> wrote in message
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)?
Jul 22 '05 #13

"Method Man" <a@b.c> wrote in message
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
Jul 22 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Bob | last post by:
Are there any known applications out there used to test the performance of the .NET garbage collector over a long period of time? Basically I need an application that creates objects, uses them, and...
6
by: chris | last post by:
Hi all, I need to know, what is the difference between dynamic memory allocation, and stack allocation ? 1. If I have a class named DestinationAddress, when should I use dynamic memory...
162
by: techievasant | last post by:
hello everyone, Iam vasant from India.. I have a test+interview on C /C++ in the coming month so plz help me by giving some resources of FAQS, interview questions, tracky questions, multiple...
62
by: ivan.leben | last post by:
How can I really delete a preloaded image from memory/disk cache? Let's say I preload an image by creating an Image object and setting its src attribute to desired URL: var img = new Image();...
66
by: Johan Tibell | last post by:
I've written a piece of code that uses sockets a lot (I know that sockets aren't portable C, this is not a question about sockets per se). Much of my code ended up looking like this: if...
24
by: Ken | last post by:
In C programming, I want to know in what situations we should use static memory allocation instead of dynamic memory allocation. My understanding is that static memory allocation like using array...
1
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was...
34
by: jacob navia | last post by:
Suppose that you have a module that always allocates memory without ever releasing it because the guy that wrote it was lazy, as lazy as me. Now, you want to reuse it in a loop. What do you do?...
14
by: vivek | last post by:
i have some doubts on dynamic memory allocation and stacks and heaps where is the dynamic memory allocation used? in function calls there are some counters like "i" in the below function. Is...
66
by: karthikbalaguru | last post by:
Hi, Will 'free' return the memory Immediately to the OS ? Thx in advans, Karthik Balaguru
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.