_new_handler() in gnu/g++

O

openbysource

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
 
J

John Harrison

openbysource said:
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 <new> header 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top