473,322 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

_new_handler() in gnu/g++

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
2 2662
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
Thanks.
It's working.

Feb 24 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: Peter Olcott | last post by:
// // Is there something wrong with my syntax for the // Copy Constructor of an Array Element, or does // the C++ language not support this? // #include <stdio.h> #include <stdlib.h> ...
5
by: Ruslan Vorobey | last post by:
Hi All. Does anybody saw any C++ new operator implementations? I need just to know does it use malloc? Especially I interesting about standard C++ libs implementation under AIX 5.1L. Thank...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.