BlackBoard » Design, Programmierung & Entwicklung » Programmieren » C++ Probleme mit set_new_handler » Hallo Gast [Anmelden|Registrieren]
Letzter Beitrag | Erster ungelesener Beitrag Druckvorschau | An Freund senden | Thema zu Favoriten hinzufügen
Neues Thema erstellen Antwort erstellen
Zum Ende der Seite springen Probleme mit set_new_handler
Autor
Beitrag « Vorheriges Thema | Nächstes Thema »
Wenso Wenso ist männlich
Amokschläfer


images/avatars/avatar-1991.gif

Dabei seit: 25.02.2002
Beiträge: 1.715
Herkunft: Hessen

Probleme mit set_new_handler       Zum Anfang der Seite springen

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

__________________
Pipi Langstrumpf ist KRIEG!!!
~for those about to rot~
welcome to where time stands still
no one leaves and no one will

Dieser Beitrag wurde 1 mal editiert, zum letzten Mal von Wenso: 13.11.2008 11:27.

13.11.2008 11:26 Wenso ist offline E-Mail an Wenso senden Homepage von Wenso Beiträge von Wenso suchen
phlox81 phlox81 ist männlich
Bote des Lichts und Moderator


images/avatars/avatar-2264.jpg

Dabei seit: 19.10.2002
Beiträge: 3.028
Herkunft: Irgendwo im Nirgendwo

      Zum Anfang der Seite springen

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.

__________________
Intelligenz ist eine Illusion des Menschen

phlox81.de | codenode.de
13.11.2008 11:57 phlox81 ist offline E-Mail an phlox81 senden Homepage von phlox81 Beiträge von phlox81 suchen
Wenso Wenso ist männlich
Amokschläfer


images/avatars/avatar-1991.gif

Dabei seit: 25.02.2002
Beiträge: 1.715
Herkunft: Hessen

Themenstarter Thema begonnen von Wenso
      Zum Anfang der Seite springen

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

__________________
Pipi Langstrumpf ist KRIEG!!!
~for those about to rot~
welcome to where time stands still
no one leaves and no one will

13.11.2008 12:46 Wenso ist offline E-Mail an Wenso senden Homepage von Wenso Beiträge von Wenso suchen
phlox81 phlox81 ist männlich
Bote des Lichts und Moderator


images/avatars/avatar-2264.jpg

Dabei seit: 19.10.2002
Beiträge: 3.028
Herkunft: Irgendwo im Nirgendwo

      Zum Anfang der Seite springen

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

__________________
Intelligenz ist eine Illusion des Menschen

phlox81.de | codenode.de
13.11.2008 13:38 phlox81 ist offline E-Mail an phlox81 senden Homepage von phlox81 Beiträge von phlox81 suchen
Black Star Black Star ist männlich
Der Pate [Admin]


images/avatars/avatar-2158.jpg

Dabei seit: 11.12.2001
Beiträge: 2.282
Herkunft: /dev/stderr

      Zum Anfang der Seite springen

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.

__________________
vescere bracis meis

15.11.2008 15:22 Black Star ist offline E-Mail an Black Star senden Homepage von Black Star Beiträge von Black Star suchen
Wenso Wenso ist männlich
Amokschläfer


images/avatars/avatar-1991.gif

Dabei seit: 25.02.2002
Beiträge: 1.715
Herkunft: Hessen

Themenstarter Thema begonnen von Wenso
      Zum Anfang der Seite springen

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

__________________
Pipi Langstrumpf ist KRIEG!!!
~for those about to rot~
welcome to where time stands still
no one leaves and no one will

17.11.2008 16:18 Wenso ist offline E-Mail an Wenso senden Homepage von Wenso Beiträge von Wenso suchen
Baumstruktur | Brettstruktur
Gehe zu:
Neues Thema erstellen Antwort erstellen
BlackBoard » Design, Programmierung & Entwicklung » Programmieren » C++ Probleme mit set_new_handler

Forensoftware: Burning Board 2.3.6, entwickelt von WoltLab GmbH