Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

_set_new_handler

Posted on 1997-10-16
5
Medium Priority
?
354 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

609 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