?
Solved

_set_new_handler

Posted on 1997-10-16
5
Medium Priority
?
345 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses
Course of the Month10 days, 18 hours left to enroll

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