Solved

_set_new_handler

Posted on 1997-10-16
5
333 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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 Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
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.

728 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now