Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   _set_new_handler() problem header? (http://www.programmingforums.org/showthread.php?t=15811)

JD-Salinger May 10th, 2008 7:40 AM

_set_new_handler() problem header?
 
As i try to compile the program, there are some errors in it, stating that redefinition of class bad_alloc() the code is this
:

  1. #include <new.h>
  2. #include <iostream>
  3. using std::cout;
  4. using std::cerr;
  5. using std::cin;
  6. using std::endl;
  7.  
  8. class bad_alloc {};
  9.  
  10. int NewHandler (size_t size)
  11. {
  12.     throw bad_alloc ();
  13.     return 0;
  14. }
  15. int main ()
  16. {
  17.         //_set_new_handler (NewHandler);
  18.         try
  19.         {
  20.                 status status;
  21.                 SymbolTable symTab;
  22.                 Function::Table funTab (symTab);
  23.                 Store store (symTab);
  24.  
  25.                 cerr << "\nEnter empty line to quit\n";
  26.                 do
  27.                 {
  28.                         cerr << "> "// prompt
  29.                         Scanner scanner (cin);
  30.  
  31.                         if (!scanner.IsEmpty ())
  32.                         {
  33.                                 Parser  parser (scanner, store, funTab, symTab);
  34.                                 status = parser.Parse ();
  35.                                 if (status == stOk)
  36.                                 {
  37.                                         double result = parser.Calculate ();
  38.                                         cout << result << endl;
  39.                                 }
  40.                                 else
  41.                                 {
  42.                                         cerr << "Syntax error.\n";
  43.                                 }
  44.                         }
  45.                         else
  46.                         {
  47.                                 break;
  48.                         }
  49.                 } while (status != stQuit);
  50.         }
  51.         catch (bad_alloc)
  52.         {
  53.                 cerr << "Out of memory!\n";
  54.         }
  55.         catch (...)
  56.         {
  57.                 cerr << "Internal error\n";
  58.         }
  59.         return 0;
  60. }


and then after that i tried to remove the code in line #8 and i tried compiling it again, but another error pops out...saying set_new_handler was not declared in this scope. i tried changing <new.h> to <new> but it still returns same error... does set_new_handler function still exists? cause i just copied the source from a book and the book was way back to 1996.... or I just included a wrong header file? or the function name has changed? thank you so much

grumpy May 10th, 2008 10:09 AM

Re: _set_new_handler() problem header?
 
I can make an informed guess about the author of the book, but will not reveal that here. The main value of your book is in kilojoules that might be released by burning it.

In any event;

1) There is no header named <new.h> in the C++ standard. However, some pre-standard C++ compilers (produced while the standard was an evolving draft) do support such a header. And, in those headers, bad_alloc was not in namespace std. If you are using one of those old compilers (or some more recent ones that supply such headers for backward compatibility with code written targeting those older compilers) this will explain the ambiguity your compiler is reporting about bad_alloc. Essentially, you have a header declaring a type named bad_alloc and another redeclaring such a type in code. Technically, this is a violation of the One Definition Rule (ODR), so it is actually undefined behaviour. This means, if your compiler library has a header named <new.h> and it declares a bad_alloc type, then your compiler is neither correct nor incorrect in complaining about ambiguity.

2) There is no function named _set_new_hander() in the C++ standard, and never was in any drafts either. There is a function named std::set_new_handler() [ie in namespace std, and without the leading underscore] declared in the standard header <new>, and it is a function that accepts a pointer to a void function with no arguments (not a function accepting a size_t argument, as in your NewHandler() function). If your book is written using _set_new_handler(), then it is a book that was written with a specific compiler in mind ..... ie it is vendor specific.

3) There are several other types (status, SymbolTable, Function::Table, Store, ....) that are completely non-standard. Use of these without any declaration will make any standard compliant compiler complain rather bitterly.

JD-Salinger May 10th, 2008 9:08 PM

Re: _set_new_handler() problem header?
 
The book was really outdated, but the way the author presented the features of c++ is very simple and easy to understand, he presented the features of c++ in real life situations unlike other books which contains bag of tricks,but does not show on how it can be applied in real life programming, I read another book titled "accelerated c++" but the way he presented the information makes me hard to grasp. I am not really good in english so i am having a really hard time understanding other books, but the book i am reading really suits me, so what i do is practicing and coding the information on the book and supplementing it with other reference books.... Anyway, thanks a lot grumpy, you are too cool knowing lots of stuff in programming, I envy you a lot... more power! thank you

grumpy May 10th, 2008 10:37 PM

Re: _set_new_handler() problem header?
 
I sympathise on the problem of finding easy to read books on C++ for beginners; the most knowledgeable authors tend to write in a style that assumes the reader is interested in concepts rather than practice, and also has a solid grasp of the fundamentals of the language. Unfortunately, information that is simple and easy to understand is worthless if it is wrong and, if it is believed by beginners, does more harm than good.

JD-Salinger May 10th, 2008 10:58 PM

Re: _set_new_handler() problem header?
 
Quote:

Originally Posted by grumpy (Post 145053)
I sympathise on the problem of finding easy to read books on C++ for beginners; the most knowledgeable authors tend to write in a style that assumes the reader is interested in concepts rather than practice, and also has a solid grasp of the fundamentals of the language. Unfortunately, information that is simple and easy to understand is worthless if it is wrong and, if it is believed by beginners, does more harm than good.

yah... i agree with that, but in my philosophy C++ is a language, just like the english language, and from what i know and in my experience in learning the "english" language, is from practice and using it day by day. The book tends to introduce the concept and its done in practice by coding it, and i think i can easily adapt to the correct use of the language if i applied it in real life situations and experience and ofcors with your help... heheheh.....

lectricpharaoh May 11th, 2008 6:46 AM

Re: _set_new_handler() problem header?
 
If the author of your book is Herbert Schildt, I'd recommend using it only as a paperweight, doorstop, fuel for the fireplace, or page-by-page as cat box liner. Really, he's got that bad of a reputation- so bad, in fact, that the term 'bullschildt' was coined to describe his writings.

JD-Salinger May 11th, 2008 8:13 AM

Re: _set_new_handler() problem header?
 
Quote:

Originally Posted by lectricpharaoh (Post 145055)
If the author of your book is Herbert Schildt, I'd recommend using it only as a paperweight, doorstop, fuel for the fireplace, or page-by-page as cat box liner. Really, he's got that bad of a reputation- so bad, in fact, that the term 'bullschildt' was coined to describe his writings.

Nope... Herbert Schildt is not the author of the book i am reading... but i have one of his books... the C++ Complete Reference Third Edition... I am Reading C++ in Action by Bartosz Milewski... The way he presented the features in c++ is great. . . he presented the features in such a way that youll have an idea on what feature it would be used to... unlike other books which presents concepts and youll be wondering when will you use this feature

lectricpharaoh May 11th, 2008 8:36 AM

Re: _set_new_handler() problem header?
 
Well, now you know what to do with that Schildt book. ;)

JD-Salinger May 11th, 2008 8:55 AM

Re: _set_new_handler() problem header?
 
Quote:

Originally Posted by lectricpharaoh (Post 145057)
Well, now you know what to do with that Schildt book. ;)

The Schildt book is just for reference hehehehehe.... do you have any good book to recommend?


All times are GMT -5. The time now is 4:14 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC