栈相关操作[1]

 

  
  
  
  
  1. #include <windows.h> 
  2. #include <stdio.h> 
  3. #include <tchar.h> 
  4.  
  5. /*Code by ping0s1992*/
  6.  
  7. DWORD PrintHeapSize(HANDLE hHeap,LPVOID lpMem); 
  8.  
  9. INT main(INT argc,PTCHAR argv[]){ 
  10.     SYSTEM_INFO si; 
  11.     HANDLE hHeap; //handle of heap 
  12.     LPVOID lpMem; //pointer of memory block 
  13.     LPVOID lpReAlloc; // pointer of realloc memory block 
  14.     DWORD dwHeapSize;  
  15.     HANDLE hHeaps[24]; 
  16.     DWORD dwHeapNum; //number of heap 
  17.     GetSystemInfo(&si); 
  18.     printf("系统内存页大小:0x%x\n系统内存分配粒度:0x%x\n",si.dwPageSize,si.dwAllocationGranularity); 
  19.  
  20.     if(argc == 2 && 0 == lstrcmp(argv[1],TEXT("-a"))){ 
  21.         hHeap = HeapCreate(HEAP_NO_SERIALIZE,si.dwPageSize,si.dwPageSize*10); 
  22.         printf("创建一个大小限定的堆\n"); 
  23.     }else if(argc == 2 && 0 == lstrcmp(argv[1],TEXT("-s"))){ 
  24.         hHeap=GetProcessHeap(); 
  25.         printf("系统中已经存在的堆\n"); 
  26.     }else
  27.         hHeap = HeapCreate(HEAP_NO_SERIALIZE,0,0); 
  28.         printf("创建一个堆,初始化为一页,可自增长.\n"); 
  29.     } 
  30.     if(hHeap == NULL){ 
  31.         printf("创建堆失败:%d\n",GetLastError()); 
  32.         return 1; 
  33.     } 
  34.  
  35.     dwHeapNum = GetProcessHeaps(24,hHeaps); 
  36.     if(dwHeapNum == 0){ 
  37.         printf("获取进程中堆数量失败:%d\n",GetLastError()); 
  38.         return 1; 
  39.     }else
  40.         printf("当前进程中一共有%u个堆.\n",dwHeapNum); 
  41.     } 
  42.     lpMem = HeapAlloc(hHeap,HEAP_ZERO_MEMORY,si.dwPageSize*3); 
  43.     if(lpMem == NULL){ 
  44.         printf("在堆中分配内存失败:%d\n",GetLastError()); 
  45.         return 1; 
  46.     } 
  47.     PrintHeapSize(hHeap,lpMem); 
  48.     lpReAlloc = HeapReAlloc(hHeap,HEAP_ZERO_MEMORY,lpMem,si.dwPageSize*11); 
  49.     if(lpReAlloc == NULL){ 
  50.         printf("在堆中重新分配内存失败:%d",GetLastError()); 
  51.         return 1; 
  52.     } 
  53.     printf("在堆中再分配内存,地址为:0x%x.\n原地址:0x%x\n",lpReAlloc,lpMem); 
  54.  
  55.     return 0; 
  56.  
  57. DWORD PrintHeapSize(HANDLE hHeap,LPVOID lpMem){ 
  58.     SIZE_T dwHeapSize; 
  59.     dwHeapSize = HeapSize(hHeap,HEAP_NO_SERIALIZE,lpMem); 
  60.     if(dwHeapSize == -1){ 
  61.         printf("Get HeapSize error:%d",GetLastError()); 
  62.         return 1; 
  63.     } 
  64.     printf("内存块大小为:0x%x\n",dwHeapSize); 
  65.     return 0; 

你可能感兴趣的:(职场,栈,sdk,休闲)