BelowProgramIt is an array with memory reserved, but no physical memory is allocated. Exception Handling is not accessible. when data is written, an exception is triggered and the memory is applied for, which saves memory usage.
// Seh. CPP: defines the entry point for the console application. // # include "stdafx. H "# include <windows. h> # include <stdio. h> # include <assert. h> # include <strsafe. h> typedef struct mytest {char name [10]; int age;} cell, * pcell; const int g_h = 1024; const int g_l = 512; int g_len = 1024*512; typedef cell mystruct [g_h] [g_l]; mystruct * pmytest = NULL; void Init (pbyte * pdata, DWORD dwsize) {* pdata = (pbyte) virtual Alloc (null, dwsize, mem_reserve | mem_top_down, page_readwrite); Assert (null! = Pdata);} Long exceptionfilter (pexception_pointers Pep, DWORD dwsize) {// default to trying another filter (safest thing to do) Long ldisposition = exception_execute_handler ;; // We only fix access violationsif (PEP-> predictionrecord-> predictioncode! = Exception_access_violation) Return (exception_continue_execution); // get address of attempted access and get attempted read or writepvoid pvaddrtouched = (pvoid) pep-> predictionrecord-> predictioninformation [1]; bool battemptedread = (PEP-> predictionrecord-> predictioninformation [0] = 0); void * paddress = pmytest; // is attempted access within this vmarray's reserved region? If (paddress <= pvaddrtouched) & (pvaddrtouched <(pbyte) paddress + dwsize) {tchar SZ [200]; stringcchprintf (SZ, _ countof (sz ), text ("Violation: attempting to % s"), battemptedread? Text ("read"): Text ("write"); printf ("% s \ n", SZ); If (! Battemptedread) {bool bcommittedstorage = false; // assume committing storage failsdo {bcommittedstorage = (null! = Virtualalloc (pvaddrtouched, sizeof (cell), mem_commit, page_readwrite); If (! Bcommittedstorage) {MessageBox (null, text ("Please close some other applications and press OK. "), text (" insufficient memory available "), mb_iconwarning | mb_ OK) ;}} while (! Bcommittedstorage); ldisposition = (bcommittedstorage? Prediction_continue_execution: execution) ;}} return (ldisposition);} static long winapi myhandledexceptionfilter (pexception_pointers pep) {messageboxa (null, "1", "1", mb_ OK ); return exception_execute_handler;} int _ tmain (INT argc, _ tchar * argv []) {// hmodule hmudule = loadlibrary ("kernel32.dll"); // punhandledexceptionfilter = (punhandledexceptionfilter) getprocaddress (hmudule, // ""); setunhandledexceptionfilter (myhandledexceptionfilter); int I = 0; strlen (null); Int J = 10/I; Init (pbyte *) & pmytest, g_len) ;__ try {int A = (* pmytest) [0] [0]. age ;:__ T (exceptionfilter (getexceptioninformation (), g_len) {printf ("111 \ n") ;:__ try {(* pmytest) [0] [0]. age = 10; (* pmytest) [0] [1]. age = 11; (* pmytest) [0] [2]. age = 11 ;:__ T (predictionfilter (getexceptioninformation (), g_len) {printf ("222 \ n") ;}getchar (); Return 0 ;}