473,385 Members | 1,427 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,385 software developers and data experts.

forcing new to fail (or throw an exception)


Hello,

Here is a little question. I was reading up on the FAQ on pointers:
http://www.parashift.com/c++-faq-lit....html#faq-16.6

and wanted to see what g++ (ver. 4.1.3) does if it cannot allocate
enough memory by trying to allocating huge amount. Here is what I am trying:
int main(){
double *ldP;
ldP = new double [2048*2048*2048];

delete ldP;
return 0;
}

It compiles okay. It runs okay too.

What am I missing here? How can I try to allocate memory huge enough
that new throws an exception?

thanks,
->HS
Jul 25 '07 #1
5 1645
H.S. wrote:
Hello,

Here is a little question. I was reading up on the FAQ on pointers:
http://www.parashift.com/c++-faq-lit....html#faq-16.6

and wanted to see what g++ (ver. 4.1.3) does if it cannot allocate
enough memory by trying to allocating huge amount. Here is what I am
trying: int main(){
double *ldP;
ldP = new double [2048*2048*2048];
Try

size_t s = 2048*2048*2048;
std::cout << "About to allocate " << s << " doubles" << std::endl;
double ldP = new double[s];
>
delete ldP;
Should be

delete[] ldP;
return 0;
}

It compiles okay. It runs okay too.

What am I missing here? How can I try to allocate memory huge enough
that new throws an exception?
Hard to say. Your program (due to wrong 'delete') had undefined
behaviour. Try fixing it.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 25 '07 #2
Victor Bazarov wrote:
H.S. wrote:
>Hello,

Here is a little question. I was reading up on the FAQ on pointers:
http://www.parashift.com/c++-faq-lit....html#faq-16.6

and wanted to see what g++ (ver. 4.1.3) does if it cannot allocate
enough memory by trying to allocating huge amount. Here is what I am
trying: int main(){
double *ldP;
ldP = new double [2048*2048*2048];

Try

size_t s = 2048*2048*2048;
which generates:
$g++ -o testmem testmem.cc
testmem.cc: In function ‘int main()’:
testmem.cc:5: warning: overflow in implicit constant conversion
$./testmem
About to allocate 0 doubles
std::cout << "About to allocate " << s << " doubles" << std::endl;
double ldP = new double[s];
> delete ldP;

Should be

delete[] ldP;
Thanks for the correction.
> return 0;
}

It compiles okay. It runs okay too.

What am I missing here? How can I try to allocate memory huge enough
that new throws an exception?

Hard to say. Your program (due to wrong 'delete') had undefined
behaviour. Try fixing it.
So after removing my mistakes, and correcting the one in your code (sort
of), here is what throws the exception (this is on a Debian Testing
kernel, 2.6.21, since max memory allocation depends on the kernel
options(?)):

#include <iostream>
int main(){
double *ldP;
size_t s = 2048*2048*58;
std::cout << "About to allocate " << s << " doubles" << std::endl;
ldP = new double [s];

delete [] ldP;
return 0;
}

$g++ -o testmem testmem.cc
$./testmem
About to allocate 243269632 doubles
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
Aborted

thanks,
->HS
Jul 25 '07 #3

H.S. <hs**************@gmail.comwrote in message...
>
#include <iostream>
int main(){
// double *ldP;
size_t s = 2048*2048*58;
std::cout << "About to allocate " << s << " doubles" << std::endl;
// ldP = new double [s];
double *ldP( new double[ s ] );
delete [] ldP;
return 0;
}

$g++ -o testmem testmem.cc
$./testmem
About to allocate 243269632 doubles
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
Aborted

thanks,
->HS
FYI: When you want a BIG number, try this:

// std::size_t big(-1); // compiler 'warning', but usually works (a)
int bigint(-1);
std::size_t big( bigint );
// #include <limits>
std::size_t big2( std::numeric_limits<std::size_t>::max() );

std::cout<<"size_t big()="<<big<<std::endl;
std::cout<<"size_t big2()="<<big2<<std::endl;

