Thread: Help Access Violation

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    10

    Help Access Violation

    Here is my code
    Code:
    #include <iostream>
    
    
    using namespace std;
    
    
    int main ()
    {
        double alpha [50];
        int x;
    
    
        for(x = 1; x <= 50; x++);
        {
            if (x <=25)
                alpha[x] = x * x;
            else 
                alpha[x] = 3 * x;
        
        cout << alpha[x];
        if ( x  == 9)
            cout << endl;
    
    
        }
        return 0;
    }
    and this is what keeps popping up when i debug it and don't know what it means

    Unhandled exception at 0x40632000 in alpha.exe: 0xC0000005: Access violation.
    Last edited by jonesk1978; 03-29-2012 at 06:35 PM.

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Arrays start at index 0, not 1. Change your loop so that it loos through the values 0..49 inclusive, instead of 1..50 inclusive
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    10
    tried that and here is the code with the new array values but still get the same error as before

    Code:
    
    #include <iostream>
    
    
    using namespace std;
    
    
    int main ()
    {
    	double alpha [49];
    	int x;
    
    
    	for(x = 0; x <= 49; x++);
    	{
    		if (x <= 24)
    			alpha[x] = x * x;
    		else 
    			alpha[x] = 3 * x;
    	
    	cout << alpha[x];
    	if ( x %10 == 9)
    		cout << endl;
    
    
    	}
    	return 0;
    }

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Because now you've reduced the size of the array to 49, so it's still overflowing... Keep the size of the array at 50
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    10
    Well i did that and now I get

    "Stack around the variable 'alpha' was corrupted."

  6. #6
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Code:
    #include <iostream>
     
     
    using namespace std;
     
     
    int main ()
    {
        double alpha [50]; // 50 doubles: indexed 0 to 49
        int x;
     
     
        for(x = 0; x <= 49; x++) // ; semicolon?  :)
        {
            if (x <= 24)
                alpha[x] = x * x;
            else
                alpha[x] = 3 * x;
         
            cout << alpha[x];
            if ( x %10 == 9)
                cout << endl;
        }
        return 0;
    }
    Last edited by CodeMonkey; 03-29-2012 at 08:21 PM. Reason: typo
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  7. #7
    Registered User
    Join Date
    Mar 2012
    Posts
    10
    Oh my thank you for that lol

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Access Violation
    By Josh21 in forum C Programming
    Replies: 1
    Last Post: 04-25-2011, 02:57 PM
  2. C - access violation
    By uber in forum C Programming
    Replies: 2
    Last Post: 07-08-2009, 01:30 PM
  3. Access Violation
    By Graham Aker in forum C Programming
    Replies: 100
    Last Post: 01-26-2009, 08:31 PM
  4. DLL Access Violation?
    By durban in forum C++ Programming
    Replies: 12
    Last Post: 10-04-2005, 05:55 PM
  5. access violation
    By bonkey in forum C++ Programming
    Replies: 15
    Last Post: 11-20-2003, 10:22 AM