BlackBoard (http://www.black-board.net/index.php)
- Design, Programmierung & Entwicklung (http://www.black-board.net/board.php?boardid=55)
-- Programmieren (http://www.black-board.net/board.php?boardid=4)
--- C++ Probleme mit set_new_handler (http://www.black-board.net/thread.php?threadid=23551)


Geschrieben von Wenso am 13.11.2008 um 11:26:

  Probleme mit set_new_handler

Guten Morgen,
Im Zuge meiner Suche habe ich eventuell bei Reliable Software eine Lösung in Form des Folder Watcher gefunden. Allerdings kann ich das Programm mit Dev-Cpp (Version 5.0 beta 9.2 (4.9.9.2) (9.0 MB) with Mingw/GCC 3.4.2) nicht kompilieren.

Ich habe, aufgrund einer vorigen Fehlermeldung, g++ den Parameter "-Wno-deprecated" angehängt.

Die Fehlermeldung ist dann folgende:
code:
1:
2:
3:
 C:\CPP-Dev\folderwatcher-2\main.cpp In function `int WinMain(HINSTANCE__*, HINSTANCE__*, char*, int)': 
19 C:\CPP-Dev\folderwatcher-2\main.cpp `_set_new_handler' undeclared (first use this function) 
  (Each undeclared identifier is reported only once for each function it appears in.) 


In Zeile 19 steht folgendes:
code:
1:
_set_new_handler (&NewHandler);



Nun suchte ich nach _set_new_handler und fand auf dieser Seite, auch von Reliable Software, den Ansatz, dass ich den Code von _set_new_handler in set_new_handler änderte und anstatt <new> den Header <new.h> einband.

Nun bekomme ich eine andere Fehlermeldung
code:
1:
2:
3:
 C:\CPP-Dev\folderwatcher\main.cpp In function `int WinMain(HINSTANCE__*, HINSTANCE__*, char*, int)': 
19 C:\CPP-Dev\folderwatcher\main.cpp invalid conversion from `int (*)(size_t)' to `void (*)()' 
19 C:\CPP-Dev\folderwatcher\main.cpp   initializing argument 1 of `void (* std::set_new_handler(void (*)()))()' 


Damit, so muss ich leider zugeben, kann ich garnichts anfangen. Auch mit dem im gelben Kasten Genannten kann ich nichts anfangen.

Für jede Idee, Tipp oder Lösungsvorschlag bin ich dankbar!
Danke im Voraus!


MfG Wenso



Geschrieben von phlox81 am 13.11.2008 um 11:57:

 

Erstmal würde ich dir zu einer anderen IDE raten.
Wenn du schon im Windowsumfeld rumstrickst wäre ein MSVC Express C++ eigentlich zu empfehlen, wenns plattformunabhängig sein soll, ist auch Code::Blocks sehr gut.
Und von MSVC liegt ja auch schon ein arbeitsbereich dem Code bei.

Du solltest keine C++ Standard Header mit .h verwenden, die sind einfach uralt.
_set_new_handler solltest du auch besser nicht nutzten, das ist nicht wirklich das Richtige.



Geschrieben von Wenso am 13.11.2008 um 12:46:

 

Ich wollte das ganze anfangs mit Eclipse / CDT / cygwin machen, aber da verhinderte die Firewall hier mein vorhaben.

Plattforumunabhängig muss es nicht sein.

Mit MSVC++ würde ich es gerne versuchen, aber für die Installation benötigt ich Admin-Rechte. Ich habe den Kollegen, der mir die Aufgabe gab, schonmal danach gefragt, aber er ist momentan noch beschäftigt. Aber ich bleibe am Ball Augenzwinkern


Also, ich verwende besser die <new> anstatt der <new.h>, right?

Was für andere Möglichkeiten hätte ich anstatt _set_new_handler?


MfG Wenso



Geschrieben von phlox81 am 13.11.2008 um 13:38:

 

ja, <new> ist korrekt.
Danach solltest du dann aber ein using namespace std; einfügen, da das alles jetzt in einem Namensraum liegt.

Bei mir kompiliert es übrigens mit dem aktuellen Visualstudio sofort.

Aber du solltest auch beachten, das der Code von 1997 ist, er ist also schon 11(!) Jahre alt.

Evtl. fragst du mal in c-plusplus.de/forum im MFC oder WinApi Forum nach, ob es nicht was aktuelleres gibt.

phlox



Geschrieben von Black Star am 15.11.2008 um 15:22:

 

Aus 'Effective C++' von Scott Meyers:

Zitat:
To specify the out-of-memory-handling function, clients call set_new_handler, which is specified in the header
<new> more or less like this:
typedef void (*new_handler)();
new_handler set_new_handler(new_handler p) throw();
As you can see, new_handler is a typedef for a pointer to a function that takes and returns nothing, and
set_new_handler is a function that takes and returns a new_handler.
set_new_handler's parameter is a pointer to the function operator new should call if it can't allocate the
requested memory. The return value of set_new_handler is a pointer to the function in effect for that purpose
before set_new_handler was called.
You use set_new_handler like this:
// function to call if operator new can't allocate enough memory
void noMoreMemory()
{
cerr << "Unable to satisfy request for memory\n";
abort();
}
int main()
{
set_new_handler(noMoreMemory);
int *pBigDataArray = new int[100000000];
...
}
If, as seems likely, operator new is unable to allocate space for 100,000,000 integers, noMoreMemory will be
called, and the program will abort after issuing an error message. This is a marginally better way to terminate the
program than a simple core dump. (By the way, consider what happens if memory must be dynamically allocated
during the course of writing the error message to cerr...)
When operator new cannot satisfy a request for memory, it calls the new-handler function not once, but repeatedly
until it can find enough memory. The code giving rise to these repeated calls is shown in Item 8, but this high-level
description is enough to conclude that a well-designed new-handler function must do one of
the following:
-- Make more memory available. This may allow operator new's next attempt to allocate the memory to
succeed. One way to implement this strategy is to allocate a large block of memory at program start-up, then
release it the first time the new-handler is invoked. Such a release is often accompanied by some kind of
warning to the user that memory is low and that future requests may fail unless more memory is somehow
made available.
-- Install a different new-handler. If the current new-handler can't make any more memory available, perhaps it
knows of a different new-handler that is more resourceful. If so, the current new-handler can install the other
new-handler in its place (by calling set_new_handler). The next time operator new calls the new-
handler function, it will get the one most recently installed. (A variation on this theme is for a new-handler to
modify its own behavior, so the next time it's invoked, it does something different. One way to achieve this is
to have the new-handler modify static or global data that affects the new-handler's behavior.)
-- Deinstall the new-handler, i.e., pass the null pointer to set_new_handler. With no new-handler installed,
operator new will throw an exception of type std::bad_alloc when its attempt to allocate memory is
unsuccessful.
-- Throw an exception of type std::bad_alloc or some type derived from std::bad_alloc. Such
exceptions will not be caught by operator new, so they will propagate to the site originating the request for
memory. (Throwing an exception of a different type will violate operator new's exception specification.
The default action when that happens is to call abort, so if your new-handler is going to throw an exception,
you definitely want to make sure it's from the std::bad_alloc hierarchy. For more information on
exception specifications, see Item M14.)
-- Not return, typically by calling abort or exit, both of which are found in the standard C library (and thus
in the standard C++ library — see Item 49).
These choices give you considerable flexibility in implementing new-handler functions.


Da steht erstmal alles noetige, was man wissen muss, um set_new_handler() zu benutzen, speziell dass es eine void-Funktion sein muss, und dass man dem Handler einen Zeiger auf diese Funktion uebergeben muss.

Fuer weiters kann ich dir das Buch von Scott Meyers waermstens empfehlen, da steht alles wichtige in Sachen C++-Grundlagen drin.



Geschrieben von Wenso am 17.11.2008 um 16:18:

 

Zitat:
Original von phlox81
Bei mir kompiliert es übrigens mit dem aktuellen Visualstudio sofort.

Es war ein wenig mühselig, bis ich VC++ hier am laufen hatte, aber damit funktioniert es auch hier.

Ich stöbere noch ein wenig im CPP-Forum rum, wenn ich Zeit für die Aufgabe habe, danke für den Tip!

Und auch danke an Black Star für den Buchauszug smile

MfG Wenso


Forensoftware: Burning Board 2.3.6, entwickelt von WoltLab GmbH