By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 832 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

_new_handler() in gnu/g++

P: n/a
Hello,

I went through a C++ program to handler memory management via "new".
It used a set_new_handler() function which prints a error message when
any memory exhaustion occurs.
I tried using that in my C++ program in Linux but my compiler (gcc)
reported a error saying:

test11.cpp: In function 'int main()':
test11.cpp:8: error: variable or field 'set_new_handler' declared void
test11.cpp:8: error: invalid lvalue in unary '&'
test11.cpp:14: error: 'set_new_handler' cannot be used as a function

I am pasting my program too:

#include <iostream>
#include <stdlib.h>
#include <new>
using namespace std;
main()
{
void memwarning();
void set_new_handler(memwarning);

char *p=new char[100];
cout << "First allocation: p = " << hex << long(p) << endl;

p=new char[64000u];
set_new_handler(0);
}
void memwarning()
{
cout << "Free store exhausted" << endl;
exit(1);
}

Please help.
Regards

Feb 24 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
openbysource wrote:
Hello,

I went through a C++ program to handler memory management via "new".
It used a set_new_handler() function which prints a error message when
any memory exhaustion occurs.
I tried using that in my C++ program in Linux but my compiler (gcc)
reported a error saying:

test11.cpp: In function 'int main()':
test11.cpp:8: error: variable or field 'set_new_handler' declared void
test11.cpp:8: error: invalid lvalue in unary '&'
test11.cpp:14: error: 'set_new_handler' cannot be used as a function
Several mistakes, corrections below.
I am pasting my program too:

#include <iostream>
#include <stdlib.h>
#include <new>
using namespace std;

Delete from here to the corrected code below.
main()
{
void memwarning();
void set_new_handler(memwarning);
void memwarning();

int main()
{
set_new_handler(memwarning);
char *p=new char[100];
cout << "First allocation: p = " << hex << long(p) << endl;

p=new char[64000u];
set_new_handler(0);
}
void memwarning()
{
cout << "Free store exhausted" << endl;
exit(1);
}

Please help.
Regards
1) Put prototypes outside of any function (i.e. void memwarning() should
go before main). This is not an error just good style.

2) Don't put void in front of a function call, i.e.

set_new_handler(memwarning);

not

void set_new_handler(memwarning);

It's quite common for newbies to confuse function prototypes with
function calls, I'd say you've got a bad case of that. Remember a
function prototype tells the compiler about the signature of a function,
you don't need to do that for set_new_handler because it's done for you
in the <newheader file. But you do need to actually call the
set_new_handler function.

3) main returns an int, main() is not legal, int main() is.

john
Feb 24 '07 #2

P: n/a
Thanks.
It's working.

Feb 24 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.