Print service provided by iDogiCat: http://www.idogicat.com/
home logo





Home > IT > Programming > C++ Programming > Misc Memo

Misc Memo

C++ boost lib

  • Boost does not support the non-standard "Safe" C++ Library shipping with Visual C++ 8.0, which may result in many spurious warnings from Boost headers and other standards-conforming C++ code. To suppress these warnings, define the macro _SCL_SECURE_NO_DEPRECATE.

Visual C++

  • if you get a 'stack overflow' exception, you can increase the stack size: open project properties dialog, go to Linker | System, change value of 'Stack Reserve Size'. It is set to 1MB by default. The input is in bytes. And BTW, it's almost impossible to catch this exception and handle it gracefully, for when this exception is thrown, there is not enough memory to process the catch block. But strictly speaking, it's still possible to catch with a very MS-specific way (you should include windows.h):

__try {
   // some code here that causes stack overflow...
} __except(EXCEPTION_EXECUTE_HANDLER) {
   LPBYTE lpPage;
   static SYSTEM_INFO si;
   static MEMORY_BASIC_INFORMATION mi;
   static DWORD dwOldProtect;

   // Get page size of system
   GetSystemInfo(&si);

   // Find SP address
   _asm mov lpPage, esp;

   // Get allocation base of stack
   VirtualQuery(lpPage, &mi, sizeof(mi));

   // Go to page beyond current page
   lpPage = (LPBYTE)(mi.BaseAddress)-si.dwPageSize;

   // Free portion of stack just abandoned
   if(!VirtualFree(mi.AllocationBase, (LPBYTE)lpPage - (LPBYTE)mi.AllocationBase, MEM_DECOMMIT)) {
      // If we get here, exit
      exit(1);
   }

   // Reintroduce the guard page
   if(!VirtualProtect(lpPage, si.dwPageSize, PAGE_GUARD | PAGE_READWRITE, &dwOldProtect)) {
      exit(1);
   }
   
   cerr << "Exception thrown: Error code " << hex << uppercase << _exception_code() << endl;
}