CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Nov 2008
    Posts
    18

    Code is crashing

    Hey guys I wrote some code to test my page file size, and it's immediately crashing does anyone know what the issue could be?

    Code:
    #include <iostream>
    #include <windows.h>
    #include <Winbase.h>
    #include <stdio.h>
    #include <Winuser.h>
    #include <limits.h>
    #include <time.h>
    using namespace std;
    int main(int argc, char* argv[])
    {
    /* Variables */
    clock_t startTime, endTime; // Initialize Timers
    int freeBytes;
    int freePage;
    int freeVirtual;
    // System Information
    /*SYSTEM_INFO siSysInfo;
    GetSystemInfo(&siSysInfo);
    siSysInfo.dwPageSize // Page Size of System in Bytes*/
    // System Memory Information
    // http://msdn.microsoft.com/en-us/library/aa366772(VS.85).aspx
    MEMORYSTATUS MemoryInfo;
    GlobalMemoryStatus(&MemoryInfo);
    // The amount of physical memory currently available, in bytes.
    freeBytes = MemoryInfo.dwAvailPhys;
    // The maximum amount of memory the current process can commit, in bytes.
    freePage = MemoryInfo.dwAvailPageFile;
    /* The amount of unreserved and uncommitted memory currently in the
    user-mode portion of the virtual address space of the calling
    process, in bytes. */
    freeVirtual = MemoryInfo.dwAvailVirtual;
    // Output Memory before program start
    cout << "dwAvailPhys: " << MemoryInfo.dwAvailPhys << endl;
    cout << "dwAvailPageFile: " << MemoryInfo.dwAvailPageFile << endl;
    cout << "dwAvailVirtual: " << MemoryInfo.dwAvailVirtual << endl;
    
    double valuesToTest[] = {0.5,0.6,0.7,0.8,0.9,0.95,0.99,1.0,1.01,1.12,1.5};
    int numberOfNewByes;
    int *arrayOfInts; //
    // Loop through each value to test
    for (int i=0; i<16; i++) {
    cout << endl << "Value tested: " << valuesToTest[i] << endl;
    /* Start the timer */
    startTime = clock();
    numberOfNewByes = (int) ( valuesToTest[i] * (freeBytes));
    arrayOfInts = new int[numberOfNewByes/sizeof(int)];
    // Output Memory
    GlobalMemoryStatus(&MemoryInfo);
    cout << "dwAvailPhys: " << MemoryInfo.dwAvailPhys << endl;
    cout << "dwAvailPageFile: " << MemoryInfo.dwAvailPageFile << endl;
    cout << "dwAvailVirtual: " << MemoryInfo.dwAvailVirtual << endl;
    // Fill array with stuffer ones
    for (int x=0; x < numberOfNewByes/sizeof(int); x++){
    arrayOfInts[x]=1;
    }
    // Simple operation of adding 1 to every element
    for (int x=0; x < numberOfNewByes/sizeof(int); x++){
    arrayOfInts[x]=arrayOfInts[x]+1;
    }
    // Free memory
    delete [] arrayOfInts;
    cout << endl << "Time elapsed: " << ((double)(clock() - startTime) / (double)CLOCKS_PER_SEC ) << "seconds " << endl;
    }
    return 0;
    }

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Code is crashing

    1. Define "crash".
    2. Did you debug this code?
    3. On what line of your code does it "crash"?
    Victor Nijegorodov

  3. #3
    Join Date
    Nov 2008
    Posts
    18

    Re: Code is crashing

    it runs the first loop then crashes

    Microsoft Windows [Version 6.1.7601]
    Copyright (c) 2009 Microsoft Corporation. All rights reserved.


    dwAvailPhys: 2321817600
    dwAvailPageFile: 4294967295
    dwAvailVirtual: 2133102592

    Value tested: 0.5

    This application has requested the Runtime to terminate it in an unusual way.
    Please contact the application's support team for more information.

    I did debug it but I could not figure out why it's crashing there seems to be no errors

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Code is crashing

    Quote Originally Posted by Undermine View Post
    it runs the first loop then crashes
    Unfortunately it is very hard to find any loop in your code snippet since you had ignored any indentations there when you posted...

    Quote Originally Posted by Undermine View Post
    ...
    I did debug it but I could not figure out why it's crashing there seems to be no errors
    Then you did it wrong.
    set break point on the line before your "first loop", then press F5 to start debugging, then press F10 to do a step and see int the watch window the values of your local variables and return values of function calls.
    Victor Nijegorodov

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Code is crashing

    Code:
    double valuesToTest[] = {0.5,0.6,0.7,0.8,0.9,0.95,0.99,1.0,1.01,1.12,1.5};
    // Loop through each value to test
    for (int i=0; i<16; i++) {
        cout << endl << "Value tested: " << valuesToTest[i] << endl;
    Your loop runs 16 times, but I only count 11 values in the array.

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Code is crashing

    And to make sure problems that Lindley pointed out do not occur:
    Code:
    double valuesToTest[] = {0.5,0.6,0.7,0.8,0.9,0.95,0.99,1.0,1.01,1.12,1.5};
    // Loop through each value to test
    
    const int numValues = sizeof(valuesToTest) / sizeof(valuesToTest[0]);
    
    for (int i=0; i < numValues; i++) {
        cout << endl << "Value tested: " << valuesToTest[i] << endl;
    Now it doesn't matter how many values are in the array. Note that this works only if valuesToTest is a true array (not a pointer).

    Regards,

    Paul McKenzie

  7. #7
    Join Date
    Jun 2008
    Posts
    592

    Re: Code is crashing

    I want to point out the template way to determine the size which adds a little bit of safety as well.

    Code:
    template< typename TYPE, int Size > 
    size_t GetArrayLength( TYPE (&Array)[Size] ) 
    {     
        return Size; 
    }
    0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
    0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000
    0000 0000 0000 0000

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured