Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

_set_new_handler

Posted on 1997-10-16
5
Medium Priority
?
358 Views
Last Modified: 2012-05-04
Visual c++4.0
console app

1.if the array is size 1000 1000 1000 the following code works
  but, if somthing like this is used:10000 100 10 it crashes?

2. for the above to work i have to put a exit before the retrurn 0;?
how do i  properly use the _set_new_handler?

thx js




int memory_error(size_t)
{
      cerr<<"The Free Store is empty"
            <<endl<<"Memory reqested not availible";
      exit(1);
      return 0;
}

main(){
_set_new_handler(memory_error);
///dynamicly create a 3d array with new///


return 0;
0
Comment
Question by:sigjas
  • 3
  • 2
5 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 1171582
What do you mean when you list the size in three groups of numbers?

In what way does it crash?  When?

Can you provide examples where you create objects with new?


0
 

Author Comment

by:sigjas
ID: 1171583
the three groups of numbers are the size or the array,they
are passed to the constructor which then creates and initializes
an array...
the error i recieve is an invalid page fault error.
//l1,l2,l3 are passed from the user
 p=new int**[l1];

    for(int n=0;n<s1;n++){
        p[n]=new int*[l2];
        for(int k=0;k<s2;k++){
        p[n][k] = new int[l3];
           
0
 
LVL 22

Accepted Solution

by:
nietod earned 200 total points
ID: 1171584
Your new handler (without the exit) does not do anything except print a message.  It should try to make memory available to the new operator (and return TRUE to indicate new should try again.  If it can't free memory, it should return FALSE, as it does).  
The problem is that when your handler returns FALSE, the new operator returns NULL.  Then you use the pointer returned without  testing if it is NULL.  Thus the crash.  Testing for NULL will prevent the crash, but won't hale you create huge arrays.

If you need truly need arrays (1000,1000,1000) your going to have to impliment them differently.  That array would be 4 gigabytes of storage space!  How much memory does your computer have.  
If you need arrays this big look into sparse arrays (if appropriate) or file based arrays, although you will need alot of disk space.
0
 

Author Comment

by:sigjas
ID: 1171585
The previous was using vc4, now I am using vc1.52
and the max array size if now  t[29][29[29] eg under 57kb

my goal was to display a error message and exit the program
if new failed.
ebuf is global at the begining of the program
my question is:
the message is not displayed if i use cerr
int memory_error(size_t)
{
    delete []ebuf;
      cout<<"The Free Store is empty";
      exit(2);      
      return 0;
}
0
 
LVL 22

Expert Comment

by:nietod
ID: 1171586
I'm having trouble understanding you.  Is your question why doesn't an error message appear printed by you new_handler proedure appear?

If so I have some questions,
   Is this a console or windows app?
   Why do you delete ebuf?
   Do you want your error message to go to cerr instead of cout?  I have never used streams, but If it remember correctly, cout is buffered, so output might not appear if the application terminates too soon.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question