Solved

Unhandled exception at 0x012510ab in Sim HW5.exe: 0xC0000005: Access violation writing location 0x00000000.

Posted on 2008-10-20
1
710 Views
Last Modified: 2012-05-05
Code runs fine for awhile and then breaks in first "for" loop at random points between Counter2 = 150 and 450 and throws the error:

Unhandled exception at 0x009810ab in Sim HW5.exe: 0xC0000005: Access violation writing location 0x00000000.

Any ideas why?
// Sim HW5.cpp : Defines the entry point for the console application.
//
 
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <cmath>
 
using namespace std;
 
int _tmain(int argc, _TCHAR* argv[])
{
	int Counter = 0;
	int Counter2;
	int Counter3;
 
	int n = 100000;
	
	int fj[20] = {0};
	
	double Divisor;
	double Z0 = 1;
	double Zi;
	double ZiMinusOne;
	double Ui;
	double TwoPowerThirtyOne;
	TwoPowerThirtyOne = pow((double)2,31);
	double v[100000];
 
	while (Counter < n+1){
		if (Counter == 0)
			ZiMinusOne = Z0;
		else
			ZiMinusOne = Zi;
 
		Divisor = (floor( 16807*ZiMinusOne/(TwoPowerThirtyOne-1)));
		Zi = 16807*ZiMinusOne - (Divisor) * (TwoPowerThirtyOne-1);
 
		Ui = Zi/(TwoPowerThirtyOne-1);
 
		v[Counter] = Zi;
 
		Counter++;
		}
 
 
breaks-->	for (Counter2 = 0; Counter2 < n-1; Counter2++){
		for (Counter3 = 0; Counter3 < 20; Counter3++){
			if (v[Counter2] >= Counter3*TwoPowerThirtyOne/20 && v[Counter2] <= (Counter3+1)*TwoPowerThirtyOne/20)
				fj[Counter2]++;
		}
	}
 
	for (Counter2 = 0; Counter2 < n-1; Counter2++)
		cout << "fj" << Counter2 << ": " << fj[Counter2] << endl;
 
	system ("PAUSE");
}

Open in new window

0
Comment
Question by:ltdanp22
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 22758670
Well, the above is running fine here (compiles with VC8). You might want to initialize each and every variable thoroughly though, e.g.
int _tmain(int argc, _TCHAR* argv[])
{
        int Counter = 0;
        int Counter2 = 0; // <--
        int Counter3 = 0; // <--
 
        int n = 100000;
        
        int fj[20] = {0};
        memset(fj,0,20 * sizeof(int)); // <--
        
        double Divisor = 1.0; // <--
        double Z0 = 1;
        double Zi = 0.0; // <--
        double ZiMinusOne = 0.0; // <--
        double Ui = 0.0; // <--
        double TwoPowerThirtyOne = 0.0; // <--
        TwoPowerThirtyOne = pow((double)2,31);
        double v[100000];
        memset(v,0,100000 * sizeof(double)); // <--
 
        while (Counter < n+1){
                if (Counter == 0)
                        ZiMinusOne = Z0;
                else
                        ZiMinusOne = Zi;
 
                Divisor = (floor( 16807*ZiMinusOne/(TwoPowerThirtyOne-1)));
                Zi = 16807*ZiMinusOne - (Divisor) * (TwoPowerThirtyOne-1);
 
                Ui = Zi/(TwoPowerThirtyOne-1);
 
                v[Counter] = Zi;
 
                Counter++;
                }
 
 
                for (Counter2 = 0; Counter2 < n-1; Counter2++){
                for (Counter3 = 0; Counter3 < 20; Counter3++){
                        if (v[Counter2] >= Counter3*TwoPowerThirtyOne/20 && v[Counter2] <= (Counter3+1)*TwoPowerThirtyOne/20)
                                fj[Counter2]++;
                }
        }
 
        for (Counter2 = 0; Counter2 < n-1; Counter2++)
                cout << "fj" << Counter2 << ": " << fj[Counter2] << endl;
 
        system ("PAUSE");
}

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question