// out: size_t big()=4294967295
// out: size_t big2()=4294967295

Of course the '-1' trick (a) only works on 'unsigned' types
(, and may be UB on some systems (?)).
Use the 'numeric_limits<>' version.

--
Bob R
POVrookie
Jul 25 '07 #4
BobR wrote:
FYI: When you want a BIG number, try this:

// std::size_t big(-1); // compiler 'warning', but usually works (a)
int bigint(-1);
std::size_t big( bigint );
// #include <limits>
std::size_t big2( std::numeric_limits<std::size_t>::max() );

std::cout<<"size_t big()="<<big<<std::endl;
std::cout<<"size_t big2()="<<big2<<std::endl;

// out: size_t big()=4294967295
// out: size_t big2()=4294967295

Of course the '-1' trick (a) only works on 'unsigned' types
(, and may be UB on some systems (?)).
No, it's well defined. The result should be the least unsigned integer
congruent to -1 modulo 2**N (where ** means power, and N is the number of
bits in std::size_t), and that would be 2**N - 1.
Use the 'numeric_limits<>' version.
The numeric_limits<version also works on signed integers. And it doesn't
confuse readers who doesn't know that std::size_t is unsigned, or haven't
studied the technicalities of integral conversions.

And: std::size_t is defined in <cstddef(and some of the other C headers).
It should be included to use std::size_t

--
rbh
Jul 26 '07 #5

Robert Bauck Hamar <ro**********@ifi.uio.nowrote in message...
BobR wrote:
// std::size_t big(-1); // compiler 'warning', but usually works (a)
[snip]
Of course the '-1' trick (a) only works on 'unsigned' types
(, and may be UB on some systems (?)).

No, it's well defined. The result should be the least unsigned integer
congruent to -1 modulo 2**N (where ** means power, and N is the number of
bits in std::size_t), and that would be 2**N - 1.
Thanks.

--
Bob R
POVrookie
Jul 26 '07 #6

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

Similar topics

0
by: arun gunda | last post by:
I want to throw exception dynamically. This what I want to do For example I want to throw System.Net.WebException exception. I will know the full exception name at run time, can I create a...
3
by: Kerri | last post by:
Hi, I am new to .NET In my Error Logic on my Aspx pages when an error happens it hits my catch statement where I throw an Exception. My question is : what is the difference between Thwo...
2
by: Dave | last post by:
Josuttis states that I may not throw an exception of type exception or of one of the standard exception types used for language support. Where in the Standard am I forbidden from "throw...
2
by: TS | last post by:
i'm wondering if it is preferred practice to throw exception in this circumstance. I have seen it done like that, but i have also read that you should try to never throw an exception in...
1
by: z. f. | last post by:
in vb asp.net page i'm overriding the finalize method in order to make cleanup. if i throw exception there it is not seen on the page. probably because the page has already sent to the client. is...
3
by: Ryan Liu | last post by:
Hi, In the .NET Framework SDK documentation, I can see DataRow.AcceptChanges method will throw RowNotInTableException exeception. And in DataTable.AcceptChanges(), the documentation does not...
5
by: Rob Dob | last post by:
I am trying to set the NullValue within the Column properties of my Dataset in VS2005. The DataType is a System.DateTime. and when I try and change it from "(Throw Exception)" I get the following...
0
by: Steve B. | last post by:
Hi, I'm wondering how to correctly throw exception within ASP.Net pages. I've page wich which waits for an "id" parameter in the querystring. I want to validate this param. I've wrote this...
1
by: =?Utf-8?B?TVIgRQ==?= | last post by:
This may seem like a stupid question but in C#: Say for instance I have a set of SQL processes that I run via ExecuteReader(). These processes return several pieces of information to the...
4
by: George2 | last post by:
Hello everyone, In Bjarne's book, it is mentioned that sort of STL may throw exception, like sorting elements in a vector. In what situation will sort throw exception? I can not find a case....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.