Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder adarshakb's Avatar
    Join Date
    Jun 2009
    Location
    Silicon valley of india
    Posts
    247
    Thanks
    11
    Thanked 1 Time in 1 Post

    set_new_handler error

    this is my code

    #include <iostream.h>
    #include <stdlib.h>
    #include <new.h>

    Code:
    void main()
    {
    	void memFull();
    	set_new_handler(memFull);
    
    	int z[6];
    	cout<<&z;
    }
    void memFull()
    {
    	cout<<"Couldnt allocate memory as memory full ";
    }
    There is a runtime error in setting up set_new_handler
    i cant figure out why


    Another code tat again has problem with set_new_handler

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <string.h>
    #include <new.h>
    
    void memFull()
    {
    	cout<<"MEMORY OVERFLOW";
    	exit(-1);
    }
    
    
    void* operator new(size_t,int);
    void operator delete(void*);
    
    
    void main()
    {
    	
    	set_new_handler(memFull);
    	int *p=new(5) int[10];
    
    
    	for(int i=0;i<10;i++)
    		cout<<p[i]<<"\n";
    
    	delete p;
    }
    
    void* operator new(size_t size,int num)
    {
    	void* p;
    	p=malloc(size);
    
    	if(p==NULL)
    		memFull();
    
    	memset(p,num,size);
    
    	return p;
    }
    
    void operator delete(void* p)
    {
    	free(p);
    }
    And HOW to give set_new_handler function functions with parameters... or is there a no-parameters rule
    Last edited by adarshakb; 07-17-2009 at 08:37 PM.

  • #2
    Regular Coder BrickInTheWall's Avatar
    Join Date
    Mar 2009
    Location
    Munich, Germany
    Posts
    139
    Thanks
    1
    Thanked 13 Times in 13 Posts
    if you are going to define the function after main, you'll have to declare a prototype of the function before main or in one of the header files.

  • #3
    Regular Coder adarshakb's Avatar
    Join Date
    Jun 2009
    Location
    Silicon valley of india
    Posts
    247
    Thanks
    11
    Thanked 1 Time in 1 Post
    i have declared the prototype in the 1st line of main...

    we can declare there or before main...
    still the bug persists at the set_new_handler(memFull) line


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •