Solved

_set_new_handler

Posted on 1997-10-16
5
337 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 100 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

770 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